< prev index next >

test/jdk/valhalla/valuetypes/MethodHandleTest.java

Print this page
rev 55119 : 8223387: [lworld] reflection support for LW2 arrays


 102     public static void testArrayElementSetterAndGetter() throws Throwable {
 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         // set an array element to null
 123         Class<?> elementType = c.getComponentType();




 124         try {
 125             // inline array element is flattenable
 126             Object v = (Object)setter.invoke(array, 0, null);
 127             assertFalse(elementType.isValue(), "should fail to set an inline class array element to null");
 128         } catch (NullPointerException e) {
 129             assertTrue(elementType.isValue(), "should only fail to set an inline class array element to null");
 130         }
















 131     }
 132 
 133     private final Class<?> c;
 134     private final Object o;
 135     private final List<String> names;
 136     public MethodHandleTest(String cn, Object o, String... fields) throws Exception {
 137         this.c = Class.forName(cn);
 138         this.o = o;
 139         this.names = List.of(fields);
 140     }
 141 
 142     public void run() throws Throwable {
 143         for (String name : names) {
 144             Field f = c.getDeclaredField(name);
 145             unreflectField(f);
 146             findGetter(f);
 147             varHandle(f);
 148             if (c.isValue())
 149                 ensureImmutable(f);
 150             else




 102     public static void testArrayElementSetterAndGetter() throws Throwable {
 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


< prev index next >