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

The following examples show how to use java.lang.invoke.MethodHandles#privateLookupIn() . 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: FieldValueReaderImpl.java    From hibernate-demos with Apache License 2.0 6 votes vote down vote up
public FieldValueReaderImpl(PackageOpener packageOpener) {
    this.lookups = new ClassValue<Lookup>() {

        @Override
        protected Lookup computeValue(Class<?> type) {
            if ( !getClass().getModule().canRead( type.getModule() ) ) {
                getClass().getModule().addReads( type.getModule() );
            }

            packageOpener.openPackageIfNeeded(
                    type.getModule(), type.getPackageName(), FieldValueReaderImpl.class.getModule()
            );

            try {
                return MethodHandles.privateLookupIn( type, MethodHandles.lookup() );
            }
            catch (IllegalAccessException e) {
                throw new RuntimeException( e );
            }
        }
    };
}
 
Example 2
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testTargetClassInOpenModule() throws Throwable {
    // m1/p1.Type
    Class<?> clazz = Class.forName("p1.Type");
    assertEquals(clazz.getModule().getName(), "m1");

    // ensure that this module reads m1
    Module thisModule = getClass().getModule();
    Module m1 = clazz.getModule();
    thisModule.addReads(clazz.getModule());
    assertTrue(m1.isOpen("p1", thisModule));

    Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
    assertTrue(lookup.lookupClass() == clazz);
    assertTrue(lookup.hasPrivateAccess());

    // get obj field
    MethodHandle mh = lookup.findStaticGetter(clazz, "obj", Object.class);
    Object obj = mh.invokeExact();
}
 
Example 3
Source File: Striped64.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public MethodHandles.Lookup run() {
    try {
        return MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup());
    } catch (ReflectiveOperationException e) {
        throw new Error(e);
    }
}
 
Example 4
Source File: Striped64.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public MethodHandles.Lookup run() {
    try {
        return MethodHandles.privateLookupIn(Thread.class, MethodHandles.lookup());
    } catch (ReflectiveOperationException e) {
        throw new ExceptionInInitializerError(e);
    }
}
 
Example 5
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = {IllegalAccessException.class})
public void testCallerDoesNotRead() throws Throwable {
    // m2/p2.Type
    Class<?> clazz = Class.forName("p2.Type");
    assertEquals(clazz.getModule().getName(), "m2");

    Module thisModule = getClass().getModule();
    Module m2 = clazz.getModule();
    assertFalse(thisModule.canRead(m2));
    assertTrue(m2.isOpen("p2", thisModule));

    Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
}
 
Example 6
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = {IllegalAccessException.class})
public void testNotOpenToCaller() throws Throwable {
    // m3/p2.Type
    Class<?> clazz = Class.forName("p3.Type");
    assertEquals(clazz.getModule().getName(), "m3");

    Module thisModule = getClass().getModule();
    Module m3 = clazz.getModule();
    thisModule.addReads(clazz.getModule());
    assertFalse(m3.isOpen("p3", thisModule));

    Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
}
 
Example 7
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}.
 * It is obtained by using {@code neighbor}.
 *
 * @param neighbor  a class belonging to the same package that the loaded
 *                  class belogns to.
 * @param bcode     the bytecode.
 * @since 3.24
 */
public static Class<?> toClass(Class<?> neighbor, byte[] bcode)
    throws CannotCompileException
{
    try {
        DefineClassHelper.class.getModule().addReads(neighbor.getModule());
        Lookup lookup = MethodHandles.lookup();
        Lookup prvlookup = MethodHandles.privateLookupIn(neighbor, lookup);
        return prvlookup.defineClass(bcode);
    } catch (IllegalAccessException | IllegalArgumentException e) {
        throw new CannotCompileException(e.getMessage() + ": " + neighbor.getName()
                                         + " has no permission to define the class");
    }
}
 
Example 8
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public WhiteBox() throws ReflectiveOperationException {
    Class<?> qClass = LinkedTransferQueue.class;
    Class<?> nodeClass = Class.forName(qClass.getName() + "$Node");
    MethodHandles.Lookup lookup
        = MethodHandles.privateLookupIn(qClass, MethodHandles.lookup());
    HEAD = lookup.findVarHandle(qClass, "head", nodeClass);
    TAIL = lookup.findVarHandle(qClass, "tail", nodeClass);
    NEXT = lookup.findVarHandle(nodeClass, "next", nodeClass);
    ITEM = lookup.findVarHandle(nodeClass, "item", Object.class);
    SWEEP_THRESHOLD = (int)
        lookup.findStaticVarHandle(qClass, "SWEEP_THRESHOLD", int.class)
        .get();
}
 
Example 9
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
WhiteBox() throws ReflectiveOperationException {
    Class<?> qClass = ConcurrentLinkedQueue.class;
    Class<?> nodeClass = Class.forName(qClass.getName() + "$Node");
    MethodHandles.Lookup lookup
        = MethodHandles.privateLookupIn(qClass, MethodHandles.lookup());
    HEAD = lookup.findVarHandle(qClass, "head", nodeClass);
    TAIL = lookup.findVarHandle(qClass, "tail", nodeClass);
    NEXT = lookup.findVarHandle(nodeClass, "next", nodeClass);
    ITEM = lookup.findVarHandle(nodeClass, "item", Object.class);
}
 
Example 10
Source File: TryAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void privateLookupPublicClassApplicationModule() throws Exception {
    Class<?> clazz = Class.forName("p.Type");
    MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
}
 
Example 11
Source File: TryAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void privateLookupNonPublicClassNonExportedPackage() throws Exception {
    Class<?> clazz = Class.forName("sun.nio.ch.SocketChannelImpl");
    MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
}
 
Example 12
Source File: TryAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void privateLookupPublicClassNonExportedPackage() throws Exception {
    Class<?> clazz = Class.forName("sun.security.x509.X500Name");
    MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
}
 
Example 13
Source File: TryAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void privateLookupNonPublicClassExportedPackage() throws Exception {
    Class<?> clazz = Class.forName("java.lang.WeakPairMap");
    MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
}
 
Example 14
Source File: TryAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void privateLookupPublicClassExportedPackage() throws Exception {
    MethodHandles.privateLookupIn(String.class, MethodHandles.lookup());
}
 
Example 15
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {NullPointerException.class})
public void testNullCaller() throws Exception {
    MethodHandles.privateLookupIn(getClass(), null);
}
 
Example 16
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {NullPointerException.class})
public void testNullTargetClass() throws Exception {
    MethodHandles.privateLookupIn(null, MethodHandles.lookup());
}
 
Example 17
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {IllegalArgumentException.class})
public void testPrimitiveArrayClassAsTargetClass() throws Exception {
    MethodHandles.privateLookupIn(int[].class, MethodHandles.lookup());
}
 
Example 18
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {IllegalArgumentException.class})
public void testArrayClassAsTargetClass() throws Exception {
    MethodHandles.privateLookupIn(PrivateLookupInTests[].class, MethodHandles.lookup());
}
 
Example 19
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {IllegalArgumentException.class})
public void testPrimitiveClassAsTargetClass() throws Exception {
    MethodHandles.privateLookupIn(int.class, MethodHandles.lookup());
}
 
Example 20
Source File: PrivateLookupInTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = {IllegalAccessException.class})
public void testPublicLookupSameModule() throws Exception {
    Lookup caller = MethodHandles.publicLookup();
    Lookup lookup = MethodHandles.privateLookupIn(publicType, caller);
}