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