Hi Paul,

I wanted to try using VarHandles for code internal to JDK but there's a problem with MethodHandles.lookup(). It doesn't allow the caller class loaded by the bootstrap class loader and located in either java.* or sun.* (but not sun.invoke.*) packages:


private static void checkUnprivilegedlookupClass(Class<?> lookupClass, int allowedModes) {
            String name = lookupClass.getName();
            if (name.startsWith("java.lang.invoke."))
throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);

            // For caller-sensitive MethodHandles.lookup()
            // disallow lookup more restricted packages
if (allowedModes == ALL_MODES && lookupClass.getClassLoader() == null) {
                if (name.startsWith("java.") ||
(name.startsWith("sun.") && !name.startsWith("sun.invoke."))) { throw newIllegalArgumentException("illegal lookupClass: " + lookupClass);
                }
            }
        }


...strangely, other bootstrap class loaded callers located in jdk.* are allowed. Why such distinction? Is there or will there be an official way to use VarHandles in JDK code and not having to resort to work-arounds like MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP").setAccessible(true)?


Regards, Peter

Reply via email to