8150173: JAXBContext.newInstance causes PrivilegedActionException when createContext's declared in abstract class extended by discovered JAXB implementation
authordfuchs
Fri, 24 Jun 2016 16:00:03 +0100
changeset 39173 68169040e394
parent 39172 924b45dbcafe
child 39174 e5db0b0ec387
8150173: JAXBContext.newInstance causes PrivilegedActionException when createContext's declared in abstract class extended by discovered JAXB implementation Summary: trivial fix - the concrete class must be instantiated, not the class that defines the createContext method. Reviewed-by: lancea, mchung
jaxws/src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java
--- a/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java	Thu Jun 23 20:35:14 2016 +0000
+++ b/jaxws/src/java.xml.bind/share/classes/javax/xml/bind/ContextFinder.java	Fri Jun 24 16:00:03 2016 +0100
@@ -193,7 +193,7 @@
             try {
                 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class, Map.class);
                 // any failure in invoking this method would be considered fatal
-                Object obj = instantiateProviderIfNecessary(m);
+                Object obj = instantiateProviderIfNecessary(spFactory);
                 context = m.invoke(obj, contextPath, classLoader, properties);
             } catch (NoSuchMethodException ignored) {
                 // it's not an error for the provider not to have this method.
@@ -203,7 +203,7 @@
                 // try the old method that doesn't take properties. compatible with 1.0.
                 // it is an error for an implementation not to have both forms of the createContext method.
                 Method m = spFactory.getMethod("createContext", String.class, ClassLoader.class);
-                Object obj = instantiateProviderIfNecessary(m);
+                Object obj = instantiateProviderIfNecessary(spFactory);
                 // any failure in invoking this method would be considered fatal
                 context = m.invoke(obj, contextPath, classLoader);
             }
@@ -228,20 +228,20 @@
         }
     }
 
-    private static Object instantiateProviderIfNecessary(Method m) throws JAXBException {
-        Class<?> declaringClass = m.getDeclaringClass();
+    private static Object instantiateProviderIfNecessary(Class<?> implClass) throws JAXBException {
         try {
-            if (JAXBContextFactory.class.isAssignableFrom(declaringClass)) {
+            if (JAXBContextFactory.class.isAssignableFrom(implClass)) {
                 return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                     @Override
                     public Object run() throws Exception {
-                        return declaringClass.newInstance();
+                        return implClass.newInstance();
                     }
                 });
             }
             return null;
-        } catch (PrivilegedActionException e) {
-            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, declaringClass, e), e);
+        } catch (PrivilegedActionException x) {
+            Throwable e = (x.getCause() == null) ? x : x.getCause();
+            throw new JAXBException(Messages.format(Messages.COULD_NOT_INSTANTIATE, implClass, e), e);
         }
     }
 
@@ -271,7 +271,7 @@
         try {
 
             Method m = spFactory.getMethod("createContext", Class[].class, Map.class);
-            Object obj = instantiateProviderIfNecessary(m);
+            Object obj = instantiateProviderIfNecessary(spFactory);
             Object context = m.invoke(obj, classes, properties);
             if (!(context instanceof JAXBContext)) {
                 // the cast would fail, so generate an exception with a nice message
@@ -392,7 +392,7 @@
 
 
     /**
-     * first factoryId should be the preffered one,
+     * first factoryId should be the preferred one,
      * more of those can be provided to support backwards compatibility
      */
     private static String classNameFromPackageProperties(ClassLoader classLoader,