1

We are working on an application in which we need to pass a SID.

We have an ACLEntry object from which we obtain the UserPrincipal then using reflection, we make the sid field accessible and retrieve it.

    //reflection to get sid string, so we can return sid with userAndGroup name
    Class<?> c = Class.forName("sun.nio.fs.WindowsUserPrincipals$User");
    Field sidString = c.getDeclaredField("sidString");
    sidString.setAccessible(true);
    UserPrincipal userPrincipal = aclEntry.principal();
    String sid = ((String) sidString.get(userPrincipal)).toLowerCase();

This worked on Java 8 but we're migrating to Java 17 and when we run the code, we receive the error: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.String sun.nio.fs.WindowsUserPrincipals$User.sidString accessible: module java.base does not "opens sun.nio.fs" to unnamed module @5f150435

Is there a way to access this sidString without using reflection or is there a way to bypass this InaccessibleObjectException using reflection?

  • Hacking into JDK internals like this is untenable, it should break at any time. If you really need to work with SIDs then you will need JNI code, or in the future, the FFM API (currently in preview in JDK 20). – Alan Bateman Mar 03 '23 at 09:29
  • Could you elaborate on your comment with an answer please? – Paul Curran Mar 06 '23 at 10:26

2 Answers2

0

It looks like this has already been answered.

We added the following to the VM Arguments and the reflection code worked.

 --add-opens has the following syntax: {A}/{package}={B}
java --add-opens java.base/java.lang=ALL-UNNAMED

To add it to the VM Arguments automatically, we updated pom file. : https://stackoverflow.com/questions/70196098/how-can-i-specify-add-opens-from-a-project-level-and-make-sure-it-is-taken-int

Then for the : https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

-2

You can pass your field in to this to force it to be accessible:

public static <T extends AccessibleObject> T forceAccessible(T o) { //code taken from https://github.com/TheOneAndOnlyDanSan/Reflection
    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];
        unsafe.putBoolean(override, unsafe.objectFieldOffset(override), true);

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

edit: replaced deprecated "o.isAccessible()" with "!override.getBoolean(o)" and added "unsafe.putBoolean(override, unsafe.objectFieldOffset(override), true);"

  • 1
    Thanks for the suggestion, however I'm not having much success implementing this. _isAccessible_ is deprecated so I updated this to `!o.canAccess(userPrincipal)` where _userPrincipal_ is an instance of UserPrincipal. Even with this change, an exception is thrown at the line `m.setAccessible(true)`. Which Java version are you using? – Paul Curran Sep 12 '22 at 13:52
  • @PaulCurran I updated the code snippet in the answer as you suggested, does it work for you now? – TheOneAndOnlyDanSan Sep 12 '22 at 19:43
  • unfortunately no, the `m.setAccessible(true)` line throws an _java.lang.reflect.InaccessibleObjectException: Unable to make private native java.lang.reflect.Field[] java.lang.Class.getDeclaredFields0(boolean) accessible: module java.base does not "opens java.lang" to unnamed module_ exception so it never even reaches the code to override _o_. – Paul Curran Sep 15 '22 at 09:00
  • @PaulCurran which Java version are you using? (it works for me with java 17 and 18) – TheOneAndOnlyDanSan Sep 15 '22 at 11:07
  • Strange, we're migrating to 17. I'll take a look again and try to see if I can work out why it's failing for us. – Paul Curran Sep 16 '22 at 09:25