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

The following examples show how to use java.lang.invoke.MethodHandles.Lookup#defineClass() . 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: DefineClassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Basic test of defineClass to define a class in the same package as test.
 */
@Test
public void testDefineClass() throws Exception {
    final String CLASS_NAME = THIS_PACKAGE + ".Foo";
    Lookup lookup = lookup();
    Class<?> clazz = lookup.defineClass(generateClass(CLASS_NAME));

    // test name
    assertEquals(clazz.getName(), CLASS_NAME);

    // test loader/package/protection-domain
    testSameAbode(clazz, lookup.lookupClass());

    // test discoverable
    testDiscoverable(clazz, lookup);

    // attempt defineClass again
    try {
        lookup.defineClass(generateClass(CLASS_NAME));
        assertTrue(false);
    } catch (LinkageError expected) { }
}
 
Example 2
Source File: DefineClassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test public/package/protected/private access from class defined with defineClass.
 */
@Test
public void testAccess() throws Exception {
    final String THIS_CLASS = this.getClass().getName();
    final String CLASS_NAME = THIS_PACKAGE + ".Runner";
    Lookup lookup = lookup();

    // public
    byte[] classBytes = generateRunner(CLASS_NAME + nextNumber(), THIS_CLASS, "method1");
    testInvoke(lookup.defineClass(classBytes));

    // package
    classBytes = generateRunner(CLASS_NAME + nextNumber(), THIS_CLASS, "method2");
    testInvoke(lookup.defineClass(classBytes));

    // protected (same package)
    classBytes = generateRunner(CLASS_NAME + nextNumber(), THIS_CLASS, "method3");
    testInvoke(lookup.defineClass(classBytes));

    // private
    classBytes = generateRunner(CLASS_NAME + nextNumber(), THIS_CLASS, "method4");
    Class<?> clazz = lookup.defineClass(classBytes);
    Runnable r = (Runnable) clazz.newInstance();
    try {
        r.run();
        assertTrue(false);
    } catch (IllegalAccessError expected) { }
}
 
Example 3
Source File: DefineClassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test defineClass to define classes in a package containing classes with
 * different protection domains.
 */
@Test
public void testTwoProtectionDomains() throws Exception {
    // p.C1 in one exploded directory
    Path dir1 = Files.createTempDirectory("classes");
    Path p = Files.createDirectory(dir1.resolve("p"));
    Files.write(p.resolve("C1.class"), generateClass("p.C1"));
    URL url1 = dir1.toUri().toURL();

    // p.C2 in another exploded directory
    Path dir2 = Files.createTempDirectory("classes");
    p = Files.createDirectory(dir2.resolve("p"));
    Files.write(p.resolve("C2.class"), generateClass("p.C2"));
    URL url2 = dir2.toUri().toURL();

    // load p.C1 and p.C2
    ClassLoader loader = new URLClassLoader(new URL[] { url1, url2 });
    Class<?> target1 = Class.forName("p.C1", false, loader);
    Class<?> target2 = Class.forName("p.C2", false, loader);
    assertTrue(target1.getClassLoader() == loader);
    assertTrue(target1.getClassLoader() == loader);
    assertNotEquals(target1.getProtectionDomain(), target2.getProtectionDomain());

    // protection domain 1
    Lookup lookup1 = privateLookupIn(target1, lookup());

    Class<?> clazz = lookup1.defineClass(generateClass("p.Foo"));
    testSameAbode(clazz, lookup1.lookupClass());
    testDiscoverable(clazz, lookup1);

    // protection domain 2
    Lookup lookup2 = privateLookupIn(target2, lookup());

    clazz = lookup2.defineClass(generateClass("p.Bar"));
    testSameAbode(clazz, lookup2.lookupClass());
    testDiscoverable(clazz, lookup2);
}
 
Example 4
Source File: DefineClassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test defineClass defining a class to the boot loader
 */
@Test
public void testBootLoader() throws Exception {
    Lookup lookup = privateLookupIn(Thread.class, lookup());
    assertTrue(lookup.getClass().getClassLoader() == null);

    Class<?> clazz = lookup.defineClass(generateClass("java.lang.Foo"));
    assertEquals(clazz.getName(), "java.lang.Foo");
    testSameAbode(clazz, Thread.class);
    testDiscoverable(clazz, lookup);
}
 
Example 5
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 6
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 can be obtained by {@code MethodHandles.lookup()} called from
 * somewhere in the package that the loaded class belongs to.
 *
 * @param bcode     the bytecode.
 * @since 3.24
 */
public static Class<?> toClass(Lookup lookup, byte[] bcode)
    throws CannotCompileException
{
    try {
        return lookup.defineClass(bcode);
    } catch (IllegalAccessException | IllegalArgumentException e) {
        throw new CannotCompileException(e.getMessage());
    }
}
 
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}.
 *
 * @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);
    }
}
 
Example 8
Source File: DefineClassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = { IllegalAccessException.class })
public void testNoPackageAccess() throws Exception {
    Lookup lookup = lookup().dropLookupMode(PACKAGE);
    lookup.defineClass(generateClass(THIS_PACKAGE + ".C"));
}