43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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.core.test.tutorial;
|
|
26 |
|
|
27 |
import java.lang.reflect.Field;
|
|
28 |
import java.lang.reflect.Method;
|
|
29 |
import java.util.Arrays;
|
|
30 |
import java.util.Collection;
|
|
31 |
|
|
32 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
33 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
34 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
35 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
36 |
|
|
37 |
import org.junit.Assert;
|
|
38 |
import org.junit.Test;
|
|
39 |
|
|
40 |
import org.graalvm.compiler.api.test.Graal;
|
|
41 |
import org.graalvm.compiler.core.target.Backend;
|
|
42 |
import org.graalvm.compiler.core.test.tutorial.StaticAnalysis.MethodState;
|
|
43 |
import org.graalvm.compiler.core.test.tutorial.StaticAnalysis.TypeFlow;
|
|
44 |
import org.graalvm.compiler.nodes.spi.StampProvider;
|
|
45 |
import org.graalvm.compiler.phases.util.Providers;
|
|
46 |
import org.graalvm.compiler.runtime.RuntimeProvider;
|
|
47 |
|
|
48 |
public class StaticAnalysisTests {
|
|
49 |
|
|
50 |
static class A {
|
|
51 |
Object foo(Object arg) {
|
|
52 |
return arg;
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
static class B extends A {
|
|
57 |
@Override
|
|
58 |
Object foo(Object arg) {
|
|
59 |
if (arg instanceof Data) {
|
|
60 |
return ((Data) arg).f;
|
|
61 |
} else {
|
|
62 |
return super.foo(arg);
|
|
63 |
}
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
static class Data {
|
|
68 |
Object f;
|
|
69 |
}
|
|
70 |
|
|
71 |
private final MetaAccessProvider metaAccess;
|
|
72 |
private final StampProvider stampProvider;
|
|
73 |
|
|
74 |
public StaticAnalysisTests() {
|
|
75 |
Backend backend = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend();
|
|
76 |
Providers providers = backend.getProviders();
|
|
77 |
this.metaAccess = providers.getMetaAccess();
|
|
78 |
this.stampProvider = providers.getStampProvider();
|
|
79 |
}
|
|
80 |
|
|
81 |
static void test01Entry() {
|
|
82 |
A a = new A();
|
|
83 |
a.foo(null);
|
|
84 |
}
|
|
85 |
|
|
86 |
@Test
|
|
87 |
public void test01() {
|
|
88 |
StaticAnalysis sa = new StaticAnalysis(metaAccess, stampProvider);
|
|
89 |
sa.addMethod(findMethod(StaticAnalysisTests.class, "test01Entry"));
|
|
90 |
sa.finish();
|
|
91 |
|
|
92 |
assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class));
|
|
93 |
assertEquals(f(sa, Data.class, "f"));
|
|
94 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class));
|
|
95 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[1]);
|
|
96 |
assertEquals(m(sa, A.class, "foo").getFormalReturn());
|
|
97 |
}
|
|
98 |
|
|
99 |
static void test02Entry() {
|
|
100 |
A a = new A();
|
|
101 |
a.foo(new Data());
|
|
102 |
|
|
103 |
B b = new B();
|
|
104 |
b.foo(null);
|
|
105 |
}
|
|
106 |
|
|
107 |
@Test
|
|
108 |
public void test02() {
|
|
109 |
StaticAnalysis sa = new StaticAnalysis(metaAccess, stampProvider);
|
|
110 |
sa.addMethod(findMethod(StaticAnalysisTests.class, "test02Entry"));
|
|
111 |
sa.finish();
|
|
112 |
|
|
113 |
assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(B.class), t(Data.class));
|
|
114 |
assertEquals(f(sa, Data.class, "f"));
|
|
115 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class), t(B.class));
|
|
116 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class));
|
|
117 |
assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class));
|
|
118 |
assertEquals(m(sa, B.class, "foo").getFormalParameters()[0], t(B.class));
|
|
119 |
assertEquals(m(sa, B.class, "foo").getFormalParameters()[1]);
|
|
120 |
assertEquals(m(sa, B.class, "foo").getFormalReturn(), t(Data.class));
|
|
121 |
}
|
|
122 |
|
|
123 |
static void test03Entry() {
|
|
124 |
Data data = new Data();
|
|
125 |
data.f = new Integer(42);
|
|
126 |
|
|
127 |
A a = new A();
|
|
128 |
a.foo(new Data());
|
|
129 |
|
|
130 |
B b = new B();
|
|
131 |
b.foo(null);
|
|
132 |
}
|
|
133 |
|
|
134 |
@Test
|
|
135 |
public void test03() {
|
|
136 |
StaticAnalysis sa = new StaticAnalysis(metaAccess, stampProvider);
|
|
137 |
sa.addMethod(findMethod(StaticAnalysisTests.class, "test03Entry"));
|
|
138 |
sa.finish();
|
|
139 |
|
|
140 |
assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(B.class), t(Data.class), t(Integer.class));
|
|
141 |
assertEquals(f(sa, Data.class, "f"), t(Integer.class));
|
|
142 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class), t(B.class));
|
|
143 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class));
|
|
144 |
assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class));
|
|
145 |
assertEquals(m(sa, B.class, "foo").getFormalParameters()[0], t(B.class));
|
|
146 |
assertEquals(m(sa, B.class, "foo").getFormalParameters()[1]);
|
|
147 |
assertEquals(m(sa, B.class, "foo").getFormalReturn(), t(Data.class), t(Integer.class));
|
|
148 |
}
|
|
149 |
|
|
150 |
static void test04Entry() {
|
|
151 |
Data data = null;
|
|
152 |
for (int i = 0; i < 2; i++) {
|
|
153 |
if (i == 0) {
|
|
154 |
data = new Data();
|
|
155 |
} else if (i == 1) {
|
|
156 |
data.f = new Integer(42);
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
A a = new A();
|
|
161 |
a.foo(data);
|
|
162 |
}
|
|
163 |
|
|
164 |
@Test
|
|
165 |
public void test04() {
|
|
166 |
StaticAnalysis sa = new StaticAnalysis(metaAccess, stampProvider);
|
|
167 |
sa.addMethod(findMethod(StaticAnalysisTests.class, "test04Entry"));
|
|
168 |
sa.finish();
|
|
169 |
|
|
170 |
assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(Data.class), t(Integer.class));
|
|
171 |
assertEquals(f(sa, Data.class, "f"), t(Integer.class));
|
|
172 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class));
|
|
173 |
assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class));
|
|
174 |
assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class));
|
|
175 |
}
|
|
176 |
|
|
177 |
private MethodState m(StaticAnalysis sa, Class<?> declaringClass, String name) {
|
|
178 |
return sa.getResults().lookupMethod(findMethod(declaringClass, name));
|
|
179 |
}
|
|
180 |
|
|
181 |
private TypeFlow f(StaticAnalysis sa, Class<?> declaringClass, String name) {
|
|
182 |
return sa.getResults().lookupField(findField(declaringClass, name));
|
|
183 |
}
|
|
184 |
|
|
185 |
private static void assertEquals(TypeFlow actual, Object... expected) {
|
|
186 |
Collection<?> actualTypes = actual.getTypes();
|
|
187 |
if (actualTypes.size() != expected.length || !actualTypes.containsAll(Arrays.asList(expected))) {
|
|
188 |
Assert.fail(actualTypes + " != " + Arrays.asList(expected));
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
private ResolvedJavaType t(Class<?> clazz) {
|
|
193 |
return metaAccess.lookupJavaType(clazz);
|
|
194 |
}
|
|
195 |
|
|
196 |
private ResolvedJavaMethod findMethod(Class<?> declaringClass, String name) {
|
|
197 |
Method reflectionMethod = null;
|
|
198 |
for (Method m : declaringClass.getDeclaredMethods()) {
|
|
199 |
if (m.getName().equals(name)) {
|
|
200 |
assert reflectionMethod == null : "More than one method with name " + name + " in class " + declaringClass.getName();
|
|
201 |
reflectionMethod = m;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
assert reflectionMethod != null : "No method with name " + name + " in class " + declaringClass.getName();
|
|
205 |
return metaAccess.lookupJavaMethod(reflectionMethod);
|
|
206 |
}
|
|
207 |
|
|
208 |
private ResolvedJavaField findField(Class<?> declaringClass, String name) {
|
|
209 |
Field reflectionField;
|
|
210 |
try {
|
|
211 |
reflectionField = declaringClass.getDeclaredField(name);
|
|
212 |
} catch (NoSuchFieldException | SecurityException ex) {
|
|
213 |
throw new AssertionError(ex);
|
|
214 |
}
|
|
215 |
return metaAccess.lookupJavaField(reflectionField);
|
|
216 |
}
|
|
217 |
}
|