Run Java 8 code on JDK 11 – what might break? [closed]

There are (at least) two specific things to look for:

  1. Reflective access to private APIs

  2. Use of MethodHandles

Java 9 and subsequent introduced the module system which enforces (lack of) access to private APIs, even through reflection. Java 8 applications would only run without modification on Java 9 but there are no guarantees for subsequent JREs. As a practical example, I have some Processor implementations that find the JavaFileManager through reflection:

            try {                                                               
                 * .getContext() ->            
                Object context =                                                
                fm =                                                            
                    .getMethod("get", Class.class)                              
                    .invoke(context, JavaFileManager.class);                    
            } catch (Exception exception) {                                     

that now generates a warning:

WARNING: An illegal reflective access operation has occurred                                                                                                                                                                           
WARNING: Illegal reflective access by ball.annotation.processing.AbstractProcessor (file:/Users/ball/.m2/repository/ball/ball-util/6.0.1-SNAPSHOT/ball-util-6.0.1-SNAPSHOT.jar) to method\
WARNING: Please consider reporting this to the maintainers of ball.annotation.processing.AbstractProcessor                                                                                                                             
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations                                                                                                                                  
WARNING: All illegal access operations will be denied in a future release

The method for accessing MethodHandles in Java 8 will not work in Java 9 and subsequent and vice versa.

Java 8’s

        Constructor<MethodHandles.Lookup> constructor =                         
        Class<?> declarer = method.getDeclaringClass();                         
        Object result =                                                         
            .unreflectSpecial(method, declarer)                                 

In Java 9 and later must be replaced with:

        Class<?> declarer = method.getDeclaringClass();                         
        Object result =                                                         
            .findSpecial(declarer, method.getName(),                            

Browse More Popular Posts

Leave a Comment