MethodNadles.lookup() for bootstrap class loader loaded classes

Peter Levart peter.levart at
Fri Apr 15 07:33:28 UTC 2016

Hi Paul,

On 04/14/2016 04:40 PM, Paul Sandoz wrote:
> Hi Peter,
> You found that annoying restriction :-) at this point i think this is mostly redundant.
> This is something i planned to update and limit the restriction to code within j.l.invoke and sun.invoke packages.

sun.invoke is explicitly allowed currently.

> I'll follow up with a patch soon to unblock, but feel free to beat me to it if you wish.

I don't quite understand this restriction. Seems to be that it was 
written at the time where the only classes loaded by bootstrap class 
loader were located in packages java.** and sun.** (was that actually 
true at some point?) and the restriction explicitly excludes classes in 
sun.invoke.** packages as though they are the only trusted code to be 
able to obtain such lookup. Does a Lookup with a lookup class loaded by 
the bootstrap class loader and allowedModes == ALL_MODES possess any 
special privileges that a Lookup with a lookup class loaded by the 
application class loader and allowedModes == ALL_MODES doesn't?

Regards, Peter

> Paul.
>> On 14 Apr 2016, at 16:25, Peter Levart <peter.levart at> wrote:
>> 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

More information about the core-libs-dev mailing list