summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Treehugger Robot <treehugger-gerrit@google.com> 2021-03-19 17:21:29 +0000
committer Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> 2021-03-19 17:21:29 +0000
commit3df03a83afbc47d5b2c00374fcb9ef9bb3462618 (patch)
treed9b0f63b49fa1327c732b5c3019e00f0a9f471f8
parent1941a166716bc5e38fcc746b525096f85303b2e4 (diff)
parentcd37173df6570293a4ef9d072e42eca1f08d1d7f (diff)
Merge "Fix InetAddressCompat exception handling" am: cd37173df6
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1645300 Change-Id: I1d65d6b011e5ce585835e1c9df9eea0afb74b0f4
-rw-r--r--packages/Connectivity/framework/src/android/net/InetAddressCompat.java38
1 files changed, 25 insertions, 13 deletions
diff --git a/packages/Connectivity/framework/src/android/net/InetAddressCompat.java b/packages/Connectivity/framework/src/android/net/InetAddressCompat.java
index 8404441de669..6b7e75c75359 100644
--- a/packages/Connectivity/framework/src/android/net/InetAddressCompat.java
+++ b/packages/Connectivity/framework/src/android/net/InetAddressCompat.java
@@ -41,7 +41,12 @@ public class InetAddressCompat {
public static void clearDnsCache() {
try {
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);
}
}
@@ -51,13 +56,7 @@ public class InetAddressCompat {
*/
public static InetAddress[] getAllByNameOnNet(String host, int netId) throws
UnknownHostException {
- try {
- 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);
- }
+ return (InetAddress[]) callGetByNameMethod("getAllByNameOnNet", host, netId);
}
/**
@@ -65,12 +64,25 @@ public class InetAddressCompat {
*/
public static InetAddress getByNameOnNet(String host, int netId) throws
UnknownHostException {
+ return (InetAddress) callGetByNameMethod("getByNameOnNet", host, netId);
+ }
+
+ private static Object callGetByNameMethod(String method, String host, int netId)
+ throws UnknownHostException {
try {
- return (InetAddress) InetAddress.class.getMethod("getByNameOnNet",
- 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 getByNameOnNet", e);
+ return InetAddress.class.getMethod(method, String.class, int.class)
+ .invoke(null, host, netId);
+ } catch (InvocationTargetException e) {
+ if (e.getCause() instanceof UnknownHostException) {
+ 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);
}
}
}