1 /* 2 * Copyright (c) 2015, 2018, 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 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 @SuppressWarnings({"deprecation", "unused"}) 124 static void test03Entry() { 125 Data data = new Data(); 126 data.f = new Integer(42); 127 128 A a = new A(); 129 a.foo(new Data()); 130 131 B b = new B(); 132 b.foo(null); 133 } 134 135 @Test 136 public void test03() { 137 StaticAnalysis sa = new StaticAnalysis(metaAccess, stampProvider); 138 sa.addMethod(findMethod(StaticAnalysisTests.class, "test03Entry")); 139 sa.finish(); 140 141 assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(B.class), t(Data.class), t(Integer.class)); 142 assertEquals(f(sa, Data.class, "f"), t(Integer.class)); 143 assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class), t(B.class)); 144 assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class)); 145 assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class)); 146 assertEquals(m(sa, B.class, "foo").getFormalParameters()[0], t(B.class)); 147 assertEquals(m(sa, B.class, "foo").getFormalParameters()[1]); 148 assertEquals(m(sa, B.class, "foo").getFormalReturn(), t(Data.class), t(Integer.class)); 149 } 150 151 @SuppressWarnings({"deprecation", "unused"}) 152 static void test04Entry() { 153 Data data = null; 154 for (int i = 0; i < 2; i++) { 155 if (i == 0) { 156 data = new Data(); 157 } else if (i == 1) { 158 data.f = new Integer(42); 159 } 160 } 161 162 A a = new A(); 163 a.foo(data); 164 } 165 166 @Test 167 public void test04() { 168 StaticAnalysis sa = new StaticAnalysis(metaAccess, stampProvider); 169 sa.addMethod(findMethod(StaticAnalysisTests.class, "test04Entry")); 170 sa.finish(); 171 172 assertEquals(sa.getResults().getAllInstantiatedTypes(), t(A.class), t(Data.class), t(Integer.class)); 173 assertEquals(f(sa, Data.class, "f"), t(Integer.class)); 174 assertEquals(m(sa, A.class, "foo").getFormalParameters()[0], t(A.class)); 175 assertEquals(m(sa, A.class, "foo").getFormalParameters()[1], t(Data.class)); 176 assertEquals(m(sa, A.class, "foo").getFormalReturn(), t(Data.class)); 177 } 178 179 private MethodState m(StaticAnalysis sa, Class<?> declaringClass, String name) { 180 return sa.getResults().lookupMethod(findMethod(declaringClass, name)); 181 } 182 183 private TypeFlow f(StaticAnalysis sa, Class<?> declaringClass, String name) { 184 return sa.getResults().lookupField(findField(declaringClass, name)); 185 } 186 187 private static void assertEquals(TypeFlow actual, Object... expected) { 188 Collection<?> actualTypes = actual.getTypes(); 189 if (actualTypes.size() != expected.length || !actualTypes.containsAll(Arrays.asList(expected))) { 190 Assert.fail(actualTypes + " != " + Arrays.asList(expected)); 191 } 192 } 193 194 private ResolvedJavaType t(Class<?> clazz) { 195 return metaAccess.lookupJavaType(clazz); 196 } 197 198 private ResolvedJavaMethod findMethod(Class<?> declaringClass, String name) { 199 Method reflectionMethod = null; 200 for (Method m : declaringClass.getDeclaredMethods()) { 201 if (m.getName().equals(name)) { 202 assert reflectionMethod == null : "More than one method with name " + name + " in class " + declaringClass.getName(); 203 reflectionMethod = m; 204 } 205 } 206 assert reflectionMethod != null : "No method with name " + name + " in class " + declaringClass.getName(); 207 return metaAccess.lookupJavaMethod(reflectionMethod); 208 } 209 210 private ResolvedJavaField findField(Class<?> declaringClass, String name) { 211 Field reflectionField; 212 try { 213 reflectionField = declaringClass.getDeclaredField(name); 214 } catch (NoSuchFieldException | SecurityException ex) { 215 throw new AssertionError(ex); 216 } 217 return metaAccess.lookupJavaField(reflectionField); 218 } 219 }