Java Code Examples for java.lang.invoke.MethodHandles.Lookup#dropLookupMode()

The following examples show how to use java.lang.invoke.MethodHandles.Lookup#dropLookupMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DropLookupModeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Starting with a full power Lookup, use dropLookupMode to create new Lookups
 * with reduced access.
 */
public void testReducingAccess() {
    Lookup lookup = MethodHandles.lookup();
    final Class<?> lc = lookup.lookupClass();
    assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PROTECTED|PRIVATE));

    lookup = lookup.dropLookupMode(PROTECTED);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE|PRIVATE));

    lookup = lookup.dropLookupMode(PRIVATE);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == (PUBLIC|MODULE|PACKAGE));

    lookup = lookup.dropLookupMode(PACKAGE);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == (PUBLIC|MODULE));

    lookup = lookup.dropLookupMode(MODULE);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == PUBLIC);

    lookup = lookup.dropLookupMode(PUBLIC);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == 0);

    // repeat with lookup has no access
    lookup = lookup.dropLookupMode(PUBLIC);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == 0);
}
 
Example 2
Source File: DropLookupModeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test dropLookupMode on the public Lookup.
 */
public void testPublicLookup() {
    final Lookup publicLookup = MethodHandles.publicLookup();
    final Class<?> lc = publicLookup.lookupClass();
    assertTrue(publicLookup.lookupModes() == (PUBLIC|UNCONDITIONAL));

    Lookup lookup = publicLookup.dropLookupMode(PRIVATE);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == PUBLIC);

    lookup = publicLookup.dropLookupMode(PROTECTED);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == PUBLIC);

    lookup = publicLookup.dropLookupMode(PACKAGE);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == PUBLIC);

    lookup = publicLookup.dropLookupMode(MODULE);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == PUBLIC);

    lookup = publicLookup.dropLookupMode(PUBLIC);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == 0);

    lookup = publicLookup.dropLookupMode(UNCONDITIONAL);
    assertTrue(lookup.lookupClass() == lc);
    assertTrue(lookup.lookupModes() == PUBLIC);
}
 
Example 3
Source File: DefineClassHelper.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Loads a class file by {@code java.lang.invoke.MethodHandles.Lookup}.
 *
 * @since 3.22
 */
static Class<?> toPublicClass(String className, byte[] bcode)
    throws CannotCompileException
{
    try {
        Lookup lookup = MethodHandles.lookup();
        lookup = lookup.dropLookupMode(java.lang.invoke.MethodHandles.Lookup.PRIVATE);
        return lookup.defineClass(bcode);
    }
    catch (Throwable t) {
        throw new CannotCompileException(t);
    }
}