Repository: ant-ivy
Updated Branches:
  refs/heads/master 1d3567f30 -> 7ebc8107a


IVY-1569 Avoid reflection warnings (or worse) in Java 9+

Project: http://git-wip-us.apache.org/repos/asf/ant-ivy/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant-ivy/commit/7ebc8107
Tree: http://git-wip-us.apache.org/repos/asf/ant-ivy/tree/7ebc8107
Diff: http://git-wip-us.apache.org/repos/asf/ant-ivy/diff/7ebc8107

Branch: refs/heads/master
Commit: 7ebc8107a22038c06c0b0ff095f9f73d854e1422
Parents: 1d3567f
Author: Gintas Grigelionis <gin...@apache.org>
Authored: Fri Feb 2 19:18:57 2018 +0100
Committer: Gintas Grigelionis <gin...@apache.org>
Committed: Fri Feb 2 19:18:57 2018 +0100

----------------------------------------------------------------------
 .../apache/ivy/util/url/IvyAuthenticator.java   | 61 ++++++++++++++------
 1 file changed, 44 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant-ivy/blob/7ebc8107/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/ivy/util/url/IvyAuthenticator.java 
b/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
index 0b0ce6b..c29d694 100644
--- a/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
+++ b/src/java/org/apache/ivy/util/url/IvyAuthenticator.java
@@ -18,6 +18,7 @@
 package org.apache.ivy.util.url;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.net.Authenticator;
 import java.net.PasswordAuthentication;
 
@@ -50,26 +51,20 @@ public final class IvyAuthenticator extends Authenticator {
         // We will try to use the original authenticator as backup 
authenticator.
         // Since there is no getter available, so try to use some reflection to
         // obtain it. If that doesn't work, assume there is no original 
authenticator
-        Authenticator original = null;
+        Authenticator original = (getJavaVersion() < 9) ? getTheAuthenticator()
+                : getDefaultAuthenticator();
 
-        try {
-            Field f = Authenticator.class.getDeclaredField("theAuthenticator");
-            f.setAccessible(true);
-            original = (Authenticator) f.get(null);
-        } catch (Throwable t) {
-            Message.debug("Error occurred while getting the original 
authenticator: "
-                    + t.getMessage());
+        if (original instanceof IvyAuthenticator) {
+            return;
         }
 
-        if (!(original instanceof IvyAuthenticator)) {
-            try {
-                Authenticator.setDefault(new IvyAuthenticator(original));
-            } catch (SecurityException e) {
-                if (!securityWarningLogged) {
-                    securityWarningLogged = true;
-                    Message.warn("Not enough permissions to set the 
IvyAuthenticator. "
-                            + "HTTP(S) authentication will be disabled!");
-                }
+        try {
+            Authenticator.setDefault(new IvyAuthenticator(original));
+        } catch (SecurityException e) {
+            if (!securityWarningLogged) {
+                securityWarningLogged = true;
+                Message.warn("Not enough permissions to set the 
IvyAuthenticator. "
+                        + "HTTP(S) authentication will be disabled!");
             }
         }
     }
@@ -121,4 +116,36 @@ public final class IvyAuthenticator extends Authenticator {
         return RequestorType.PROXY.equals(getRequestorType());
     }
 
+    private static Authenticator getDefaultAuthenticator() {
+        try {
+            final Method m = 
Authenticator.class.getDeclaredMethod("getDefault");
+            return (Authenticator) m.invoke(null);
+        } catch (final Throwable t) {
+            handleReflectionException(t);
+        }
+        return null;
+    }
+
+    private static Authenticator getTheAuthenticator() {
+        try {
+            Field f = Authenticator.class.getDeclaredField("theAuthenticator");
+            f.setAccessible(true);
+            return (Authenticator) f.get(null);
+        } catch (final Throwable t) {
+            handleReflectionException(t);
+        }
+        return null;
+    }
+
+    private static void handleReflectionException(final Throwable t) {
+        Message.debug("Error occurred while getting the original 
authenticator: "
+                + t.getMessage());
+    }
+
+    private static int getJavaVersion() {
+        // See 
https://docs.oracle.com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp90002
+        final String[] version = 
System.getProperty("java.version").split("\\.");
+        final int major = Integer.parseInt(version[0]);
+        return major == 1 ? Integer.parseInt(version[1]) : major;
+    }
 }

Reply via email to