15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 /*
26 * @test
27 * @summary test MethodHandle/VarHandle on inline types
28 * @compile -XDallowWithFieldOperator Point.java Line.java MutablePath.java MixedValues.java
29 * @run testng/othervm -XX:+EnableValhalla MethodHandleTest
30 */
31
32 import java.lang.invoke.*;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Modifier;
35 import java.util.*;
36
37 import org.testng.annotations.BeforeTest;
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
40 import static org.testng.Assert.*;
41
42 public class MethodHandleTest {
43 private static final Point P = Point.makePoint(10, 20);
44 private static final Line L = Line.makeLine(10, 20, 30, 40);
45 private static final MutablePath PATH = MutablePath.makePath(10, 20, 30, 40);
46
47 @Test
48 public static void testPointClass() throws Throwable {
49 MethodHandleTest test = new MethodHandleTest("Point", P, "x", "y");
50 test.run();
51 }
52
53 @Test
54 public static void testLineClass() throws Throwable {
55 MethodHandleTest test = new MethodHandleTest("Line", L, "p1", "p2");
103 testArray(Point[].class, P);
104 testArray(Line[].class, L);
105 testArray(MutablePath[].class, PATH);
106 }
107
108 static void testArray(Class<?> c, Object o) throws Throwable {
109 MethodHandle setter = MethodHandles.arrayElementSetter(c);
110 MethodHandle getter = MethodHandles.arrayElementGetter(c);
111 MethodHandle ctor = MethodHandles.arrayConstructor(c);
112 int size = 5;
113 Object[] array = (Object[])ctor.invoke(size);
114 for (int i=0; i < size; i++) {
115 setter.invoke(array, i, o);
116 }
117 for (int i=0; i < size; i++) {
118 Object v = (Object)getter.invoke(array, i);
119 assertEquals(v, o);
120 }
121
122 Class<?> elementType = c.getComponentType();
123 if (elementType.isValue()) {
124 assertTrue(elementType == elementType.asValueType());
125 }
126 // set an array element to null
127 try {
128 Object v = (Object)setter.invoke(array, 0, null);
129 assertFalse(elementType.isValue(), "should fail to set an inline class array element to null");
130 } catch (NullPointerException e) {
131 assertTrue(elementType.isValue(), "should only fail to set an inline class array element to null");
132 }
133 }
134
135 @Test
136 public static void testNullableArray() throws Throwable {
137 Class<?> arrayClass = (new Point?[0]).getClass();
138 Class<?> elementType = arrayClass.getComponentType();
139 assertTrue(elementType == Point.class.asBoxType());
140
141 MethodHandle setter = MethodHandles.arrayElementSetter(arrayClass);
142 MethodHandle getter = MethodHandles.arrayElementGetter(arrayClass);
143 MethodHandle ctor = MethodHandles.arrayConstructor(arrayClass);
144 Object[] array = (Object[]) ctor.invoke(2);
145 setter.invoke(array, 0, P);
146 setter.invoke(array, 1, null);
147 assertEquals((Point)getter.invoke(array, 0), P);
148 assertNull((Object)getter.invoke(array, 1));
149 }
150
151 private final Class<?> c;
152 private final Object o;
153 private final List<String> names;
154 public MethodHandleTest(String cn, Object o, String... fields) throws Exception {
155 this.c = Class.forName(cn);
156 this.o = o;
157 this.names = List.of(fields);
158 }
159
160 public void run() throws Throwable {
161 for (String name : names) {
162 Field f = c.getDeclaredField(name);
163 unreflectField(f);
164 findGetter(f);
165 varHandle(f);
166 if (c.isValue())
167 ensureImmutable(f);
168 else
169 ensureNullable(f);
170 }
171 }
172
173 public List<String> names() {
174 return names;
175 }
176
177 void findGetter(Field f) throws Throwable {
178 MethodHandle mh = MethodHandles.lookup().findGetter(c, f.getName(), f.getType());
179 Object value = mh.invoke(o);
180 }
181
182 void varHandle(Field f) throws Throwable {
183 VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
184 Object value = vh.get(o);
185 }
186
187 void unreflectField(Field f) throws Throwable {
188 MethodHandle mh = MethodHandles.lookup().unreflectGetter(f);
189 Object value = mh.invoke(o);
190 }
191
192 /*
193 * Test setting a field of an inline type to a new value.
194 * The field must be flattenable but may or may not be flattened.
195 */
196 void setValueField(String name, Object obj, Object value) throws Throwable {
197 Field f = c.getDeclaredField(name);
198 boolean isStatic = Modifier.isStatic(f.getModifiers());
199 assertTrue(f.getType().isValue());
200 assertTrue((isStatic && obj == null) || (!isStatic && obj != null));
201 Object v = f.get(obj);
202
203 // Field::set
204 try {
205 f.set(obj, value);
206 assertEquals(f.get(obj), value);
207 } finally {
208 f.set(obj, v);
209 }
210
211
212 if (isStatic) {
213 setStaticField(f, value);
214 } else {
215 setInstanceField(f, obj, value);
216 }
217 }
218
219 private void setInstanceField(Field f, Object obj, Object value) throws Throwable {
245 assertEquals(f.get(null), value);
246 } finally {
247 f.set(null, v);
248 }
249 // VarHandle::set
250 try {
251 VarHandle vh = MethodHandles.lookup().findStaticVarHandle(c, f.getName(), f.getType());
252 vh.set(f.getType().cast(value));
253 assertEquals(f.get(null), value);
254 } finally {
255 f.set(null, v);
256 }
257 }
258
259 /*
260 * Test setting the given field to null via reflection, method handle
261 * and var handle.
262 */
263 void ensureNullable(Field f) throws Throwable {
264 assertFalse(Modifier.isStatic(f.getModifiers()));
265 // flattenable implies non-nullable
266 boolean canBeNull = !isFlattenable(f);
267 // test reflection
268 try {
269 f.set(o, null);
270 assertTrue(canBeNull, f + " cannot be set to null");
271 } catch (NullPointerException e) {
272 assertFalse(canBeNull, f + " should allow be set to null");
273 }
274 // test method handle, i.e. putfield bytecode behavior
275 try {
276 MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
277 mh.invoke(o, null);
278 assertTrue(canBeNull, f + " cannot be set to null");
279 } catch (NullPointerException e) {
280 assertFalse(canBeNull, f + " should allow be set to null");
281 }
282 // test var handle
283 try {
284 VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
285 vh.set(o, null);
286 assertTrue(canBeNull, f + " cannot be set to null");
298 throw new RuntimeException(f + " should be immutable");
299 } catch (IllegalAccessException e) {}
300
301 // test method handle, i.e. putfield bytecode behavior
302 try {
303 MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
304 mh.invoke(o, v);
305 throw new RuntimeException(f + " should be immutable");
306 } catch (IllegalAccessException e) { }
307 // test var handle
308 try {
309 VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
310 vh.set(o, v);
311 throw new RuntimeException(f + " should be immutable");
312 } catch (UnsupportedOperationException e) {}
313 }
314
315 boolean isFlattened(Field f) {
316 return (f.getModifiers() & 0x00008000) == 0x00008000;
317 }
318
319 boolean isFlattenable(Field f) {
320 return (f.getModifiers() & 0x00000100) == 0x00000100;
321 }
322
323 }
|
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 /*
26 * @test
27 * @summary test MethodHandle/VarHandle on inline types
28 * @compile -XDallowWithFieldOperator Point.java Line.java MutablePath.java MixedValues.java
29 * @run testng/othervm -XX:+EnableValhalla MethodHandleTest
30 */
31
32 import java.lang.invoke.*;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Modifier;
35 import java.util.List;
36
37 import org.testng.annotations.BeforeTest;
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
40 import static org.testng.Assert.*;
41
42 public class MethodHandleTest {
43 private static final Point P = Point.makePoint(10, 20);
44 private static final Line L = Line.makeLine(10, 20, 30, 40);
45 private static final MutablePath PATH = MutablePath.makePath(10, 20, 30, 40);
46
47 @Test
48 public static void testPointClass() throws Throwable {
49 MethodHandleTest test = new MethodHandleTest("Point", P, "x", "y");
50 test.run();
51 }
52
53 @Test
54 public static void testLineClass() throws Throwable {
55 MethodHandleTest test = new MethodHandleTest("Line", L, "p1", "p2");
103 testArray(Point[].class, P);
104 testArray(Line[].class, L);
105 testArray(MutablePath[].class, PATH);
106 }
107
108 static void testArray(Class<?> c, Object o) throws Throwable {
109 MethodHandle setter = MethodHandles.arrayElementSetter(c);
110 MethodHandle getter = MethodHandles.arrayElementGetter(c);
111 MethodHandle ctor = MethodHandles.arrayConstructor(c);
112 int size = 5;
113 Object[] array = (Object[])ctor.invoke(size);
114 for (int i=0; i < size; i++) {
115 setter.invoke(array, i, o);
116 }
117 for (int i=0; i < size; i++) {
118 Object v = (Object)getter.invoke(array, i);
119 assertEquals(v, o);
120 }
121
122 Class<?> elementType = c.getComponentType();
123 if (elementType.isInlineClass()) {
124 assertTrue(elementType == elementType.asPrimaryType());
125 }
126 // set an array element to null
127 try {
128 Object v = (Object)setter.invoke(array, 0, null);
129 assertFalse(elementType.isInlineClass(), "should fail to set an inline class array element to null");
130 } catch (NullPointerException e) {
131 assertTrue(elementType.isInlineClass(), "should only fail to set an inline class array element to null");
132 }
133 }
134
135 @Test
136 public static void testNullableArray() throws Throwable {
137 Class<?> arrayClass = (new Point?[0]).getClass();
138 Class<?> elementType = arrayClass.getComponentType();
139 assertTrue(elementType == Point.class.asNullableType(), arrayClass.getComponentType().toString());
140
141 MethodHandle setter = MethodHandles.arrayElementSetter(arrayClass);
142 MethodHandle getter = MethodHandles.arrayElementGetter(arrayClass);
143 MethodHandle ctor = MethodHandles.arrayConstructor(arrayClass);
144 Object[] array = (Object[]) ctor.invoke(2);
145 setter.invoke(array, 0, P);
146 setter.invoke(array, 1, null);
147 assertEquals((Point)getter.invoke(array, 0), P);
148 assertNull((Object)getter.invoke(array, 1));
149 }
150
151 private final Class<?> c;
152 private final Object o;
153 private final List<String> names;
154 public MethodHandleTest(String cn, Object o, String... fields) throws Exception {
155 this.c = Class.forName(cn);
156 this.o = o;
157 this.names = List.of(fields);
158 }
159
160 public void run() throws Throwable {
161 for (String name : names) {
162 Field f = c.getDeclaredField(name);
163 unreflectField(f);
164 findGetter(f);
165 varHandle(f);
166 if (c.isInlineClass())
167 ensureImmutable(f);
168 else
169 ensureNullable(f);
170 }
171 }
172
173 public List<String> names() {
174 return names;
175 }
176
177 void findGetter(Field f) throws Throwable {
178 MethodHandle mh = MethodHandles.lookup().findGetter(c, f.getName(), f.getType());
179 Object value = mh.invoke(o);
180 }
181
182 void varHandle(Field f) throws Throwable {
183 VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
184 Object value = vh.get(o);
185 }
186
187 void unreflectField(Field f) throws Throwable {
188 MethodHandle mh = MethodHandles.lookup().unreflectGetter(f);
189 Object value = mh.invoke(o);
190 }
191
192 /*
193 * Test setting a field of an inline type to a new value.
194 * The field must be flattenable but may or may not be flattened.
195 */
196 void setValueField(String name, Object obj, Object value) throws Throwable {
197 Field f = c.getDeclaredField(name);
198 boolean isStatic = Modifier.isStatic(f.getModifiers());
199 assertTrue(f.getType().isInlineClass());
200 assertTrue((isStatic && obj == null) || (!isStatic && obj != null));
201 Object v = f.get(obj);
202
203 // Field::set
204 try {
205 f.set(obj, value);
206 assertEquals(f.get(obj), value);
207 } finally {
208 f.set(obj, v);
209 }
210
211
212 if (isStatic) {
213 setStaticField(f, value);
214 } else {
215 setInstanceField(f, obj, value);
216 }
217 }
218
219 private void setInstanceField(Field f, Object obj, Object value) throws Throwable {
245 assertEquals(f.get(null), value);
246 } finally {
247 f.set(null, v);
248 }
249 // VarHandle::set
250 try {
251 VarHandle vh = MethodHandles.lookup().findStaticVarHandle(c, f.getName(), f.getType());
252 vh.set(f.getType().cast(value));
253 assertEquals(f.get(null), value);
254 } finally {
255 f.set(null, v);
256 }
257 }
258
259 /*
260 * Test setting the given field to null via reflection, method handle
261 * and var handle.
262 */
263 void ensureNullable(Field f) throws Throwable {
264 assertFalse(Modifier.isStatic(f.getModifiers()));
265 boolean canBeNull = f.getType().isNullableType();
266 // test reflection
267 try {
268 f.set(o, null);
269 assertTrue(canBeNull, f + " cannot be set to null");
270 } catch (NullPointerException e) {
271 assertFalse(canBeNull, f + " should allow be set to null");
272 }
273 // test method handle, i.e. putfield bytecode behavior
274 try {
275 MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
276 mh.invoke(o, null);
277 assertTrue(canBeNull, f + " cannot be set to null");
278 } catch (NullPointerException e) {
279 assertFalse(canBeNull, f + " should allow be set to null");
280 }
281 // test var handle
282 try {
283 VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
284 vh.set(o, null);
285 assertTrue(canBeNull, f + " cannot be set to null");
297 throw new RuntimeException(f + " should be immutable");
298 } catch (IllegalAccessException e) {}
299
300 // test method handle, i.e. putfield bytecode behavior
301 try {
302 MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
303 mh.invoke(o, v);
304 throw new RuntimeException(f + " should be immutable");
305 } catch (IllegalAccessException e) { }
306 // test var handle
307 try {
308 VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
309 vh.set(o, v);
310 throw new RuntimeException(f + " should be immutable");
311 } catch (UnsupportedOperationException e) {}
312 }
313
314 boolean isFlattened(Field f) {
315 return (f.getModifiers() & 0x00008000) == 0x00008000;
316 }
317 }
|