JDK-8150173 : JAXBContext.newInstance causes PrivilegedActionException when createContext's declared in abstract class extended by discovered JAXB implementation
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxb
  • Affected Version: 9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2016-02-18
  • Updated: 2016-07-05
  • Resolved: 2016-06-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b125Fixed
Related Reports
Relates :  
Description
Let's consider code below:

import javax.xml.bind.*;
import java.util.Map;

public class Test7 {
    private static JAXBContext tmp;

    public static abstract class FactoryBase implements JAXBContextFactory {
        @Override
        public JAXBContext createContext(Class<?>[] classesToBeBound, Map<String, ?> properties) throws JAXBException {
            return tmp;
        }

        @Override
        public JAXBContext createContext(String contextPath, ClassLoader classLoader, Map<String, ?> properties)
                throws JAXBException {
            return tmp;
        }
    }

    public static class Factory extends FactoryBase {}

    public static void main(String[] args) throws JAXBException {
        tmp = JAXBContext.newInstance(Test7.class);
        System.setProperty(JAXBContext.JAXB_CONTEXT_FACTORY, "Test7$Factory");
        JAXBContext.newInstance(Test7.class);
    }
}

It causes following output on JDK9b104 however it should have succeeded:

Exception in thread "main" javax.xml.bind.JAXBException: Provider class Test7$FactoryBase could not be instantiated: java.security.PrivilegedActionException: java.lang.InstantiationException
 - with linked exception:
[java.security.PrivilegedActionException: java.lang.InstantiationException]
	at javax.xml.bind.ContextFinder.instantiateProviderIfNecessary(ContextFinder.java:244)
	at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:274)
	at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:265)
	at javax.xml.bind.ContextFinder.find(ContextFinder.java:369)
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:659)
	at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:603)
	at Test7.main(Test7.java:25)
Disconnected from the target VM, address: '127.0.0.1:61426', transport: 'socket'
Caused by: java.security.PrivilegedActionException: java.lang.InstantiationException
	at java.security.AccessController.doPrivileged(Native Method)
	at javax.xml.bind.ContextFinder.instantiateProviderIfNecessary(ContextFinder.java:235)
	... 6 more
Caused by: java.lang.InstantiationException
	at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:426)
	at java.lang.Class.newInstance(Class.java:466)
	at javax.xml.bind.ContextFinder$2.run(ContextFinder.java:238)
	... 8 more

Comments
Updated webrev: http://cr.openjdk.java.net/~dfuchs/webrev_8150173/webrev.01/
22-06-2016

Thanks Mandy - yes that's what the patch I proposed on core-libs-dev does: http://cr.openjdk.java.net/~dfuchs/webrev_8150173/webrev.00/
21-06-2016

The spec of JAXBContext states: The class is implementation of JAXBContextFactory. It must also implement no-arg constructor. If discovered in other step then 3, new instance using no-arg constructor is created first. After that, appropriate instance method is invoked on this instance. The bug may be just this line: Class<?> declaringClass = m.getDeclaringClass();
21-06-2016

Daniel, the failing tests (in the binaries) are at: http://jre.us.oracle.com/java/re/jck/9/promoted/latest/binaries/JCK-runtime-9/tests/api/javax_xml/bind/JAXBContext/NewInstance_Classes/ http://jre.us.oracle.com/java/re/jck/9/promoted/latest/binaries/JCK-runtime-9/tests/api/javax_xml/bind/JAXBContext/NewInstance_ClassesMap/ http://jre.us.oracle.com/java/re/jck/9/promoted/latest/binaries/JCK-runtime-9/tests/api/javax_xml/bind/JAXBContext/NewInstance_StringClassLoader/ http://jre.us.oracle.com/java/re/jck/9/promoted/latest/binaries/JCK-runtime-9/tests/api/javax_xml/bind/JAXBContext/NewInstance_String/ http://jre.us.oracle.com/java/re/jck/9/promoted/latest/binaries/JCK-runtime-9/tests/api/javax_xml/bind/JAXBContext/NewInstance_StringClassLoaderMap/ They should be at a similar location in the latest bundles. I should also add that latest = b38 at the time I added this comment.
20-06-2016

Daniel - Mandy suggested that you may be able to help on this one. Can you take a look?
15-06-2016