當前位置: 首頁>>代碼示例>>Java>>正文


Java Lookup.defineClass方法代碼示例

本文整理匯總了Java中java.lang.invoke.MethodHandles.Lookup.defineClass方法的典型用法代碼示例。如果您正苦於以下問題:Java Lookup.defineClass方法的具體用法?Java Lookup.defineClass怎麽用?Java Lookup.defineClass使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.invoke.MethodHandles.Lookup的用法示例。


在下文中一共展示了Lookup.defineClass方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testDefineClass

import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
/**
 * 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) { }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:DefineClassTest.java

示例2: testAccess

import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
/**
 * 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) { }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:31,代碼來源:DefineClassTest.java

示例3: testTwoProtectionDomains

import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
/**
 * Test defineClass to define classes in a package containing classes with
 * different protection domains.
 */
@Test
public void testTwoProtectionDomains() throws Exception {
    Path here = Paths.get("");

    // p.C1 in one exploded directory
    Path dir1 = Files.createTempDirectory(here, "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(here, "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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:43,代碼來源:DefineClassTest.java

示例4: testBootLoader

import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
/**
 * 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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:DefineClassTest.java

示例5: testNoPackageAccess

import java.lang.invoke.MethodHandles.Lookup; //導入方法依賴的package包/類
@Test(expectedExceptions = { IllegalAccessException.class })
public void testNoPackageAccess() throws Exception {
    Lookup lookup = lookup().dropLookupMode(PACKAGE);
    lookup.defineClass(generateClass(THIS_PACKAGE + ".C"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:6,代碼來源:DefineClassTest.java


注:本文中的java.lang.invoke.MethodHandles.Lookup.defineClass方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。