comparison graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TypeUniverse.java @ 21555:d12eaef9af72

renamed com.oracle.graal.api.meta.test to com.oracle.graal.java.test since it is Graal specific (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Tue, 26 May 2015 23:45:05 +0200
parents graal/com.oracle.graal.api.meta.test/src/com/oracle/graal/api/meta/test/TypeUniverse.java@973c6551dfd8
children 48c1ebd24120
comparison
equal deleted inserted replaced
21554:b1530a6cce8c 21555:d12eaef9af72
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.graal.java.test;
24
25 import java.io.*;
26 import java.lang.reflect.*;
27 import java.util.*;
28 import java.util.Queue;
29
30 import org.junit.*;
31
32 import sun.misc.*;
33
34 import com.oracle.graal.api.meta.*;
35 import com.oracle.graal.api.replacements.*;
36 import com.oracle.graal.api.runtime.*;
37 import com.oracle.graal.phases.util.*;
38 import com.oracle.graal.runtime.*;
39
40 /**
41 * Context for type related api.meta tests.
42 */
43 public class TypeUniverse {
44
45 public final Unsafe unsafe;
46 public static final double JAVA_VERSION = Double.valueOf(System.getProperty("java.specification.version"));
47
48 public final MetaAccessProvider metaAccess;
49 public final ConstantReflectionProvider constantReflection;
50 public final SnippetReflectionProvider snippetReflection;
51 public final Collection<Class<?>> classes = new HashSet<>();
52 public final Map<Class<?>, Class<?>> arrayClasses = new HashMap<>();
53 public final List<JavaConstant> constants = new ArrayList<>();
54
55 public TypeUniverse() {
56 Providers providers = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders();
57 metaAccess = providers.getMetaAccess();
58 constantReflection = providers.getConstantReflection();
59 snippetReflection = Graal.getRequiredCapability(SnippetReflectionProvider.class);
60 Unsafe theUnsafe = null;
61 try {
62 theUnsafe = Unsafe.getUnsafe();
63 } catch (Exception e) {
64 try {
65 Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
66 theUnsafeField.setAccessible(true);
67 theUnsafe = (Unsafe) theUnsafeField.get(null);
68 } catch (Exception e1) {
69 throw (InternalError) new InternalError("unable to initialize unsafe").initCause(e1);
70 }
71 }
72 unsafe = theUnsafe;
73
74 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,
75 String.class, Serializable.class, Cloneable.class, Test.class, TestMetaAccessProvider.class, List.class, Collection.class, Map.class, Queue.class, HashMap.class,
76 LinkedHashMap.class, IdentityHashMap.class, AbstractCollection.class, AbstractList.class, ArrayList.class, TrustedInterface.class};
77 for (Class<?> c : initialClasses) {
78 addClass(c);
79 }
80 for (Field f : JavaConstant.class.getDeclaredFields()) {
81 int mods = f.getModifiers();
82 if (f.getType() == JavaConstant.class && Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) {
83 try {
84 JavaConstant c = (JavaConstant) f.get(null);
85 if (c != null) {
86 constants.add(c);
87 }
88 } catch (Exception e) {
89 }
90 }
91 }
92 for (Class<?> c : classes) {
93 if (c != void.class && !c.isArray()) {
94 constants.add(snippetReflection.forObject(Array.newInstance(c, 42)));
95 }
96 }
97 constants.add(snippetReflection.forObject(new ArrayList<>()));
98 constants.add(snippetReflection.forObject(new IdentityHashMap<>()));
99 constants.add(snippetReflection.forObject(new LinkedHashMap<>()));
100 constants.add(snippetReflection.forObject(new TreeMap<>()));
101 constants.add(snippetReflection.forObject(new ArrayDeque<>()));
102 constants.add(snippetReflection.forObject(new LinkedList<>()));
103 constants.add(snippetReflection.forObject("a string"));
104 constants.add(snippetReflection.forObject(42));
105 constants.add(snippetReflection.forObject(String.class));
106 constants.add(snippetReflection.forObject(String[].class));
107 }
108
109 public synchronized Class<?> getArrayClass(Class<?> componentType) {
110 Class<?> arrayClass = arrayClasses.get(componentType);
111 if (arrayClass == null) {
112 arrayClass = Array.newInstance(componentType, 0).getClass();
113 arrayClasses.put(componentType, arrayClass);
114 }
115 return arrayClass;
116 }
117
118 public static int dimensions(Class<?> c) {
119 if (c.getComponentType() != null) {
120 return 1 + dimensions(c.getComponentType());
121 }
122 return 0;
123 }
124
125 private void addClass(Class<?> c) {
126 if (classes.add(c)) {
127 if (c.getSuperclass() != null) {
128 addClass(c.getSuperclass());
129 }
130 for (Class<?> sc : c.getInterfaces()) {
131 addClass(sc);
132 }
133 for (Class<?> dc : c.getDeclaredClasses()) {
134 addClass(dc);
135 }
136 for (Method m : c.getDeclaredMethods()) {
137 addClass(m.getReturnType());
138 for (Class<?> p : m.getParameterTypes()) {
139 addClass(p);
140 }
141 }
142
143 if (c != void.class && dimensions(c) < 2) {
144 Class<?> arrayClass = Array.newInstance(c, 0).getClass();
145 arrayClasses.put(c, arrayClass);
146 addClass(arrayClass);
147 }
148 }
149 }
150 }