43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2012, 2015, 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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.hotspot.test;
|
|
26 |
|
|
27 |
import java.lang.invoke.ConstantCallSite;
|
|
28 |
import java.lang.invoke.MethodHandles;
|
|
29 |
import java.lang.invoke.MethodType;
|
|
30 |
|
|
31 |
import org.junit.Test;
|
|
32 |
|
|
33 |
import org.graalvm.compiler.api.directives.GraalDirectives;
|
|
34 |
import org.graalvm.compiler.api.replacements.MethodSubstitution;
|
|
35 |
import org.graalvm.compiler.replacements.test.MethodSubstitutionTest;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Tests HotSpot specific {@link MethodSubstitution}s.
|
|
39 |
*/
|
|
40 |
public class HotSpotMethodSubstitutionTest extends MethodSubstitutionTest {
|
|
41 |
|
|
42 |
@Test
|
|
43 |
public void testObjectSubstitutions() {
|
|
44 |
TestClassA obj = new TestClassA();
|
|
45 |
|
|
46 |
testGraph("getClass0");
|
|
47 |
testGraph("objectHashCode");
|
|
48 |
|
|
49 |
test("getClass0", "a string");
|
|
50 |
test("objectHashCode", obj);
|
49873
|
51 |
|
|
52 |
testGraph("objectNotify", "Object.notify");
|
|
53 |
testGraph("objectNotifyAll", "Object.notifyAll");
|
|
54 |
|
|
55 |
synchronized (obj) {
|
|
56 |
test("objectNotify", obj);
|
|
57 |
test("objectNotifyAll", obj);
|
|
58 |
}
|
|
59 |
// Test with IllegalMonitorStateException (no synchronized block)
|
|
60 |
test("objectNotify", obj);
|
|
61 |
test("objectNotifyAll", obj);
|
43972
|
62 |
}
|
|
63 |
|
|
64 |
@SuppressWarnings("all")
|
|
65 |
public static Class<?> getClass0(Object obj) {
|
|
66 |
return obj.getClass();
|
|
67 |
}
|
|
68 |
|
|
69 |
@SuppressWarnings("all")
|
|
70 |
public static int objectHashCode(TestClassA obj) {
|
|
71 |
return obj.hashCode();
|
|
72 |
}
|
|
73 |
|
49873
|
74 |
@SuppressWarnings("all")
|
|
75 |
public static void objectNotify(Object obj) {
|
|
76 |
obj.notify();
|
|
77 |
}
|
|
78 |
|
|
79 |
@SuppressWarnings("all")
|
|
80 |
public static void objectNotifyAll(Object obj) {
|
|
81 |
obj.notifyAll();
|
|
82 |
}
|
|
83 |
|
43972
|
84 |
@Test
|
|
85 |
public void testClassSubstitutions() {
|
|
86 |
testGraph("getModifiers");
|
|
87 |
testGraph("isInterface");
|
|
88 |
testGraph("isArray");
|
|
89 |
testGraph("isPrimitive");
|
|
90 |
testGraph("getSuperClass");
|
|
91 |
testGraph("getComponentType");
|
|
92 |
|
|
93 |
for (Class<?> c : new Class<?>[]{getClass(), Cloneable.class, int[].class, String[][].class}) {
|
|
94 |
test("getModifiers", c);
|
|
95 |
test("isInterface", c);
|
|
96 |
test("isArray", c);
|
|
97 |
test("isPrimitive", c);
|
|
98 |
test("getSuperClass", c);
|
|
99 |
test("getComponentType", c);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
@SuppressWarnings("all")
|
|
104 |
public static int getModifiers(Class<?> clazz) {
|
|
105 |
return clazz.getModifiers();
|
|
106 |
}
|
|
107 |
|
|
108 |
@SuppressWarnings("all")
|
|
109 |
public static boolean isInterface(Class<?> clazz) {
|
|
110 |
return clazz.isInterface();
|
|
111 |
}
|
|
112 |
|
|
113 |
@SuppressWarnings("all")
|
|
114 |
public static boolean isArray(Class<?> clazz) {
|
|
115 |
return clazz.isArray();
|
|
116 |
}
|
|
117 |
|
|
118 |
@SuppressWarnings("all")
|
|
119 |
public static boolean isPrimitive(Class<?> clazz) {
|
|
120 |
return clazz.isPrimitive();
|
|
121 |
}
|
|
122 |
|
|
123 |
@SuppressWarnings("all")
|
|
124 |
public static Class<?> getSuperClass(Class<?> clazz) {
|
|
125 |
return clazz.getSuperclass();
|
|
126 |
}
|
|
127 |
|
|
128 |
@SuppressWarnings("all")
|
|
129 |
public static Class<?> getComponentType(Class<?> clazz) {
|
|
130 |
return clazz.getComponentType();
|
|
131 |
}
|
|
132 |
|
|
133 |
@Test
|
|
134 |
public void testThreadSubstitutions() {
|
|
135 |
testGraph("currentThread");
|
|
136 |
testGraph("threadIsInterrupted");
|
|
137 |
testGraph("threadInterrupted");
|
|
138 |
|
|
139 |
Thread currentThread = Thread.currentThread();
|
|
140 |
test("currentThread", currentThread);
|
|
141 |
test("threadIsInterrupted", currentThread);
|
|
142 |
}
|
|
143 |
|
|
144 |
@SuppressWarnings("all")
|
|
145 |
public static boolean currentThread(Thread other) {
|
|
146 |
return Thread.currentThread() == other;
|
|
147 |
}
|
|
148 |
|
|
149 |
@SuppressWarnings("all")
|
|
150 |
public static boolean threadIsInterrupted(Thread thread) {
|
|
151 |
return thread.isInterrupted();
|
|
152 |
}
|
|
153 |
|
|
154 |
@SuppressWarnings("all")
|
|
155 |
public static boolean threadInterrupted() {
|
|
156 |
return Thread.interrupted();
|
|
157 |
}
|
|
158 |
|
|
159 |
@Test
|
|
160 |
public void testSystemSubstitutions() {
|
|
161 |
testGraph("systemTime");
|
|
162 |
testGraph("systemIdentityHashCode");
|
|
163 |
|
|
164 |
for (Object o : new Object[]{this, new int[5], new String[2][], new Object()}) {
|
|
165 |
test("systemIdentityHashCode", o);
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
@SuppressWarnings("all")
|
|
170 |
public static long systemTime() {
|
|
171 |
return System.currentTimeMillis() + System.nanoTime();
|
|
172 |
}
|
|
173 |
|
|
174 |
@SuppressWarnings("all")
|
|
175 |
public static int systemIdentityHashCode(Object obj) {
|
|
176 |
return System.identityHashCode(obj);
|
|
177 |
}
|
|
178 |
|
|
179 |
private static class TestClassA {
|
|
180 |
}
|
|
181 |
|
|
182 |
public static String testCallSiteGetTargetSnippet(int i) throws Exception {
|
|
183 |
ConstantCallSite site;
|
|
184 |
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
|
185 |
switch (i) {
|
|
186 |
case 1:
|
|
187 |
site = GraalDirectives.opaque(new ConstantCallSite(lookup.findVirtual(String.class, "replace", MethodType.methodType(String.class, char.class, char.class))));
|
|
188 |
break;
|
|
189 |
default:
|
|
190 |
site = GraalDirectives.opaque(new ConstantCallSite(lookup.findStatic(java.util.Arrays.class, "asList", MethodType.methodType(java.util.List.class, Object[].class))));
|
|
191 |
}
|
|
192 |
return site.getTarget().toString();
|
|
193 |
}
|
|
194 |
|
|
195 |
public static String testCastSnippet(int i, Object obj) throws Exception {
|
|
196 |
Class<?> c;
|
|
197 |
switch (i) {
|
|
198 |
case 1:
|
|
199 |
c = GraalDirectives.opaque(Number.class);
|
|
200 |
break;
|
|
201 |
default:
|
|
202 |
c = GraalDirectives.opaque(Integer.class);
|
|
203 |
break;
|
|
204 |
}
|
|
205 |
return c.cast(obj).toString();
|
|
206 |
}
|
|
207 |
|
|
208 |
public static String testGetClassSnippet(int i) {
|
|
209 |
Object c;
|
|
210 |
switch (i) {
|
|
211 |
case 1:
|
|
212 |
c = GraalDirectives.opaque(new Object());
|
|
213 |
break;
|
|
214 |
default:
|
|
215 |
c = GraalDirectives.opaque("TEST");
|
|
216 |
break;
|
|
217 |
}
|
|
218 |
return c.getClass().toString();
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Tests ambiguous receiver of CallSite.getTarget.
|
|
223 |
*/
|
|
224 |
@Test
|
|
225 |
public void testCallSiteGetTarget() {
|
|
226 |
test("testCallSiteGetTargetSnippet", 1);
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Tests ambiguous receiver of Class.cast.
|
|
231 |
*/
|
|
232 |
@Test
|
|
233 |
public void testCast() {
|
|
234 |
test("testCastSnippet", 1, new Integer(1));
|
|
235 |
}
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Tests ambiguous receiver of Object.getClass.
|
|
239 |
*/
|
|
240 |
@Test
|
|
241 |
public void testGetClass() {
|
|
242 |
test("testGetClassSnippet", 1);
|
|
243 |
}
|
|
244 |
}
|