jdk/test/java/lang/invoke/DefineClassTest.java
changeset 45004 ea3137042a61
parent 44359 c6761862ca0b
child 45833 882f4885ec80
equal deleted inserted replaced
44789:73fd39e0702e 45004:ea3137042a61
    73      * Basic test of defineClass to define a class in the same package as test.
    73      * Basic test of defineClass to define a class in the same package as test.
    74      */
    74      */
    75     @Test
    75     @Test
    76     public void testDefineClass() throws Exception {
    76     public void testDefineClass() throws Exception {
    77         final String CLASS_NAME = THIS_PACKAGE + ".Foo";
    77         final String CLASS_NAME = THIS_PACKAGE + ".Foo";
    78         Lookup lookup = lookup().dropLookupMode(PRIVATE);
    78         Lookup lookup = lookup();
    79         Class<?> clazz = lookup.defineClass(generateClass(CLASS_NAME));
    79         Class<?> clazz = lookup.defineClass(generateClass(CLASS_NAME));
    80 
    80 
    81         // test name
    81         // test name
    82         assertEquals(clazz.getName(), CLASS_NAME);
    82         assertEquals(clazz.getName(), CLASS_NAME);
    83 
    83 
    99      */
    99      */
   100     @Test
   100     @Test
   101     public void testAccess() throws Exception {
   101     public void testAccess() throws Exception {
   102         final String THIS_CLASS = this.getClass().getName();
   102         final String THIS_CLASS = this.getClass().getName();
   103         final String CLASS_NAME = THIS_PACKAGE + ".Runner";
   103         final String CLASS_NAME = THIS_PACKAGE + ".Runner";
   104         Lookup lookup = lookup().dropLookupMode(PRIVATE);
   104         Lookup lookup = lookup();
   105 
   105 
   106         // public
   106         // public
   107         byte[] classBytes = generateRunner(CLASS_NAME + nextNumber(), THIS_CLASS, "method1");
   107         byte[] classBytes = generateRunner(CLASS_NAME + nextNumber(), THIS_CLASS, "method1");
   108         testInvoke(lookup.defineClass(classBytes));
   108         testInvoke(lookup.defineClass(classBytes));
   109 
   109 
   142     public void testInitializerNotRun() throws Exception {
   142     public void testInitializerNotRun() throws Exception {
   143         final String THIS_CLASS = this.getClass().getName();
   143         final String THIS_CLASS = this.getClass().getName();
   144         final String CLASS_NAME = THIS_PACKAGE + ".ClassWithClinit";
   144         final String CLASS_NAME = THIS_PACKAGE + ".ClassWithClinit";
   145 
   145 
   146         byte[] classBytes = generateClassWithInitializer(CLASS_NAME, THIS_CLASS, "fail");
   146         byte[] classBytes = generateClassWithInitializer(CLASS_NAME, THIS_CLASS, "fail");
   147         Lookup lookup = lookup().dropLookupMode(PRIVATE);
   147         Class<?> clazz = lookup().defineClass(classBytes);
   148 
   148 
   149         Class<?> clazz = lookup.defineClass(classBytes);
       
   150         // trigger initializer to run
   149         // trigger initializer to run
   151         try {
   150         try {
   152             clazz.newInstance();
   151             clazz.newInstance();
   153             assertTrue(false);
   152             assertTrue(false);
   154         } catch (ExceptionInInitializerError e) {
   153         } catch (ExceptionInInitializerError e) {
   184         assertTrue(target1.getClassLoader() == loader);
   183         assertTrue(target1.getClassLoader() == loader);
   185         assertTrue(target1.getClassLoader() == loader);
   184         assertTrue(target1.getClassLoader() == loader);
   186         assertNotEquals(target1.getProtectionDomain(), target2.getProtectionDomain());
   185         assertNotEquals(target1.getProtectionDomain(), target2.getProtectionDomain());
   187 
   186 
   188         // protection domain 1
   187         // protection domain 1
   189         Lookup lookup1 = privateLookupIn(target1, lookup()).dropLookupMode(PRIVATE);
   188         Lookup lookup1 = privateLookupIn(target1, lookup());
   190 
   189 
   191         Class<?> clazz = lookup1.defineClass(generateClass("p.Foo"));
   190         Class<?> clazz = lookup1.defineClass(generateClass("p.Foo"));
   192         testSameAbode(clazz, lookup1.lookupClass());
   191         testSameAbode(clazz, lookup1.lookupClass());
   193         testDiscoverable(clazz, lookup1);
   192         testDiscoverable(clazz, lookup1);
   194 
   193 
   195         // protection domain 2
   194         // protection domain 2
   196         Lookup lookup2 = privateLookupIn(target2, lookup()).dropLookupMode(PRIVATE);
   195         Lookup lookup2 = privateLookupIn(target2, lookup());
   197 
   196 
   198         clazz = lookup2.defineClass(generateClass("p.Bar"));
   197         clazz = lookup2.defineClass(generateClass("p.Bar"));
   199         testSameAbode(clazz, lookup2.lookupClass());
   198         testSameAbode(clazz, lookup2.lookupClass());
   200         testDiscoverable(clazz, lookup2);
   199         testDiscoverable(clazz, lookup2);
   201     }
   200     }
   203     /**
   202     /**
   204      * Test defineClass defining a class to the boot loader
   203      * Test defineClass defining a class to the boot loader
   205      */
   204      */
   206     @Test
   205     @Test
   207     public void testBootLoader() throws Exception {
   206     public void testBootLoader() throws Exception {
   208         Lookup lookup = privateLookupIn(Thread.class, lookup()).dropLookupMode(PRIVATE);
   207         Lookup lookup = privateLookupIn(Thread.class, lookup());
   209         assertTrue(lookup.getClass().getClassLoader() == null);
   208         assertTrue(lookup.getClass().getClassLoader() == null);
   210 
   209 
   211         Class<?> clazz = lookup.defineClass(generateClass("java.lang.Foo"));
   210         Class<?> clazz = lookup.defineClass(generateClass("java.lang.Foo"));
   212         assertEquals(clazz.getName(), "java.lang.Foo");
   211         assertEquals(clazz.getName(), "java.lang.Foo");
   213         testSameAbode(clazz, Thread.class);
   212         testSameAbode(clazz, Thread.class);
   214         testDiscoverable(clazz, lookup);
   213         testDiscoverable(clazz, lookup);
   215     }
   214     }
   216 
   215 
   217     @Test(expectedExceptions = { IllegalArgumentException.class })
   216     @Test(expectedExceptions = { IllegalArgumentException.class })
   218     public void testWrongPackage() throws Exception {
   217     public void testWrongPackage() throws Exception {
   219         Lookup lookup = lookup().dropLookupMode(PRIVATE);
   218         lookup().defineClass(generateClass("other.C"));
   220         lookup.defineClass(generateClass("other.C"));
       
   221     }
   219     }
   222 
   220 
   223     @Test(expectedExceptions = { IllegalAccessException.class })
   221     @Test(expectedExceptions = { IllegalAccessException.class })
   224     public void testNoPackageAccess() throws Exception {
   222     public void testNoPackageAccess() throws Exception {
   225         Lookup lookup = lookup().dropLookupMode(PACKAGE);
   223         Lookup lookup = lookup().dropLookupMode(PACKAGE);
   226         lookup.defineClass(generateClass(THIS_PACKAGE + ".C"));
   224         lookup.defineClass(generateClass(THIS_PACKAGE + ".C"));
   227     }
   225     }
   228 
   226 
   229     @Test(expectedExceptions = { UnsupportedOperationException.class })
       
   230     public void testHasPrivateAccess() throws Exception {
       
   231         Lookup lookup = lookup();
       
   232         assertTrue(lookup.hasPrivateAccess());
       
   233         lookup.defineClass(generateClass(THIS_PACKAGE + ".C"));
       
   234     }
       
   235 
       
   236     @Test(expectedExceptions = { ClassFormatError.class })
   227     @Test(expectedExceptions = { ClassFormatError.class })
   237     public void testTruncatedClassFile() throws Exception {
   228     public void testTruncatedClassFile() throws Exception {
   238         Lookup lookup = lookup().dropLookupMode(PRIVATE);
   229         lookup().defineClass(new byte[0]);
   239         lookup.defineClass(new byte[0]);
       
   240     }
   230     }
   241 
   231 
   242     @Test(expectedExceptions = { NullPointerException.class })
   232     @Test(expectedExceptions = { NullPointerException.class })
   243     public void testNull() throws Exception {
   233     public void testNull() throws Exception {
   244         Lookup lookup = lookup().dropLookupMode(PRIVATE);
   234         lookup().defineClass(null);
   245         lookup.defineClass(null);
       
   246     }
   235     }
   247 
   236 
   248     /**
   237     /**
   249      * Generates a class file with the given class name
   238      * Generates a class file with the given class name
   250      */
   239      */