test/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TypeUniverse.java

Print this page




  32 import java.util.AbstractCollection;
  33 import java.util.AbstractList;
  34 import java.util.ArrayDeque;
  35 import java.util.ArrayList;
  36 import java.util.Collection;
  37 import java.util.Collections;
  38 import java.util.HashMap;
  39 import java.util.HashSet;
  40 import java.util.IdentityHashMap;
  41 import java.util.LinkedHashMap;
  42 import java.util.LinkedList;
  43 import java.util.List;
  44 import java.util.Map;
  45 import java.util.Queue;
  46 import java.util.Set;
  47 import java.util.TreeMap;
  48 import java.util.stream.Collectors;
  49 
  50 import jdk.vm.ci.meta.ConstantReflectionProvider;
  51 import jdk.vm.ci.meta.JavaConstant;
  52 import jdk.vm.ci.meta.JavaField;
  53 import jdk.vm.ci.meta.MetaAccessProvider;

  54 import jdk.vm.ci.meta.ResolvedJavaType;
  55 import jdk.vm.ci.meta.TrustedInterface;
  56 import jdk.vm.ci.runtime.JVMCI;
  57 
  58 import org.junit.Test;
  59 
  60 import sun.misc.Unsafe;
  61 
  62 //JaCoCo Exclude
  63 
  64 /**
  65  * Context for type related tests.
  66  */
  67 public class TypeUniverse {
  68 
  69     public static final Unsafe unsafe;
  70     public static final double JAVA_VERSION = Double.valueOf(System.getProperty("java.specification.version"));
  71 
  72     public static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
  73     public static final ConstantReflectionProvider constantReflection = JVMCI.getRuntime().getHostJVMCIBackend().getConstantReflection();


 162 
 163         @Override
 164         public String toString() {
 165             return name + "=" + value;
 166         }
 167 
 168         public String getSimpleName() {
 169             return name.substring(name.lastIndexOf('.') + 1);
 170         }
 171     }
 172 
 173     /**
 174      * Reads the value of all {@code static final} fields from a given class into an array of
 175      * {@link ConstantValue}s.
 176      */
 177     public static List<ConstantValue> readConstants(Class<?> fromClass) {
 178         try {
 179             List<ConstantValue> res = new ArrayList<>();
 180             for (Field field : fromClass.getDeclaredFields()) {
 181                 if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) {
 182                     JavaField javaField = metaAccess.lookupJavaField(field);
 183                     Object boxed = field.get(null);
 184                     if (boxed instanceof JavaConstant) {
 185                         res.add(new ConstantValue(javaField.format("%H.%n"), (JavaConstant) boxed, boxed));
 186                     } else {
 187                         JavaConstant value = constantReflection.readConstantFieldValue(javaField, null);
 188                         if (value != null) {
 189                             res.add(new ConstantValue(javaField.format("%H.%n"), value, boxed));
 190                             if (boxed instanceof Object[]) {
 191                                 Object[] arr = (Object[]) boxed;
 192                                 for (int i = 0; i < arr.length; i++) {
 193                                     JavaConstant element = constantReflection.readArrayElement(value, i);
 194                                     if (element != null) {
 195                                         res.add(new ConstantValue(javaField.format("%H.%n[" + i + "]"), element, arr[i]));
 196                                     }
 197                                 }
 198                             }
 199                         }
 200                     }
 201                 }
 202             }




  32 import java.util.AbstractCollection;
  33 import java.util.AbstractList;
  34 import java.util.ArrayDeque;
  35 import java.util.ArrayList;
  36 import java.util.Collection;
  37 import java.util.Collections;
  38 import java.util.HashMap;
  39 import java.util.HashSet;
  40 import java.util.IdentityHashMap;
  41 import java.util.LinkedHashMap;
  42 import java.util.LinkedList;
  43 import java.util.List;
  44 import java.util.Map;
  45 import java.util.Queue;
  46 import java.util.Set;
  47 import java.util.TreeMap;
  48 import java.util.stream.Collectors;
  49 
  50 import jdk.vm.ci.meta.ConstantReflectionProvider;
  51 import jdk.vm.ci.meta.JavaConstant;

  52 import jdk.vm.ci.meta.MetaAccessProvider;
  53 import jdk.vm.ci.meta.ResolvedJavaField;
  54 import jdk.vm.ci.meta.ResolvedJavaType;
  55 import jdk.vm.ci.meta.TrustedInterface;
  56 import jdk.vm.ci.runtime.JVMCI;
  57 
  58 import org.junit.Test;
  59 
  60 import sun.misc.Unsafe;
  61 
  62 //JaCoCo Exclude
  63 
  64 /**
  65  * Context for type related tests.
  66  */
  67 public class TypeUniverse {
  68 
  69     public static final Unsafe unsafe;
  70     public static final double JAVA_VERSION = Double.valueOf(System.getProperty("java.specification.version"));
  71 
  72     public static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
  73     public static final ConstantReflectionProvider constantReflection = JVMCI.getRuntime().getHostJVMCIBackend().getConstantReflection();


 162 
 163         @Override
 164         public String toString() {
 165             return name + "=" + value;
 166         }
 167 
 168         public String getSimpleName() {
 169             return name.substring(name.lastIndexOf('.') + 1);
 170         }
 171     }
 172 
 173     /**
 174      * Reads the value of all {@code static final} fields from a given class into an array of
 175      * {@link ConstantValue}s.
 176      */
 177     public static List<ConstantValue> readConstants(Class<?> fromClass) {
 178         try {
 179             List<ConstantValue> res = new ArrayList<>();
 180             for (Field field : fromClass.getDeclaredFields()) {
 181                 if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) {
 182                     ResolvedJavaField javaField = metaAccess.lookupJavaField(field);
 183                     Object boxed = field.get(null);
 184                     if (boxed instanceof JavaConstant) {
 185                         res.add(new ConstantValue(javaField.format("%H.%n"), (JavaConstant) boxed, boxed));
 186                     } else {
 187                         JavaConstant value = constantReflection.readConstantFieldValue(javaField, null);
 188                         if (value != null) {
 189                             res.add(new ConstantValue(javaField.format("%H.%n"), value, boxed));
 190                             if (boxed instanceof Object[]) {
 191                                 Object[] arr = (Object[]) boxed;
 192                                 for (int i = 0; i < arr.length; i++) {
 193                                     JavaConstant element = constantReflection.readArrayElement(value, i);
 194                                     if (element != null) {
 195                                         res.add(new ConstantValue(javaField.format("%H.%n[" + i + "]"), element, arr[i]));
 196                                     }
 197                                 }
 198                             }
 199                         }
 200                     }
 201                 }
 202             }