< prev index next >

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

Print this page
rev 58565 : 8238358: Implementation of JEP 371: Hidden Classes
Reviewed-by: duke
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
rev 58568 : [mq]: hidden-class-4


  51         try {
  52             byte[] bytes = Files.readAllBytes(cf);
  53             return MethodHandles.lookup().defineHiddenClass(bytes, true).lookupClass();
  54         } catch (IOException e) {
  55             throw new UncheckedIOException(e);
  56         } catch (IllegalAccessException e) {
  57             throw new RuntimeException(e);
  58         }
  59     }
  60 
  61     /*
  62      * Test Lookup::unreflectSetter and Lookup::unreflectVarHandle that
  63      * can write the value of a non-static final field in a normal class
  64      */
  65     @Test
  66     public void testFieldsInNormalClass() throws Throwable {
  67         // despite the name "HiddenClass", this class is loaded by the
  68         // class loader as non-hidden class
  69         Class<?> c = Fields.class;
  70         Fields o = new Fields();
  71         assertFalse(c.isHiddenClass());
  72         readOnlyAccessibleObject(c, "STATIC_FINAL", null, true);
  73         readWriteAccessibleObject(c, "STATIC_NON_FINAL", null, false);
  74         readWriteAccessibleObject(c, "FINAL", o, true);
  75         readWriteAccessibleObject(c, "NON_FINAL", o, false);
  76     }
  77 
  78     /*
  79      * Test Lookup::unreflectSetter and Lookup::unreflectVarHandle that
  80      * has NO write the value of a non-static final field in a hidden class
  81      */
  82     @Test
  83     public void testFieldsInHiddenClass() throws Throwable {
  84         assertTrue(hiddenClass.isHiddenClass());
  85         Object o = hiddenClass.newInstance();
  86         readOnlyAccessibleObject(hiddenClass, "STATIC_FINAL", null, true);
  87         readWriteAccessibleObject(hiddenClass, "STATIC_NON_FINAL", null, false);
  88         readOnlyAccessibleObject(hiddenClass, "FINAL", o, true);
  89         readWriteAccessibleObject(hiddenClass, "NON_FINAL", o, false);
  90     }
  91 
  92     /*
  93      * Verify read-only access via unreflectSetter and unreflectVarHandle
  94      */
  95     private static void readOnlyAccessibleObject(Class<?> c, String name, Object o, boolean isFinal) throws Throwable {
  96         Field f = c.getDeclaredField(name);
  97         int modifier = f.getModifiers();
  98         if (isFinal) {
  99             assertTrue(Modifier.isFinal(modifier));
 100         } else {
 101             assertFalse(Modifier.isFinal(modifier));
 102         }
 103         assertTrue(f.trySetAccessible());
 104 




  51         try {
  52             byte[] bytes = Files.readAllBytes(cf);
  53             return MethodHandles.lookup().defineHiddenClass(bytes, true).lookupClass();
  54         } catch (IOException e) {
  55             throw new UncheckedIOException(e);
  56         } catch (IllegalAccessException e) {
  57             throw new RuntimeException(e);
  58         }
  59     }
  60 
  61     /*
  62      * Test Lookup::unreflectSetter and Lookup::unreflectVarHandle that
  63      * can write the value of a non-static final field in a normal class
  64      */
  65     @Test
  66     public void testFieldsInNormalClass() throws Throwable {
  67         // despite the name "HiddenClass", this class is loaded by the
  68         // class loader as non-hidden class
  69         Class<?> c = Fields.class;
  70         Fields o = new Fields();
  71         assertFalse(c.isHidden());
  72         readOnlyAccessibleObject(c, "STATIC_FINAL", null, true);
  73         readWriteAccessibleObject(c, "STATIC_NON_FINAL", null, false);
  74         readWriteAccessibleObject(c, "FINAL", o, true);
  75         readWriteAccessibleObject(c, "NON_FINAL", o, false);
  76     }
  77 
  78     /*
  79      * Test Lookup::unreflectSetter and Lookup::unreflectVarHandle that
  80      * has NO write the value of a non-static final field in a hidden class
  81      */
  82     @Test
  83     public void testFieldsInHiddenClass() throws Throwable {
  84         assertTrue(hiddenClass.isHidden());
  85         Object o = hiddenClass.newInstance();
  86         readOnlyAccessibleObject(hiddenClass, "STATIC_FINAL", null, true);
  87         readWriteAccessibleObject(hiddenClass, "STATIC_NON_FINAL", null, false);
  88         readOnlyAccessibleObject(hiddenClass, "FINAL", o, true);
  89         readWriteAccessibleObject(hiddenClass, "NON_FINAL", o, false);
  90     }
  91 
  92     /*
  93      * Verify read-only access via unreflectSetter and unreflectVarHandle
  94      */
  95     private static void readOnlyAccessibleObject(Class<?> c, String name, Object o, boolean isFinal) throws Throwable {
  96         Field f = c.getDeclaredField(name);
  97         int modifier = f.getModifiers();
  98         if (isFinal) {
  99             assertTrue(Modifier.isFinal(modifier));
 100         } else {
 101             assertFalse(Modifier.isFinal(modifier));
 102         }
 103         assertTrue(f.trySetAccessible());
 104 


< prev index next >