简体   繁体   中英

Issue while updating java project from JDK11 to JDK17

I have a java project using jdk11. I need to migrate the project to java 17 but integration tests have started failing. As soon as I upgraded to JDK17 I started getting below error:

java.lang.reflect.InaccessibleObjectException: Unable to make private sun.reflect.generics.repository.FieldRepository java.lang.reflect.Field.getGenericInfo() accessible: module java.base does not "opens java.lang.reflect" to unnamed module @5702b3b1

I was also getting error for java.util but that is fixed using below command line code.

I have tried adding the command line options as below but the tests are still failing

                <argLine>
                    --illegal-access=permit
                    --add-opens java.base/java.lang=ALL-UNNAMED
                    --add-opens java.base/sun.reflect=ALL-UNNAMED
                    --add-opens java.base/java.util=ALL-UNNAMED
                </argLine>

Any help is appreciated.

You can pass your method in to this:

public static <T extends AccessibleObject> T forceAccessible(T o) {
    try {
        Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);

        Method m = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
        m.setAccessible(true);
        Field override = ((Field[]) m.invoke(AccessibleObject.class, false))[0];

        if (!o.isAccessible()) {
            try {
                o.setAccessible(true);
            } catch (InaccessibleObjectException e) {
                unsafe.putBoolean(o, unsafe.objectFieldOffset(override), true);
            }
        }
    } catch (Exception e) {}
    return o;
}

I made a library for this kind of stuff:

https://github.com/TheOneAndOnlyDanSan/Reflection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM