< prev index next >

test/jdk/valhalla/valuetypes/ValueBootstrapMethods.java

Print this page
rev 55127 : 8223351: [lworld] Primary mirror and nullable mirror for inline type
Reviewed-by: tbd


  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 value bootstrap methods
  28  * @modules java.base/jdk.internal.org.objectweb.asm
  29  * @compile -XDallowWithFieldOperator ValueBootstrapMethods.java
  30  * @run main/othervm -XX:+EnableValhalla -Dvalue.bsm.salt=1 ValueBootstrapMethods
  31  */
  32 
  33 import java.io.IOException;
  34 import java.lang.invoke.CallSite;
  35 import java.lang.invoke.MethodHandles;
  36 import java.lang.invoke.MethodType;
  37 import java.lang.reflect.*;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.util.HashSet;
  42 import java.util.List;
  43 import java.util.Objects;
  44 import java.util.Set;
  45 
  46 import jdk.internal.org.objectweb.asm.Attribute;
  47 import jdk.internal.org.objectweb.asm.ByteVector;
  48 import jdk.internal.org.objectweb.asm.ClassReader;
  49 import jdk.internal.org.objectweb.asm.ClassWriter;
  50 import jdk.internal.org.objectweb.asm.Handle;
  51 import jdk.internal.org.objectweb.asm.Label;
  52 import jdk.internal.org.objectweb.asm.MethodVisitor;
  53 import jdk.internal.org.objectweb.asm.Type;
  54 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  55 
  56 public class ValueBootstrapMethods {
  57     private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
  58 
  59     public static void main(String... args) throws Throwable {
  60         Class<?> test = valueTestClass();
  61         Value value = Value.make(10, 5.03, "foo", "bar", "goo");
  62 
  63         Class<?> valueClass = Value.class.asValueType();
  64         Method hashCode = test.getMethod("hashCode", valueClass);
  65         int hash = (int)hashCode.invoke(null, value);
  66         assertEquals(hash, value.hashCode());
  67 
  68         Method toString = test.getMethod("toString", valueClass);
  69         String s = (String)toString.invoke(null, value);
  70         assertEquals(s, value.toString());
  71 
  72         Method equals = test.getMethod("equals", valueClass, Object.class);
  73         boolean rc = (boolean)equals.invoke(null, value, value);
  74         if (!rc) {
  75             throw new RuntimeException("expected equals");
  76         }
  77     }
  78 
  79     public static final inline class Value {
  80         private final int i;
  81         private final double d;
  82         private final String s;
  83         private final List<String> l;
  84         Value() {
  85             this.i = 0;
  86             this.d = 0;
  87             this.s = "default";
  88             this.l = List.of();
  89         }
  90         public static Value make(int i, double d, String s, String... items) {
  91             Value v = Value.default;
  92             v = __WithField(v.i, i);
  93             v = __WithField(v.d, d);
  94             v = __WithField(v.s, s);
  95             v = __WithField(v.l, List.of(items));
  96             return v;
  97         }
  98 
  99         List<Object> values() {
 100             return List.of(Value.class.asValueType(), i, d, s, l);
 101         }
 102 
 103         public int hashCode() {
 104             return values().hashCode();
 105         }
 106 
 107         public String toString() {
 108             System.out.println(l);
 109             return String.format("[%s i=%s d=%s s=%s l=%s]", Value.class.getName(),
 110                                  i, String.valueOf(d), s, l.toString());
 111         }
 112     }
 113 
 114     /*
 115      * Generate ValueTest class
 116      */
 117     private static Class<?> valueTestClass() throws Exception {
 118         Path path = Paths.get(TEST_CLASSES, "ValueTest.class");
 119         generate(Value.class.asValueType(), "ValueTest", path);
 120         return Class.forName("ValueTest");
 121     }
 122 
 123     private static void assertEquals(Object o1, Object expected) {
 124         if (!Objects.equals(o1, expected)) {
 125             throw new RuntimeException(o1 + " expected: " + expected);
 126         }
 127     }
 128 
 129     static final int CLASSFILE_VERSION = 56;
 130     static void generate(Class<?> c, String className, Path path) throws IOException {
 131         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
 132 
 133         cw.visit(CLASSFILE_VERSION,
 134             ACC_SUPER + ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC,
 135             className,
 136             null,
 137             "java/lang/Object",
 138             null
 139         );




  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 value bootstrap methods
  28  * @modules java.base/jdk.internal.org.objectweb.asm
  29  * @compile -XDallowWithFieldOperator ValueBootstrapMethods.java
  30  * @run main/othervm -XX:+EnableValhalla -Dvalue.bsm.salt=1 ValueBootstrapMethods
  31  */
  32 
  33 import java.io.IOException;
  34 import java.lang.invoke.CallSite;
  35 import java.lang.invoke.MethodHandles;
  36 import java.lang.invoke.MethodType;
  37 import java.lang.reflect.Method;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;

  41 import java.util.List;
  42 import java.util.Objects;

  43 



  44 import jdk.internal.org.objectweb.asm.ClassWriter;
  45 import jdk.internal.org.objectweb.asm.Handle;

  46 import jdk.internal.org.objectweb.asm.MethodVisitor;
  47 import jdk.internal.org.objectweb.asm.Type;
  48 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  49 
  50 public class ValueBootstrapMethods {
  51     private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
  52 
  53     public static void main(String... args) throws Throwable {
  54         Class<?> test = valueTestClass();
  55         Value value = Value.make(10, 5.03, "foo", "bar", "goo");
  56 
  57         Class<?> valueClass = Value.class.asPrimaryType();
  58         Method hashCode = test.getMethod("hashCode", valueClass);
  59         int hash = (int)hashCode.invoke(null, value);
  60         assertEquals(hash, value.hashCode());
  61 
  62         Method toString = test.getMethod("toString", valueClass);
  63         String s = (String)toString.invoke(null, value);
  64         assertEquals(s, value.toString());
  65 
  66         Method equals = test.getMethod("equals", valueClass, Object.class);
  67         boolean rc = (boolean)equals.invoke(null, value, value);
  68         if (!rc) {
  69             throw new RuntimeException("expected equals");
  70         }
  71     }
  72 
  73     public static final inline class Value {
  74         private final int i;
  75         private final double d;
  76         private final String s;
  77         private final List<String> l;
  78         Value() {
  79             this.i = 0;
  80             this.d = 0;
  81             this.s = "default";
  82             this.l = List.of();
  83         }
  84         public static Value make(int i, double d, String s, String... items) {
  85             Value v = Value.default;
  86             v = __WithField(v.i, i);
  87             v = __WithField(v.d, d);
  88             v = __WithField(v.s, s);
  89             v = __WithField(v.l, List.of(items));
  90             return v;
  91         }
  92 
  93         List<Object> values() {
  94             return List.of(Value.class, i, d, s, l);
  95         }
  96 
  97         public int hashCode() {
  98             return values().hashCode();
  99         }
 100 
 101         public String toString() {
 102             System.out.println(l);
 103             return String.format("[%s i=%s d=%s s=%s l=%s]", Value.class.getName(),
 104                                  i, String.valueOf(d), s, l.toString());
 105         }
 106     }
 107 
 108     /*
 109      * Generate ValueTest class
 110      */
 111     private static Class<?> valueTestClass() throws Exception {
 112         Path path = Paths.get(TEST_CLASSES, "ValueTest.class");
 113         generate(Value.class, "ValueTest", path);
 114         return Class.forName("ValueTest");
 115     }
 116 
 117     private static void assertEquals(Object o1, Object expected) {
 118         if (!Objects.equals(o1, expected)) {
 119             throw new RuntimeException(o1 + " expected: " + expected);
 120         }
 121     }
 122 
 123     static final int CLASSFILE_VERSION = 56;
 124     static void generate(Class<?> c, String className, Path path) throws IOException {
 125         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
 126 
 127         cw.visit(CLASSFILE_VERSION,
 128             ACC_SUPER + ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC,
 129             className,
 130             null,
 131             "java/lang/Object",
 132             null
 133         );


< prev index next >