hotspot/test/compiler/jvmci/compilerToVM/ConstantPoolTestsHelper.java
changeset 39421 a9652c919db8
parent 37301 a936b4e01afb
child 40059 c2304140ed64
--- a/hotspot/test/compiler/jvmci/compilerToVM/ConstantPoolTestsHelper.java	Wed Jun 15 13:47:17 2016 +0200
+++ b/hotspot/test/compiler/jvmci/compilerToVM/ConstantPoolTestsHelper.java	Wed Jun 15 14:27:58 2016 +0200
@@ -31,6 +31,10 @@
 import compiler.jvmci.compilerToVM.ConstantPoolTestCase.TestedCPEntry;
 import java.util.HashMap;
 import java.util.Map;
+import jdk.vm.ci.meta.MetaAccessProvider;
+import jdk.vm.ci.meta.ResolvedJavaMethod;
+import jdk.vm.ci.meta.ResolvedJavaType;
+import jdk.vm.ci.runtime.JVMCI;
 import jdk.internal.misc.SharedSecrets;
 import jdk.internal.org.objectweb.asm.Opcodes;
 import sun.hotspot.WhiteBox;
@@ -44,6 +48,7 @@
 public class ConstantPoolTestsHelper {
 
     public static final int NO_CP_CACHE_PRESENT = Integer.MAX_VALUE;
+    private static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
 
     public enum DummyClasses {
         DUMMY_CLASS(MultipleImplementer2.class, CP_MAP_FOR_CLASS),
@@ -76,6 +81,45 @@
         }
     }
 
+    /**
+     * Obtain a resolved Java method declared by a given type.
+     *
+     * @param type the declaring type
+     * @param the method's name
+     *
+     * Currently, the lookup is based only on the method's name
+     * but not on the method's signature (i.e., the first method
+     * with a matching name declared on {@code type} is returned).
+     */
+    private static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) {
+        if (methodName.equals("<clinit>")) {
+            return type.getClassInitializer();
+        }
+
+        if (methodName.equals("<init>")) {
+            ResolvedJavaMethod[] initializers = type.getDeclaredConstructors();
+            if (initializers.length >= 0) {
+                return initializers[0];
+            } else {
+                throw new IllegalArgumentException();
+            }
+        }
+
+        for (ResolvedJavaMethod method : type.getDeclaredMethods()) {
+            if (method.getName().equals(methodName)) {
+                return method;
+            }
+        }
+
+        throw new IllegalArgumentException();
+    }
+
+    private static ResolvedJavaType getType(Class<?> clazz) {
+        ResolvedJavaType type = metaAccess.lookupJavaType(clazz);
+        type.initialize();
+        return type;
+    }
+
     private static final Map<ConstantTypes, TestedCPEntry[]> CP_MAP_FOR_CLASS = new HashMap<>();
     static {
         CP_MAP_FOR_CLASS.put(CONSTANT_CLASS,
@@ -141,6 +185,7 @@
                     new TestedCPEntry("compiler/jvmci/common/testcases/MultipleImplementer2",
                                       "objectField",
                                       "Ljava/lang/Object;",
+                                      new ResolvedJavaMethod[] { getMethod(getType(MultipleImplementer2.class), "<init>"), null },
                                       new byte[] {(byte) Opcodes.PUTFIELD | (byte) Opcodes.GETFIELD},
                                       Opcodes.ACC_FINAL),
                     new TestedCPEntry("compiler/jvmci/common/testcases/MultipleImplementer2",
@@ -296,6 +341,7 @@
                     new TestedCPEntry("compiler/jvmci/common/testcases/MultipleAbstractImplementer",
                                       "objectField",
                                       "Ljava/lang/Object;",
+                                      new ResolvedJavaMethod[] { getMethod(getType(MultipleAbstractImplementer.class), "<init>"), null },
                                       new byte[] {(byte) Opcodes.PUTFIELD | (byte) Opcodes.GETFIELD},
                                       Opcodes.ACC_FINAL),
                     new TestedCPEntry("compiler/jvmci/common/testcases/MultipleAbstractImplementer",
@@ -401,6 +447,7 @@
                     new TestedCPEntry("compiler/jvmci/common/testcases/MultipleImplementersInterface",
                                       "OBJECT_CONSTANT",
                                       "Ljava/lang/Object;",
+                                      new ResolvedJavaMethod[] { getMethod(getType(MultipleImplementersInterface.class), "<clinit>"), null },
                                       new byte[] {(byte) Opcodes.PUTSTATIC, (byte) Opcodes.GETSTATIC},
                                       Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_PUBLIC),
                 }