RFR: 8150173: JAXBContext.newInstance causes PrivilegedActionException when createContext's declared in absract class extended by discovered JAXB implementation

Georgiy Rakov georgiy.rakov at oracle.com
Wed Jun 22 10:55:04 UTC 2016


This is about backward compatibility, but anyway would it be good to add 
to tests static method case (also please see the attached file):

     public static class FactoryBase {
         public static JAXBContext createContext(Class[] 
classesToBeBound, Map<String, Object> properties) throws JAXBException {
             return tmp;
         }

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

     public static class Factory extends FactoryBase {
     }

Now it works fine but it might be useful as a test anyway.

It's a valid case according to spec which reads (please see point 2):

  Once the provider factory class is discovered, context creation is 
delegated to one of its createContext(...) methods. For backward 
compatibility reasons, there are two ways how to implement provider 
factory class:

     ...

     2. the class is not implementation of interface above and then it 
is mandated to implement the following static method signatures:

      public static JAXBContext createContext(
                                           String contextPath,
                                           ClassLoader classLoader,
Map<String,Object> properties ) throws JAXBException

      public static JAXBContext createContext(
                                           Class[] classes,
Map<String,Object> properties ) throws JAXBException


     In this scenario, appropriate static method is used instead of 
instance method. This approach is incompatible with ServiceLoader so it 
can't be used with step 3.

Thanks,
Georgiy.

On 21.06.2016 20:39, Daniel Fuchs wrote:
> Hi,
>
> Please find below a somewhat trivial patch for
>
> 8150173: JAXBContext.newInstance causes PrivilegedActionException
>          when createContext's declared in absract class extended
>          by discovered JAXB implementation
> https://bugs.openjdk.java.net/browse/JDK-8150173
>
> Patch:
> http://cr.openjdk.java.net/~dfuchs/webrev_8150173/webrev.00
>
> This is an oversight that was introduced with JDK-8145104.
>
> The issue is simply that newInstance() must be invoked on
> the concrete class, not on the class that defines the
> createContext method.
>
> best regards,
>
> -- daniel

-------------- next part --------------
import javax.xml.bind.*;
import java.util.Map;

public class Test8 {
    private static JAXBContext tmp =null;

    public static class FactoryBase {
        public static JAXBContext createContext(Class[] classesToBeBound, Map<String, Object> properties) throws JAXBException {
            return tmp;
        }

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

    public static class Factory extends FactoryBase {
    }

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


More information about the core-libs-dev mailing list