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