2

Recently passed from Java 9 to Java 11 where the code below used to work fine :

        final Field a = bb.class.getDeclaredField("a");
        a.setAccessible(true);
        final Field b = bb.class.getDeclaredField("b");
        b.setAccessible(true);
        final Field modifersField = Field.class.getDeclaredField("modifiers");
        modifersField.setAccessible(true);
        modifersField.setInt(a, ~Modifier.FINAL & a.getModifiers());
        modifersField.setInt(b, ~Modifier.FINAL & b.getModifiers());
        a.set(null, BigInteger.valueOf(5));
        b.set(null, BigInteger.valueOf(5));
        modifersField.setAccessible(false);

But in Java 11 i get this error :

java.lang.reflect.InaccessibleObjectException: Unable to make field private int java.lang.reflect.Field.modifiers accessible: module java.base does not "opens java.lang.reflect" to module 

I tried added VM Parameters like :

--add-exports
java.base/java.lang.reflect=com.goxr3plus.applicationName

But again nothing ... just a warning and application crashes :

WARNING: package java.lang.reflect not in javafx.base

What am i doing wrong ?

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • 3
    the linked two answers, explain the same thing – Eugene May 28 '19 at 12:21
  • 6
    The java.lang.reflect package is in java.base, not javafx.base. In any case, hacking the private/undocumented Field.modifiers field is horrible and no guarantee that it won't break at any time (JDK 12 at least). – Alan Bateman May 28 '19 at 13:23
  • 2
    This sounds like an XY problem. It should not be necessary to violate encapsulation by modifying private or final fields. – VGR May 28 '19 at 14:40
  • @VGR I need to do it , really . I don't understand why Java 11 removed this important concept . – GOXR3PLUS Jun 10 '19 at 07:34
  • 2
    As I said: encapsulation. Private members are private for a reason. They constitute the internal implementation of a class, which can change at any time. The class’s responsibility is to provide a result according to each method’s contract. How it does that is the class’s own business. – VGR Jun 10 '19 at 13:39

0 Answers0