Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellojinjie/6650799 to your computer and use it in GitHub Desktop.
Save hellojinjie/6650799 to your computer and use it in GitHub Desktop.
sun.net.www.protocol.http.HttpURLConnection.java 中一段很搞的代码。 内部匿名类无法访问外面的非 final 的变量。这里用数组的形式进行 workaround
final boolean result[] = {false};
java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
public Object run() {
try {
InetAddress a1 = InetAddress.getByName(h1);
InetAddress a2 = InetAddress.getByName(h2);
result[0] = a1.equals(a2);
} catch (UnknownHostException e) {
} catch (SecurityException e) {
}
return null;
}
});
return result[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment