comparison graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TypeUniverse.java @ 21663:381ab4105afe

moved com.oracle.graal.java.test to com.oracle.jvmci.runtime.test
author Doug Simon <doug.simon@oracle.com>
date Tue, 02 Jun 2015 15:15:58 +0200
parents graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TypeUniverse.java@47bebae7454f
children 5910a266f32d
comparison
equal deleted inserted replaced
21662:b45e0f791465 21663:381ab4105afe
1 /*
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
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 package com.oracle.jvmci.runtime.test;
24
25 import static java.lang.reflect.Modifier.*;
26
27 import java.io.*;
28 import java.lang.reflect.*;
29 import java.util.*;
30 import java.util.Queue;
31 import java.util.stream.*;
32
33 import org.junit.*;
34
35 import sun.misc.*;
36
37 import com.oracle.jvmci.meta.*;
38 import com.oracle.jvmci.runtime.*;
39
40 /**
41 * Context for type related tests.
42 */
43 public class TypeUniverse {
44
45 public static final Unsafe unsafe;
46 public static final double JAVA_VERSION = Double.valueOf(System.getProperty("java.specification.version"));
47
48 public static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
49 public static final ConstantReflectionProvider constantReflection = JVMCI.getRuntime().getHostJVMCIBackend().getConstantReflection();
50 public static final Collection<Class<?>> classes = new HashSet<>();
51 public static final Set<ResolvedJavaType> javaTypes;
52 public static final Map<Class<?>, Class<?>> arrayClasses = new HashMap<>();
53
54 private static List<ConstantValue> constants;
55
56 static {
57 Unsafe theUnsafe = null;
58 try {
59 theUnsafe = Unsafe.getUnsafe();
60 } catch (Exception e) {
61 try {
62 Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
63 theUnsafeField.setAccessible(true);
64 theUnsafe = (Unsafe) theUnsafeField.get(null);
65 } catch (Exception e1) {
66 throw (InternalError) new InternalError("unable to initialize unsafe").initCause(e1);
67 }
68 }
69 unsafe = theUnsafe;
70
71 Class<?>[] initialClasses = {void.class, boolean.class, byte.class, short.class, char.class, int.class, float.class, long.class, double.class, Object.class, Class.class, ClassLoader.class,
72 String.class, Serializable.class, Cloneable.class, Test.class, TestMetaAccessProvider.class, List.class, Collection.class, Map.class, Queue.class, HashMap.class,
73 LinkedHashMap.class, IdentityHashMap.class, AbstractCollection.class, AbstractList.class, ArrayList.class, TrustedInterface.class};
74 for (Class<?> c : initialClasses) {
75 addClass(c);
76 }
77
78 javaTypes = Collections.unmodifiableSet(classes.stream().map(c -> metaAccess.lookupJavaType(c)).collect(Collectors.toSet()));
79 }
80
81 static class ConstantsUniverse {
82 static final Object[] ARRAYS = classes.stream().map(c -> c != void.class && !c.isArray() ? Array.newInstance(c, 42) : null).filter(o -> o != null).collect(Collectors.toList()).toArray();
83 static final Object CONST1 = new ArrayList<>();
84 static final Object CONST2 = new ArrayList<>();
85 static final Object CONST3 = new IdentityHashMap<>();
86 static final Object CONST4 = new LinkedHashMap<>();
87 static final Object CONST5 = new TreeMap<>();
88 static final Object CONST6 = new ArrayDeque<>();
89 static final Object CONST7 = new LinkedList<>();
90 static final Object CONST8 = "a string";
91 static final Object CONST9 = 42;
92 static final Object CONST10 = String.class;
93 static final Object CONST11 = String[].class;
94 }
95
96 public static List<ConstantValue> constants() {
97 if (constants == null) {
98 List<ConstantValue> res = readConstants(JavaConstant.class);
99 res.addAll(readConstants(ConstantsUniverse.class));
100 constants = res;
101 }
102 return constants;
103 }
104
105 public static class ConstantValue {
106 public final String name;
107 public final JavaConstant value;
108 public final Object boxed;
109
110 public ConstantValue(String name, JavaConstant value, Object boxed) {
111 this.name = name;
112 this.value = value;
113 this.boxed = boxed;
114 }
115
116 @Override
117 public String toString() {
118 return name + "=" + value;
119 }
120
121 public String getSimpleName() {
122 return name.substring(name.lastIndexOf('.') + 1);
123 }
124 }
125
126 /**
127 * Reads the value of all {@code static final} fields from a given class into an array of
128 * {@link ConstantValue}s.
129 */
130 public static List<ConstantValue> readConstants(Class<?> fromClass) {
131 try {
132 List<ConstantValue> res = new ArrayList<>();
133 for (Field field : fromClass.getDeclaredFields()) {
134 if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) {
135 JavaField javaField = metaAccess.lookupJavaField(field);
136 Object boxed = field.get(null);
137 if (boxed instanceof JavaConstant) {
138 res.add(new ConstantValue(javaField.format("%H.%n"), (JavaConstant) boxed, boxed));
139 } else {
140 JavaConstant value = constantReflection.readConstantFieldValue(javaField, null);
141 if (value != null) {
142 res.add(new ConstantValue(javaField.format("%H.%n"), value, boxed));
143 if (boxed instanceof Object[]) {
144 Object[] arr = (Object[]) boxed;
145 for (int i = 0; i < arr.length; i++) {
146 JavaConstant element = constantReflection.readArrayElement(value, i);
147 if (element != null) {
148 res.add(new ConstantValue(javaField.format("%H.%n[" + i + "]"), element, arr[i]));
149 }
150 }
151 }
152 }
153 }
154 }
155 }
156 return res;
157 } catch (Exception e) {
158 throw new AssertionError(e);
159 }
160 }
161
162 public synchronized Class<?> getArrayClass(Class<?> componentType) {
163 Class<?> arrayClass = arrayClasses.get(componentType);
164 if (arrayClass == null) {
165 arrayClass = Array.newInstance(componentType, 0).getClass();
166 arrayClasses.put(componentType, arrayClass);
167 }
168 return arrayClass;
169 }
170
171 public static int dimensions(Class<?> c) {
172 if (c.getComponentType() != null) {
173 return 1 + dimensions(c.getComponentType());
174 }
175 return 0;
176 }
177
178 private static void addClass(Class<?> c) {
179 if (classes.add(c)) {
180 if (c.getSuperclass() != null) {
181 addClass(c.getSuperclass());
182 }
183 for (Class<?> sc : c.getInterfaces()) {
184 addClass(sc);
185 }
186 for (Class<?> dc : c.getDeclaredClasses()) {
187 addClass(dc);
188 }
189 for (Method m : c.getDeclaredMethods()) {
190 addClass(m.getReturnType());
191 for (Class<?> p : m.getParameterTypes()) {
192 addClass(p);
193 }
194 }
195
196 if (c != void.class && dimensions(c) < 2) {
197 Class<?> arrayClass = Array.newInstance(c, 0).getClass();
198 arrayClasses.put(c, arrayClass);
199 addClass(arrayClass);
200 }
201 }
202 }
203 }