< prev index next >

test/jdk/valhalla/valuetypes/MethodHandleTest.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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 value types
  28  * @build Point Line MutablePath
  29  * @compile -XDallowFlattenabilityModifiers MethodHandleTest.java
  30  * @run testng/othervm -XX:+EnableValhalla MethodHandleTest
  31  */
  32 
  33 import java.lang.invoke.*;
  34 import java.lang.reflect.Field;
  35 import java.lang.reflect.Modifier;
  36 import java.util.*;
  37 import java.util.stream.Stream;
  38 
  39 import org.testng.annotations.BeforeTest;
  40 import org.testng.annotations.DataProvider;
  41 import org.testng.annotations.Test;
  42 import static org.testng.Assert.*;
  43 
  44 public class MethodHandleTest {
  45     private static final Point P = Point.makePoint(10, 20);
  46     private static final Line L = Line.makeLine(10, 20, 30, 40);
  47     private static final MutablePath PATH = MutablePath.makePath(10, 20, 30, 40);
  48 
  49     @Test
  50     public static void testPointClass() throws Throwable  {
  51         MethodHandleTest test = new MethodHandleTest("Point", P, "x", "y");
  52         test.run();
  53     }
  54 
  55     @Test
  56     public static void testLineClass() throws Throwable {
  57         MethodHandleTest test = new MethodHandleTest("Line", L, "p1", "p2");


  68         Point p = Point.makePoint(100, 200);
  69         test.setValueField("p1", path, p);
  70         test.setValueField("p2", path, p);
  71     }
  72 
  73     @Test
  74     public static void testValueFields() throws Throwable {
  75         MutablePath path = MutablePath.makePath(1, 2, 3, 4);
  76         // p1 and p2 are a non-final field of value type in a reference
  77         MethodHandleTest test1 = new MethodHandleTest("Point", path.p1, "x", "y");
  78         test1.run();
  79 
  80         MethodHandleTest test2 = new MethodHandleTest("Point", path.p2, "x", "y");
  81         test2.run();
  82     }
  83 
  84     @Test
  85     public static void testMixedValues() throws Throwable {
  86         MixedValues mv = new MixedValues(P, L, PATH, "mixed", "types");
  87         MethodHandleTest test =
  88             new MethodHandleTest("MethodHandleTest$MixedValues", mv, "p", "l", "mutablePath", "list", "nfp");
  89         test.run();
  90 
  91         Point p = Point.makePoint(100, 200);
  92         Line l = Line.makeLine(100, 200, 300, 400);
  93         test.setValueField("p", mv, p);
  94         test.setValueField("nfp", mv, p);
  95         test.setValueField("l", mv, l);
  96         test.setValueField("l", mv, l);
  97         test.setValueField("staticPoint", null, p);
  98         test.setValueField("staticLine", null, l);
  99         // remove the following cases when javac and jvm make
 100         // static value fields be flattenable
 101         test.setValueField("staticPoint", null, null);
 102         test.setValueField("staticLine", null, null);
 103     }
 104 
 105     @Test
 106     public static void testArrayElementSetterAndGetter() throws Throwable {
 107         testArray(Point[].class, P);
 108         testArray(Line[].class, L);


 289             MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
 290             mh.invoke(o, v);
 291             throw new RuntimeException(f + " should be immutable");
 292         } catch (IllegalAccessException e) { }
 293         // test var handle
 294         try {
 295             VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
 296             vh.set(o, v);
 297             throw new RuntimeException(f + " should be immutable");
 298         } catch (UnsupportedOperationException e) {}
 299     }
 300 
 301     boolean isFlattened(Field f) {
 302         return (f.getModifiers() & 0x00008000) == 0x00008000;
 303     }
 304 
 305     boolean isFlattenable(Field f) {
 306         return (f.getModifiers() & 0x00000100) == 0x00000100;
 307     }
 308 
 309     static class MixedValues {
 310         static Point staticPoint = Point.makePoint(10, 10);
 311         static Line staticLine;   // LW1 allows null static value field
 312         Point p;
 313         Line l;
 314         MutablePath mutablePath;
 315         List<String> list;
 316         __NotFlattened Point nfp;
 317 
 318         public MixedValues(Point p, Line l, MutablePath path, String... names) {
 319             this.p = p;
 320             this.l = l;
 321             this.mutablePath = path;
 322             this.list = List.of(names);
 323             this.nfp = p;
 324         }
 325     }
 326 
 327 }


   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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 value types
  28  * @compile -XDallowFlattenabilityModifiers 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");


  66         Point p = Point.makePoint(100, 200);
  67         test.setValueField("p1", path, p);
  68         test.setValueField("p2", path, p);
  69     }
  70 
  71     @Test
  72     public static void testValueFields() throws Throwable {
  73         MutablePath path = MutablePath.makePath(1, 2, 3, 4);
  74         // p1 and p2 are a non-final field of value type in a reference
  75         MethodHandleTest test1 = new MethodHandleTest("Point", path.p1, "x", "y");
  76         test1.run();
  77 
  78         MethodHandleTest test2 = new MethodHandleTest("Point", path.p2, "x", "y");
  79         test2.run();
  80     }
  81 
  82     @Test
  83     public static void testMixedValues() throws Throwable {
  84         MixedValues mv = new MixedValues(P, L, PATH, "mixed", "types");
  85         MethodHandleTest test =
  86             new MethodHandleTest("MixedValues", mv, "p", "l", "mutablePath", "list", "nfp");
  87         test.run();
  88 
  89         Point p = Point.makePoint(100, 200);
  90         Line l = Line.makeLine(100, 200, 300, 400);
  91         test.setValueField("p", mv, p);
  92         test.setValueField("nfp", mv, p);
  93         test.setValueField("l", mv, l);
  94         test.setValueField("l", mv, l);
  95         test.setValueField("staticPoint", null, p);
  96         test.setValueField("staticLine", null, l);
  97         // remove the following cases when javac and jvm make
  98         // static value fields be flattenable
  99         test.setValueField("staticPoint", null, null);
 100         test.setValueField("staticLine", null, null);
 101     }
 102 
 103     @Test
 104     public static void testArrayElementSetterAndGetter() throws Throwable {
 105         testArray(Point[].class, P);
 106         testArray(Line[].class, L);


 287             MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
 288             mh.invoke(o, v);
 289             throw new RuntimeException(f + " should be immutable");
 290         } catch (IllegalAccessException e) { }
 291         // test var handle
 292         try {
 293             VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
 294             vh.set(o, v);
 295             throw new RuntimeException(f + " should be immutable");
 296         } catch (UnsupportedOperationException e) {}
 297     }
 298 
 299     boolean isFlattened(Field f) {
 300         return (f.getModifiers() & 0x00008000) == 0x00008000;
 301     }
 302 
 303     boolean isFlattenable(Field f) {
 304         return (f.getModifiers() & 0x00000100) == 0x00000100;
 305     }
 306 


















 307 }
< prev index next >