< prev index next >

test/compiler/jvmci/compilerToVM/GetSymbolTest.java

Print this page




  35  *                              compiler.jvmci.common.CTVMUtilities
  36  *                              jdk.test.lib.Utils
  37  *                              jdk.test.lib.Asserts
  38  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  39  *     -Xbootclasspath/a:. compiler.jvmci.compilerToVM.GetSymbolTest
  40  */
  41 
  42 package compiler.jvmci.compilerToVM;
  43 
  44 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  45 import compiler.jvmci.common.CTVMUtilities;
  46 import compiler.jvmci.common.testcases.SingleImplementer;
  47 import java.lang.reflect.Field;
  48 import java.lang.reflect.Member;
  49 import java.lang.reflect.Method;
  50 import java.util.ArrayList;
  51 import java.util.List;
  52 import java.util.function.Function;
  53 import java.util.stream.Collectors;
  54 import java.util.stream.Stream;
  55 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl;
  56 import jdk.vm.ci.meta.ConstantPool;
  57 import jdk.test.lib.Utils;
  58 
  59 public class GetSymbolTest {
  60     private static final int CONSTANT_POOL_UTF8_TAG = 1; // see jvms, section 4.4
  61 
  62     private static final Function<Member[], List<String>> NAMES = members ->
  63             Stream.of(members)
  64                     .map(Member::getName)
  65                     .collect(Collectors.toList());
  66 
  67     public static void main(String[] args) {
  68         new GetSymbolTest().test(SingleImplementer.class);
  69     }
  70 
  71     private void test(Class<?> aClass) {
  72         Utils.ensureClassIsLoaded(aClass);
  73         Method method;
  74         try {
  75             method = aClass.getDeclaredMethod("nonInterfaceMethod");
  76         } catch (NoSuchMethodException e) {
  77             throw new Error("TEST BUG: can't find test method", e);
  78         }
  79         HotSpotResolvedJavaMethodImpl resolvedMethod
  80                 = CTVMUtilities.getResolvedMethod(aClass, method);
  81         List<String> symbols;
  82         try {
  83             symbols = getSymbols(resolvedMethod);
  84         } catch (ReflectiveOperationException e) {
  85             throw new Error("TEST BUG: can't access private members", e);
  86         }
  87         List<String> classSymbols = new ArrayList<>();
  88         classSymbols.addAll(NAMES.apply(aClass.getDeclaredFields()));
  89         classSymbols.addAll(NAMES.apply(aClass.getDeclaredMethods()));
  90         // Check that all members of test class have symbols from constant pool
  91         for (String s : classSymbols) {
  92             if (!symbols.contains(s)) {
  93                 // failed. print all symbols found by getSymbol
  94                 System.out.println("getSymbol data:");
  95                 for (String ctvmValue : symbols) {
  96                     System.out.println(ctvmValue);
  97                 }
  98                 throw new AssertionError("Unable to find symbol " + s
  99                         + " using CompilerToVM.getSymbol");
 100             }
 101         }
 102     }
 103 
 104     private List<String> getSymbols(HotSpotResolvedJavaMethodImpl
 105             metaspaceMethod) throws ReflectiveOperationException {
 106         List<String> symbols = new ArrayList<>();
 107         ConstantPool pool = metaspaceMethod.getConstantPool();
 108         long length = pool.length();
 109         // jvms-4.1: The constant_pool table is indexed from 1 ...
 110         for (int i = 1; i < length; i++) {
 111             if (getTag(pool, i) == CONSTANT_POOL_UTF8_TAG) {
 112                 long entryPointer;
 113                 Method getEntryAt = pool.getClass()
 114                         .getDeclaredMethod("getEntryAt", int.class);
 115                 getEntryAt.setAccessible(true);
 116                 entryPointer = (Long) getEntryAt.invoke(pool, i);
 117                 String symbol = CompilerToVMHelper.getSymbol(entryPointer);
 118                 symbols.add(symbol);
 119             }
 120         }
 121         return symbols;
 122     }
 123 
 124     private int getTag(ConstantPool pool, int index)


  35  *                              compiler.jvmci.common.CTVMUtilities
  36  *                              jdk.test.lib.Utils
  37  *                              jdk.test.lib.Asserts
  38  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  39  *     -Xbootclasspath/a:. compiler.jvmci.compilerToVM.GetSymbolTest
  40  */
  41 
  42 package compiler.jvmci.compilerToVM;
  43 
  44 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  45 import compiler.jvmci.common.CTVMUtilities;
  46 import compiler.jvmci.common.testcases.SingleImplementer;
  47 import java.lang.reflect.Field;
  48 import java.lang.reflect.Member;
  49 import java.lang.reflect.Method;
  50 import java.util.ArrayList;
  51 import java.util.List;
  52 import java.util.function.Function;
  53 import java.util.stream.Collectors;
  54 import java.util.stream.Stream;
  55 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  56 import jdk.vm.ci.meta.ConstantPool;
  57 import jdk.test.lib.Utils;
  58 
  59 public class GetSymbolTest {
  60     private static final int CONSTANT_POOL_UTF8_TAG = 1; // see jvms, section 4.4
  61 
  62     private static final Function<Member[], List<String>> NAMES = members ->
  63             Stream.of(members)
  64                     .map(Member::getName)
  65                     .collect(Collectors.toList());
  66 
  67     public static void main(String[] args) {
  68         new GetSymbolTest().test(SingleImplementer.class);
  69     }
  70 
  71     private void test(Class<?> aClass) {
  72         Utils.ensureClassIsLoaded(aClass);
  73         Method method;
  74         try {
  75             method = aClass.getDeclaredMethod("nonInterfaceMethod");
  76         } catch (NoSuchMethodException e) {
  77             throw new Error("TEST BUG: can't find test method", e);
  78         }
  79         HotSpotResolvedJavaMethod resolvedMethod
  80                 = CTVMUtilities.getResolvedMethod(aClass, method);
  81         List<String> symbols;
  82         try {
  83             symbols = getSymbols(resolvedMethod);
  84         } catch (ReflectiveOperationException e) {
  85             throw new Error("TEST BUG: can't access private members", e);
  86         }
  87         List<String> classSymbols = new ArrayList<>();
  88         classSymbols.addAll(NAMES.apply(aClass.getDeclaredFields()));
  89         classSymbols.addAll(NAMES.apply(aClass.getDeclaredMethods()));
  90         // Check that all members of test class have symbols from constant pool
  91         for (String s : classSymbols) {
  92             if (!symbols.contains(s)) {
  93                 // failed. print all symbols found by getSymbol
  94                 System.out.println("getSymbol data:");
  95                 for (String ctvmValue : symbols) {
  96                     System.out.println(ctvmValue);
  97                 }
  98                 throw new AssertionError("Unable to find symbol " + s
  99                         + " using CompilerToVM.getSymbol");
 100             }
 101         }
 102     }
 103 
 104     private List<String> getSymbols(HotSpotResolvedJavaMethod
 105             metaspaceMethod) throws ReflectiveOperationException {
 106         List<String> symbols = new ArrayList<>();
 107         ConstantPool pool = metaspaceMethod.getConstantPool();
 108         long length = pool.length();
 109         // jvms-4.1: The constant_pool table is indexed from 1 ...
 110         for (int i = 1; i < length; i++) {
 111             if (getTag(pool, i) == CONSTANT_POOL_UTF8_TAG) {
 112                 long entryPointer;
 113                 Method getEntryAt = pool.getClass()
 114                         .getDeclaredMethod("getEntryAt", int.class);
 115                 getEntryAt.setAccessible(true);
 116                 entryPointer = (Long) getEntryAt.invoke(pool, i);
 117                 String symbol = CompilerToVMHelper.getSymbol(entryPointer);
 118                 symbols.add(symbol);
 119             }
 120         }
 121         return symbols;
 122     }
 123 
 124     private int getTag(ConstantPool pool, int index)
< prev index next >