1 /* 2 * Copyright (c) 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 24 package org.graalvm.compiler.hotspot.test; 25 26 import org.junit.Test; 27 28 import org.graalvm.compiler.core.test.GraalCompilerTest; 29 import org.graalvm.compiler.debug.Debug; 30 import org.graalvm.compiler.debug.Debug.Scope; 31 import org.graalvm.compiler.graph.Node; 32 import org.graalvm.compiler.nodes.Invoke; 33 import org.graalvm.compiler.nodes.ReturnNode; 34 import org.graalvm.compiler.nodes.StructuredGraph; 35 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 36 37 public class ClassSubstitutionsTests extends GraalCompilerTest { 38 39 public Number instanceField; 40 41 public Object[] arrayField; 42 43 public String[] stringArrayField; 44 45 @SuppressWarnings("try") 46 protected StructuredGraph test(final String snippet) { 47 try (Scope s = Debug.scope("ClassSubstitutionsTest", getMetaAccess().lookupJavaMethod(getMethod(snippet)))) { 48 StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES); 49 compile(graph.method(), graph); 50 assertNotInGraph(graph, Invoke.class); 51 Debug.dump(Debug.BASIC_LOG_LEVEL, graph, snippet); 52 return graph; 53 } catch (Throwable e) { 54 throw Debug.handle(e); 55 } 56 } 57 58 protected static StructuredGraph assertNotInGraph(StructuredGraph graph, Class<?> clazz) { 59 for (Node node : graph.getNodes()) { 60 if (clazz.isInstance(node)) { 61 fail(node.toString()); 62 } 63 } 64 return graph; 65 } 66 67 public boolean constantIsArray() { 68 return "".getClass().isArray(); 69 } 70 71 public boolean constantIsInterface() { 72 return "".getClass().isInterface(); 73 } 74 75 public boolean constantIsPrimitive() { 76 return "".getClass().isPrimitive(); 77 } 78 79 @Test 80 public void testIsArray() { 81 testConstantReturn("constantIsArray", 0); 82 } 83 84 @Test 85 public void testIsInterface() { 86 testConstantReturn("constantIsInterface", 0); 87 } 88 89 @Test 90 public void testIsPrimitive() { 91 testConstantReturn("constantIsPrimitive", 0); 92 } 93 94 public boolean fieldIsNotArray() { 95 if (instanceField != null) { 96 // The base type of instanceField is not Object or an Interface, so it's provably an 97 // instance type, so isArray will always return false. 98 return instanceField.getClass().isArray(); 99 } 100 return false; 101 } 102 103 @Test 104 public void testFieldIsNotArray() { 105 testConstantReturn("fieldIsNotArray", 0); 106 } 107 108 public boolean foldComponentType() { 109 return stringArrayField.getClass().getComponentType() == String.class; 110 } 111 112 @Test 113 public void testFoldComponentType() { 114 testConstantReturn("foldComponentType", 1); 115 } 116 117 @Test 118 public void testFieldIsArray() { 119 testConstantReturn("fieldIsArray", 1); 120 } 121 122 public boolean fieldIsArray() { 123 if (arrayField != null) { 124 // The base type of arrayField is an array of some sort so isArray will always return 125 // true. 126 return arrayField.getClass().isArray(); 127 } 128 return true; 129 } 130 131 private static class A { 132 } 133 134 private static class B extends A { 135 } 136 137 private static class C { 138 } 139 140 private static final A a = new A(); 141 private static final B b = new B(); 142 private static final C c = new C(); 143 144 public boolean classIsAssignable1() { 145 return a.getClass().isAssignableFrom(a.getClass()); 146 } 147 148 public boolean classIsAssignable2() { 149 return a.getClass().isAssignableFrom(b.getClass()); 150 } 151 152 public boolean classIsAssignable3() { 153 return a.getClass().isAssignableFrom(c.getClass()); 154 } 155 156 public boolean classIsAssignable4() { 157 return b.getClass().isAssignableFrom(a.getClass()); 158 } 159 160 public boolean classIsAssignable5() { 161 return c.getClass().isAssignableFrom(b.getClass()); 162 } 163 164 public boolean classIsAssignable6() { 165 return int.class.isAssignableFrom(b.getClass()); 166 } 167 168 public boolean classIsAssignable7() { 169 return int.class.isAssignableFrom(int.class); 170 } 171 172 @Test 173 public void testClassIsAssignable() { 174 testConstantReturn("classIsAssignable1", 1); 175 testConstantReturn("classIsAssignable2", 1); 176 testConstantReturn("classIsAssignable3", 0); 177 testConstantReturn("classIsAssignable4", 0); 178 testConstantReturn("classIsAssignable5", 0); 179 testConstantReturn("classIsAssignable6", 0); 180 testConstantReturn("classIsAssignable7", 1); 181 } 182 183 private void testConstantReturn(String name, Object value) { 184 StructuredGraph result = test(name); 185 ReturnNode ret = result.getNodes(ReturnNode.TYPE).first(); 186 assertDeepEquals(1, result.getNodes(ReturnNode.TYPE).count()); 187 188 assertDeepEquals(true, ret.result().isConstant()); 189 assertDeepEquals(value, ret.result().asJavaConstant().asBoxedPrimitive()); 190 } 191 }