Merge "Fix InetAddressCompat exception handling" am: cd37173df6 am: ef16d9420d

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1645300

Change-Id: Ib33b1d69e8480ba1ee2193671a88ed91d624d4bc
This commit is contained in:
Treehugger Robot
2021-03-19 18:01:07 +00:00
committed by Automerger Merge Worker

View File

@@ -41,7 +41,12 @@ public class InetAddressCompat {
public static void clearDnsCache() { public static void clearDnsCache() {
try { try {
InetAddress.class.getMethod("clearDnsCache").invoke(null); InetAddress.class.getMethod("clearDnsCache").invoke(null);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new IllegalStateException("Unknown InvocationTargetException", e.getCause());
} catch (IllegalAccessException | NoSuchMethodException e) {
Log.wtf(InetAddressCompat.class.getSimpleName(), "Error clearing DNS cache", e); Log.wtf(InetAddressCompat.class.getSimpleName(), "Error clearing DNS cache", e);
} }
} }
@@ -51,13 +56,7 @@ public class InetAddressCompat {
*/ */
public static InetAddress[] getAllByNameOnNet(String host, int netId) throws public static InetAddress[] getAllByNameOnNet(String host, int netId) throws
UnknownHostException { UnknownHostException {
try { return (InetAddress[]) callGetByNameMethod("getAllByNameOnNet", host, netId);
return (InetAddress[]) InetAddress.class.getMethod("getAllByNameOnNet",
String.class, int.class).invoke(null, host, netId);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling getAllByNameOnNet", e);
throw new IllegalStateException("Error querying via getAllNameOnNet", e);
}
} }
/** /**
@@ -65,12 +64,25 @@ public class InetAddressCompat {
*/ */
public static InetAddress getByNameOnNet(String host, int netId) throws public static InetAddress getByNameOnNet(String host, int netId) throws
UnknownHostException { UnknownHostException {
return (InetAddress) callGetByNameMethod("getByNameOnNet", host, netId);
}
private static Object callGetByNameMethod(String method, String host, int netId)
throws UnknownHostException {
try { try {
return (InetAddress) InetAddress.class.getMethod("getByNameOnNet", return InetAddress.class.getMethod(method, String.class, int.class)
String.class, int.class).invoke(null, host, netId); .invoke(null, host, netId);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { } catch (InvocationTargetException e) {
Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling getAllByNameOnNet", e); if (e.getCause() instanceof UnknownHostException) {
throw new IllegalStateException("Error querying via getByNameOnNet", e); throw (UnknownHostException) e.getCause();
}
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new IllegalStateException("Unknown InvocationTargetException", e.getCause());
} catch (IllegalAccessException | NoSuchMethodException e) {
Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling " + method, e);
throw new IllegalStateException("Error querying via " + method, e);
} }
} }
} }