< prev index next >

test/jdk/java/lang/invoke/defineHiddenClass/BasicTest.java

Print this page
rev 58768 : 8238358: Implementation of JEP 371: Hidden Classes
Reviewed-by: alanb, cjplummer, coleenp, dholmes, dlong, forax, jlahoda, psandoz, plevart, vromero
Contributed-by: mandy.chung@oracle.com, lois.foltan@oracle.com, david.holmes@oracle.com, harold.seigel@oracle.com, serguei.spitsyn@oracle.com, alex.buckley@oracle.com, jamsheed.c.m@oracle.com, jan.lahoda@oracle.com, amy.lu@oracle.com
rev 58769 : [mq]: type-descriptor-name


  98         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
  99         assertHiddenClass(hc);
 100         singletonNest(hc);
 101         return hc;
 102     }
 103 
 104     // basic test on a hidden class
 105     @Test
 106     public void hiddenClass() throws Throwable {
 107         HiddenTest t = (HiddenTest)defineHiddenClass("HiddenClass").newInstance();
 108         t.test();
 109 
 110         // sanity check
 111         Class<?> c = t.getClass();
 112         Class<?>[] intfs = c.getInterfaces();
 113         assertTrue(c.isHidden());
 114         assertFalse(c.isPrimitive());
 115         assertTrue(intfs.length == 1);
 116         assertTrue(intfs[0] == HiddenTest.class);
 117         assertTrue(c.getCanonicalName() == null);
 118         assertTrue(c.getName().startsWith("HiddenClass/"));




 119 
 120         // test array of hidden class
 121         testHiddenArray(c);
 122 
 123         // test setAccessible
 124         checkSetAccessible(c, "realTest");
 125         checkSetAccessible(c, "test");
 126     }
 127 
 128     // primitive class is not a hidden class
 129     @Test
 130     public void primitiveClass() {
 131         assertFalse(int.class.isHidden());
 132         assertFalse(String.class.isHidden());
 133     }
 134 
 135     private void testHiddenArray(Class<?> type) throws Exception {
 136         // array of hidden class
 137         Object array = Array.newInstance(type, 2);
 138         Class<?> arrayType = array.getClass();
 139         assertTrue(arrayType.isArray());
 140         assertTrue(Array.getLength(array) == 2);
 141         assertFalse(arrayType.isHidden());
 142         assertTrue(arrayType.getName().startsWith("[LHiddenClass/"), "unexpected name: " + arrayType.getName());




 143 
 144         assertTrue(arrayType.getComponentType().isHidden());
 145         assertTrue(arrayType.getComponentType() == type);
 146         Object t = type.newInstance();
 147         Array.set(array, 0, t);
 148         Object o = Array.get(array, 0);
 149         assertTrue(o == t);
 150     }
 151 
 152     private void checkSetAccessible(Class<?> c, String name, Class<?>... ptypes) throws Exception {
 153         Method m = c.getDeclaredMethod(name, ptypes);
 154         assertTrue(m.trySetAccessible());
 155         m.setAccessible(true);
 156     }
 157 
 158     // Define a hidden class that uses lambda
 159     // This verifies LambdaMetaFactory supports the caller which is a hidden class
 160     @Test
 161     public void testLambda() throws Throwable {
 162         HiddenTest t = (HiddenTest)defineHiddenClass("Lambda").newInstance();




  98         Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
  99         assertHiddenClass(hc);
 100         singletonNest(hc);
 101         return hc;
 102     }
 103 
 104     // basic test on a hidden class
 105     @Test
 106     public void hiddenClass() throws Throwable {
 107         HiddenTest t = (HiddenTest)defineHiddenClass("HiddenClass").newInstance();
 108         t.test();
 109 
 110         // sanity check
 111         Class<?> c = t.getClass();
 112         Class<?>[] intfs = c.getInterfaces();
 113         assertTrue(c.isHidden());
 114         assertFalse(c.isPrimitive());
 115         assertTrue(intfs.length == 1);
 116         assertTrue(intfs[0] == HiddenTest.class);
 117         assertTrue(c.getCanonicalName() == null);
 118 
 119         String hcName = "HiddenClass";
 120         String hcSuffix = "0x[0-9a-f]+";
 121         assertTrue(c.getName().matches(hcName + "/" + hcSuffix));
 122         assertTrue(c.descriptorString().matches("L" + hcName + "." + hcSuffix + ";"), c.descriptorString());
 123 
 124         // test array of hidden class
 125         testHiddenArray(c);
 126 
 127         // test setAccessible
 128         checkSetAccessible(c, "realTest");
 129         checkSetAccessible(c, "test");
 130     }
 131 
 132     // primitive class is not a hidden class
 133     @Test
 134     public void primitiveClass() {
 135         assertFalse(int.class.isHidden());
 136         assertFalse(String.class.isHidden());
 137     }
 138 
 139     private void testHiddenArray(Class<?> type) throws Exception {
 140         // array of hidden class
 141         Object array = Array.newInstance(type, 2);
 142         Class<?> arrayType = array.getClass();
 143         assertTrue(arrayType.isArray());
 144         assertTrue(Array.getLength(array) == 2);
 145         assertFalse(arrayType.isHidden());
 146 
 147         String hcName = "HiddenClass";
 148         String hcSuffix = "0x[0-9a-f]+";
 149         assertTrue(arrayType.getName().matches("\\[" + "L" + hcName + "/" + hcSuffix + ";"));
 150         assertTrue(arrayType.descriptorString().matches("\\[" + "L" + hcName + "." + hcSuffix + ";"));
 151 
 152         assertTrue(arrayType.getComponentType().isHidden());
 153         assertTrue(arrayType.getComponentType() == type);
 154         Object t = type.newInstance();
 155         Array.set(array, 0, t);
 156         Object o = Array.get(array, 0);
 157         assertTrue(o == t);
 158     }
 159 
 160     private void checkSetAccessible(Class<?> c, String name, Class<?>... ptypes) throws Exception {
 161         Method m = c.getDeclaredMethod(name, ptypes);
 162         assertTrue(m.trySetAccessible());
 163         m.setAccessible(true);
 164     }
 165 
 166     // Define a hidden class that uses lambda
 167     // This verifies LambdaMetaFactory supports the caller which is a hidden class
 168     @Test
 169     public void testLambda() throws Throwable {
 170         HiddenTest t = (HiddenTest)defineHiddenClass("Lambda").newInstance();


< prev index next >