2

Recently I upgraded to JDK19 and I am trying to list all the declared fields in the Method class. But it returns nothing.

import java.lang.reflect.Method;
import java.util.Arrays;

public class MethodFieldsDemo {
    public static void main(String... args){
        Arrays.stream(Method.class.getDeclaredFields()).forEach(f-> System.out.println(f.getName()));
    }
}

When I run the same program using JDK 8, it returns all the fields.

Am I missing something or is there anything I need to do when workign with JDK 19.

Sai Dandem
  • 8,229
  • 11
  • 26
  • 1
    I had to download Java 8 from an archive and run it to confirm. I don't know what changed, but I have the same issue when running with Java 17. – hfontanez Mar 14 '23 at 01:07
  • 1
    Yes, this is intentional, starting with JDK9 every release breaks this kind of introspection; when one module does not export a package, it cannot be accessed like this, and no amount of `setAccessible` will let you get around that. `--add-opens` can. – rzwitserloot Mar 14 '23 at 01:15
  • 6
    @rzwitserloot it is not modules, it is the [reflection filter](https://github.com/openjdk/jdk/blob/49181b81dd284f65455492183ce5d0ab38b48d52/src/java.base/share/classes/jdk/internal/reflect/Reflection.java#L62). Even `--add-opens` doesn't help. – Johannes Kuhn Mar 14 '23 at 02:17

0 Answers0