# HG changeset patch # User Doug Simon # Date 1433250958 -7200 # Node ID 381ab4105afe6c78515cda00a674ec1ce2399cc9 # Parent b45e0f7914654be22ee702331e061d4279e4273d moved com.oracle.graal.java.test to com.oracle.jvmci.runtime.test diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/ConstantTest.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/ConstantTest.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.JavaConstant; -import com.oracle.jvmci.meta.Kind; -import org.junit.*; - -public class ConstantTest extends FieldUniverse { - - @Test - public void testNegativeZero() { - Assert.assertTrue("Constant for 0.0f must be different from -0.0f", JavaConstant.FLOAT_0 != JavaConstant.forFloat(-0.0F)); - Assert.assertTrue("Constant for 0.0d must be different from -0.0d", JavaConstant.DOUBLE_0 != JavaConstant.forDouble(-0.0d)); - } - - @Test - public void testNullIsNull() { - Assert.assertTrue(JavaConstant.NULL_POINTER.isNull()); - } - - @Test - public void testOne() { - for (Kind kind : Kind.values()) { - if (kind.isNumericInteger() || kind.isNumericFloat()) { - Assert.assertTrue(JavaConstant.one(kind).getKind() == kind); - } - } - Assert.assertEquals(1, JavaConstant.one(Kind.Int).asInt()); - Assert.assertEquals(1L, JavaConstant.one(Kind.Long).asLong()); - Assert.assertEquals(1, JavaConstant.one(Kind.Byte).asInt()); - Assert.assertEquals(1, JavaConstant.one(Kind.Short).asInt()); - Assert.assertEquals(1, JavaConstant.one(Kind.Char).asInt()); - Assert.assertTrue(1F == JavaConstant.one(Kind.Float).asFloat()); - Assert.assertTrue(1D == JavaConstant.one(Kind.Double).asDouble()); - } - - @Test(expected = IllegalArgumentException.class) - public void testIllegalOne() { - JavaConstant.one(Kind.Illegal); - } - - @Test(expected = IllegalArgumentException.class) - public void testVoidOne() { - JavaConstant.one(Kind.Void); - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/FieldUniverse.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/FieldUniverse.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.ResolvedJavaField; -import java.lang.reflect.*; -import java.util.*; - -/** - * Context for field related tests. - */ -public class FieldUniverse extends TypeUniverse { - - public final Map fields = new HashMap<>(); - - public FieldUniverse() { - for (Class c : classes) { - for (Field f : c.getDeclaredFields()) { - ResolvedJavaField field = metaAccess.lookupJavaField(f); - fields.put(f, field); - } - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/MethodUniverse.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/MethodUniverse.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.ResolvedJavaMethod; -import java.lang.reflect.*; -import java.util.*; - -/** - * Context for method related tests. - */ -public class MethodUniverse extends TypeUniverse { - - public final Map methods = new HashMap<>(); - public final Map, ResolvedJavaMethod> constructors = new HashMap<>(); - - public MethodUniverse() { - for (Class c : classes) { - for (Method m : c.getDeclaredMethods()) { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m); - methods.put(m, method); - } - for (Constructor m : c.getDeclaredConstructors()) { - constructors.put(m, metaAccess.lookupJavaMethod(m)); - } - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/NameAndSignature.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/NameAndSignature.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.ResolvedJavaMethod; -import com.oracle.jvmci.meta.Signature; -import com.oracle.jvmci.meta.ResolvedJavaType; -import com.oracle.jvmci.meta.MetaAccessProvider; -import java.lang.reflect.*; -import java.util.*; - -import com.oracle.graal.api.runtime.*; -import com.oracle.graal.runtime.*; - -class NameAndSignature { - - public static final MetaAccessProvider metaAccess = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders().getMetaAccess(); - - final String name; - final Class returnType; - final Class[] parameterTypes; - - public NameAndSignature(Method m) { - this.name = m.getName(); - this.returnType = m.getReturnType(); - this.parameterTypes = m.getParameterTypes(); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof NameAndSignature) { - NameAndSignature s = (NameAndSignature) obj; - return s.returnType == returnType && name.equals(s.name) && Arrays.equals(s.parameterTypes, parameterTypes); - } - return false; - } - - @Override - public int hashCode() { - return name.hashCode(); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(name + "("); - String sep = ""; - for (Class p : parameterTypes) { - sb.append(sep); - sep = ", "; - sb.append(p.getName()); - } - return sb.append(')').append(returnType.getName()).toString(); - } - - public boolean signatureEquals(ResolvedJavaMethod m) { - Signature s = m.getSignature(); - ResolvedJavaType declaringClass = m.getDeclaringClass(); - if (!s.getReturnType(declaringClass).resolve(declaringClass).equals(metaAccess.lookupJavaType(returnType))) { - return false; - } - if (s.getParameterCount(false) != parameterTypes.length) { - return false; - } - for (int i = 0; i < parameterTypes.length; i++) { - if (!s.getParameterType(i, declaringClass).resolve(declaringClass).equals(metaAccess.lookupJavaType(parameterTypes[i]))) { - return false; - } - } - return true; - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/ResolvedJavaTypeResolveConcreteMethodTest.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/ResolvedJavaTypeResolveConcreteMethodTest.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,175 +0,0 @@ -/* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.ResolvedJavaMethod; -import com.oracle.jvmci.meta.ResolvedJavaType; -import com.oracle.jvmci.meta.MetaAccessProvider; -import static org.junit.Assert.*; - -import org.junit.*; - -import com.oracle.graal.api.runtime.*; -import com.oracle.graal.phases.util.*; -import com.oracle.graal.runtime.*; - -public class ResolvedJavaTypeResolveConcreteMethodTest { - public final MetaAccessProvider metaAccess; - - public ResolvedJavaTypeResolveConcreteMethodTest() { - Providers providers = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders(); - metaAccess = providers.getMetaAccess(); - } - - protected abstract static class A { - @SuppressWarnings("unused") - private void priv() { - } - - public void v1() { - } - - public void v2() { - } - - public abstract void abs(); - } - - protected static class B extends A implements I { - public void i() { - } - - @Override - public void v2() { - } - - @Override - public void abs() { - - } - } - - protected static class C extends B { - public void d() { - } - } - - protected abstract static class D extends A { - - } - - protected static class E extends D { - @Override - public void abs() { - } - } - - protected interface I { - void i(); - - default void d() { - } - } - - @Test - public void testDefaultMethod() { - ResolvedJavaType i = getType(I.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaMethod di = getMethod(i, "d"); - ResolvedJavaMethod dc = getMethod(c, "d"); - - assertEquals(di, i.resolveConcreteMethod(di, c)); - assertEquals(di, b.resolveConcreteMethod(di, c)); - assertEquals(dc, c.resolveConcreteMethod(di, c)); - } - - @Test - public void testPrivateMethod() { - ResolvedJavaType a = getType(A.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaMethod priv = getMethod(a, "priv"); - - assertNull(a.resolveConcreteMethod(priv, c)); - assertNull(b.resolveConcreteMethod(priv, c)); - } - - @Test - public void testAbstractMethod() { - ResolvedJavaType a = getType(A.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaType d = getType(D.class); - ResolvedJavaType e = getType(E.class); - ResolvedJavaMethod absa = getMethod(a, "abs"); - ResolvedJavaMethod absb = getMethod(b, "abs"); - ResolvedJavaMethod abse = getMethod(e, "abs"); - - assertNull(a.resolveConcreteMethod(absa, c)); - assertNull(d.resolveConcreteMethod(absa, c)); - - assertEquals(absb, b.resolveConcreteMethod(absa, c)); - assertEquals(absb, b.resolveConcreteMethod(absb, c)); - assertEquals(absb, c.resolveConcreteMethod(absa, c)); - assertEquals(absb, c.resolveConcreteMethod(absb, c)); - assertEquals(abse, e.resolveConcreteMethod(absa, c)); - assertNull(e.resolveConcreteMethod(absb, c)); - assertEquals(abse, e.resolveConcreteMethod(abse, c)); - } - - @Test - public void testVirtualMethod() { - ResolvedJavaType a = getType(A.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaMethod v1a = getMethod(a, "v1"); - ResolvedJavaMethod v2a = getMethod(a, "v2"); - ResolvedJavaMethod v2b = getMethod(b, "v2"); - - assertEquals(v1a, a.resolveConcreteMethod(v1a, c)); - assertEquals(v1a, b.resolveConcreteMethod(v1a, c)); - assertEquals(v1a, c.resolveConcreteMethod(v1a, c)); - assertEquals(v2a, a.resolveConcreteMethod(v2a, c)); - assertEquals(v2b, b.resolveConcreteMethod(v2a, c)); - assertEquals(v2b, b.resolveConcreteMethod(v2b, c)); - assertEquals(v2b, c.resolveConcreteMethod(v2a, c)); - assertEquals(v2b, c.resolveConcreteMethod(v2b, c)); - - } - - static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) { - for (ResolvedJavaMethod method : type.getDeclaredMethods()) { - if (method.getName().equals(methodName)) { - return method; - } - } - throw new IllegalArgumentException(); - } - - protected ResolvedJavaType getType(Class clazz) { - ResolvedJavaType type = metaAccess.lookupJavaType(clazz); - type.initialize(); - return type; - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/ResolvedJavaTypeResolveMethodTest.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/ResolvedJavaTypeResolveMethodTest.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,175 +0,0 @@ -/* - * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.ResolvedJavaMethod; -import com.oracle.jvmci.meta.ResolvedJavaType; -import com.oracle.jvmci.meta.MetaAccessProvider; -import static org.junit.Assert.*; - -import org.junit.*; - -import com.oracle.graal.api.runtime.*; -import com.oracle.graal.phases.util.*; -import com.oracle.graal.runtime.*; - -public class ResolvedJavaTypeResolveMethodTest { - public final MetaAccessProvider metaAccess; - - public ResolvedJavaTypeResolveMethodTest() { - Providers providers = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders(); - metaAccess = providers.getMetaAccess(); - } - - protected abstract static class A { - @SuppressWarnings("unused") - private void priv() { - } - - public void v1() { - } - - public void v2() { - } - - public abstract void abs(); - } - - protected static class B extends A implements I { - public void i() { - } - - @Override - public void v2() { - } - - @Override - public void abs() { - - } - } - - protected static class C extends B { - public void d() { - } - } - - protected abstract static class D extends A { - - } - - protected static class E extends D { - @Override - public void abs() { - } - } - - protected interface I { - void i(); - - default void d() { - } - } - - @Test - public void testDefaultMethod() { - ResolvedJavaType i = getType(I.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaMethod di = getMethod(i, "d"); - ResolvedJavaMethod dc = getMethod(c, "d"); - - assertEquals(di, i.resolveMethod(di, c, true)); - assertEquals(di, b.resolveMethod(di, c, true)); - assertEquals(dc, c.resolveMethod(di, c, true)); - } - - @Test - public void testPrivateMethod() { - ResolvedJavaType a = getType(A.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaMethod priv = getMethod(a, "priv"); - - assertNull(a.resolveMethod(priv, c, true)); - assertNull(b.resolveMethod(priv, c, true)); - } - - @Test - public void testAbstractMethod() { - ResolvedJavaType a = getType(A.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaType d = getType(D.class); - ResolvedJavaType e = getType(E.class); - ResolvedJavaMethod absa = getMethod(a, "abs"); - ResolvedJavaMethod absb = getMethod(b, "abs"); - ResolvedJavaMethod abse = getMethod(e, "abs"); - - assertEquals(absa, a.resolveMethod(absa, c, true)); - assertEquals(absa, d.resolveMethod(absa, c, true)); - - assertEquals(absb, b.resolveMethod(absa, c, true)); - assertEquals(absb, b.resolveMethod(absb, c, true)); - assertEquals(absb, c.resolveMethod(absa, c, true)); - assertEquals(absb, c.resolveMethod(absb, c, true)); - assertEquals(abse, e.resolveMethod(absa, c, true)); - assertNull(e.resolveMethod(absb, c, true)); - assertEquals(abse, e.resolveMethod(abse, c, true)); - } - - @Test - public void testVirtualMethod() { - ResolvedJavaType a = getType(A.class); - ResolvedJavaType b = getType(B.class); - ResolvedJavaType c = getType(C.class); - ResolvedJavaMethod v1a = getMethod(a, "v1"); - ResolvedJavaMethod v2a = getMethod(a, "v2"); - ResolvedJavaMethod v2b = getMethod(b, "v2"); - - assertEquals(v1a, a.resolveMethod(v1a, c, true)); - assertEquals(v1a, b.resolveMethod(v1a, c, true)); - assertEquals(v1a, c.resolveMethod(v1a, c, true)); - assertEquals(v2a, a.resolveMethod(v2a, c, true)); - assertEquals(v2b, b.resolveMethod(v2a, c, true)); - assertEquals(v2b, b.resolveMethod(v2b, c, true)); - assertEquals(v2b, c.resolveMethod(v2a, c, true)); - assertEquals(v2b, c.resolveMethod(v2b, c, true)); - - } - - static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) { - for (ResolvedJavaMethod method : type.getDeclaredMethods()) { - if (method.getName().equals(methodName)) { - return method; - } - } - throw new IllegalArgumentException(); - } - - protected ResolvedJavaType getType(Class clazz) { - ResolvedJavaType type = metaAccess.lookupJavaType(clazz); - type.initialize(); - return type; - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestBytecodeDisassemblerProvider.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestBytecodeDisassemblerProvider.java Tue Jun 02 15:15:23 2015 +0200 +++ b/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestBytecodeDisassemblerProvider.java Tue Jun 02 15:15:58 2015 +0200 @@ -24,6 +24,8 @@ import com.oracle.jvmci.meta.ResolvedJavaMethod; import com.oracle.jvmci.meta.BytecodeDisassemblerProvider; +import com.oracle.jvmci.runtime.test.*; + import org.junit.*; import com.oracle.graal.java.*; diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestConstantReflectionProvider.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestConstantReflectionProvider.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import static org.junit.Assert.*; - -import java.lang.reflect.*; - -import org.junit.*; - -import com.oracle.jvmci.meta.*; - -/** - * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that - * actually returns non-null results for access operations that are possible, i.e., the tests will - * fail for an implementation that spuriously returns null (which is allowed by the specification). - */ -public class TestConstantReflectionProvider extends TypeUniverse { - - @Test - public void constantEqualsTest() { - for (JavaConstant c1 : constants) { - for (JavaConstant c2 : constants) { - // test symmetry - assertEquals(constantReflection.constantEquals(c1, c2), constantReflection.constantEquals(c2, c1)); - if (c1.getKind() != Kind.Object && c2.getKind() != Kind.Object) { - assertEquals(c1.equals(c2), constantReflection.constantEquals(c2, c1)); - } - } - } - } - - @Test - public void readArrayLengthTest() { - for (JavaConstant c : constants) { - Integer actual = constantReflection.readArrayLength(c); - if (c.getKind() != Kind.Object || c.isNull() || !snippetReflection.asObject(Object.class, c).getClass().isArray()) { - assertNull(actual); - } else { - assertNotNull(actual); - int actualInt = actual; - assertEquals(Array.getLength(snippetReflection.asObject(Object.class, c)), actualInt); - } - } - } - - @Test - public void boxTest() { - for (JavaConstant c : constants) { - JavaConstant boxed = constantReflection.boxPrimitive(c); - if (c.getKind().isPrimitive()) { - assertTrue(boxed.getKind().isObject()); - assertFalse(boxed.isNull()); - } - } - - assertEquals(Long.valueOf(42), snippetReflection.asObject(Long.class, constantReflection.boxPrimitive(JavaConstant.forLong(42)))); - assertEquals(Integer.valueOf(66), snippetReflection.asObject(Integer.class, constantReflection.boxPrimitive(JavaConstant.forInt(66)))); - assertEquals(Byte.valueOf((byte) 123), snippetReflection.asObject(Byte.class, constantReflection.boxPrimitive(JavaConstant.forByte((byte) 123)))); - assertSame(Boolean.TRUE, snippetReflection.asObject(Boolean.class, constantReflection.boxPrimitive(JavaConstant.forBoolean(true)))); - - assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER)); - assertNull(constantReflection.boxPrimitive(snippetReflection.forObject("abc"))); - } - - @Test - public void unboxTest() { - for (JavaConstant c : constants) { - JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c); - if (unboxed != null) { - assertFalse(unboxed.getKind().isObject()); - } - } - - assertEquals(JavaConstant.forLong(42), constantReflection.unboxPrimitive(snippetReflection.forObject(Long.valueOf(42)))); - assertEquals(JavaConstant.forInt(66), constantReflection.unboxPrimitive(snippetReflection.forObject(Integer.valueOf(66)))); - assertEquals(JavaConstant.forByte((byte) 123), constantReflection.unboxPrimitive(snippetReflection.forObject(Byte.valueOf((byte) 123)))); - assertSame(JavaConstant.forBoolean(true), constantReflection.unboxPrimitive(snippetReflection.forObject(Boolean.TRUE))); - - assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER)); - assertNull(constantReflection.unboxPrimitive(snippetReflection.forObject("abc"))); - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestJavaField.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestJavaField.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import static org.junit.Assert.*; - -import java.lang.reflect.*; -import java.util.*; - -import org.junit.*; - -import com.oracle.jvmci.meta.*; - -/** - * Tests for {@link JavaField}. - */ -public class TestJavaField extends FieldUniverse { - - @Test - public void getNameTest() { - for (Map.Entry e : fields.entrySet()) { - String expected = e.getKey().getName(); - String actual = e.getValue().getName(); - assertEquals(expected, actual); - } - } - - @Test - public void getTypeTest() { - for (Map.Entry e : fields.entrySet()) { - // Must resolve types first as a resolved types != unresolved types - ResolvedJavaField rf = e.getValue(); - JavaType expected = metaAccess.lookupJavaType(e.getKey().getType()).resolve(rf.getDeclaringClass()); - JavaType actual = rf.getType().resolve(rf.getDeclaringClass()); - assertEquals(expected, actual); - } - } - - @Test - public void getKindTest() { - for (Map.Entry e : fields.entrySet()) { - Kind expected = metaAccess.lookupJavaType(e.getKey().getType()).getKind(); - Kind actual = e.getValue().getKind(); - assertEquals(expected, actual); - } - } - - @Test - public void getDeclaringClassTest() { - for (Map.Entry e : fields.entrySet()) { - Class expected = e.getKey().getDeclaringClass(); - ResolvedJavaType actual = e.getValue().getDeclaringClass(); - assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestJavaMethod.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestJavaMethod.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import static org.junit.Assert.*; - -import java.lang.reflect.*; -import java.util.*; - -import org.junit.*; - -import com.oracle.jvmci.meta.*; - -/** - * Tests for {@link JavaMethod}. - */ -public class TestJavaMethod extends MethodUniverse { - - @Test - public void getNameTest() { - for (Map.Entry e : methods.entrySet()) { - String expected = e.getKey().getName(); - String actual = e.getValue().getName(); - assertEquals(expected, actual); - } - } - - @Test - public void getDeclaringClassTest() { - for (Map.Entry e : methods.entrySet()) { - Class expected = e.getKey().getDeclaringClass(); - ResolvedJavaType actual = e.getValue().getDeclaringClass(); - assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); - } - } - - @Test - public void getSignatureTest() { - for (Map.Entry e : methods.entrySet()) { - assertTrue(new NameAndSignature(e.getKey()).signatureEquals(e.getValue())); - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestJavaType.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestJavaType.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.JavaType; -import com.oracle.jvmci.meta.Kind; -import static org.junit.Assert.*; - -import org.junit.*; - -/** - * Tests for {@link JavaType}. - */ -public class TestJavaType extends TypeUniverse { - - public TestJavaType() { - } - - @Test - public void getKindTest() { - for (Class c : classes) { - JavaType type = metaAccess.lookupJavaType(c); - Kind expected = Kind.fromJavaClass(c); - Kind actual = type.getKind(); - assertEquals(expected, actual); - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestMetaAccessProvider.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestMetaAccessProvider.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import static com.oracle.jvmci.meta.MetaUtil.*; -import static org.junit.Assert.*; - -import java.lang.reflect.*; - -import org.junit.*; - -import com.oracle.jvmci.meta.*; - -/** - * Tests for {@link MetaAccessProvider}. - */ -public class TestMetaAccessProvider extends TypeUniverse { - - @Test - public void lookupJavaTypeTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - assertNotNull(type); - assertEquals(c.getModifiers(), type.getModifiers()); - if (!type.isArray()) { - assertEquals(type.getName(), toInternalName(c.getName())); - assertEquals(type.toJavaName(), c.getName()); - } - } - } - - @Test - public void lookupJavaMethodTest() { - for (Class c : classes) { - for (Method reflect : c.getDeclaredMethods()) { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(reflect); - assertNotNull(method); - int expected = reflect.getModifiers() & Modifier.methodModifiers(); - int actual = method.getModifiers(); - assertEquals(String.format("%s: 0x%x != 0x%x", reflect, expected, actual), expected, actual); - assertTrue(method.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass()))); - } - } - } - - @Test - public void lookupJavaFieldTest() { - for (Class c : classes) { - for (Field reflect : c.getDeclaredFields()) { - ResolvedJavaField field = metaAccess.lookupJavaField(reflect); - assertNotNull(field); - int expected = reflect.getModifiers(); - int actual = field.getModifiers(); - assertEquals(String.format("%s: 0x%x != 0x%x", reflect, expected, actual), expected, actual); - assertTrue(field.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass()))); - } - } - } - - @Test - public void lookupJavaTypeConstantTest() { - for (JavaConstant c : constants) { - if (c.getKind() == Kind.Object && !c.isNull()) { - Object o = snippetReflection.asObject(Object.class, c); - ResolvedJavaType type = metaAccess.lookupJavaType(c); - assertNotNull(type); - assertTrue(type.equals(metaAccess.lookupJavaType(o.getClass()))); - } else { - assertEquals(metaAccess.lookupJavaType(c), null); - } - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestResolvedJavaField.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestResolvedJavaField.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import static org.junit.Assert.*; - -import java.lang.annotation.*; -import java.lang.reflect.*; -import java.util.*; - -import org.junit.*; - -import com.oracle.jvmci.meta.*; - -/** - * Tests for {@link ResolvedJavaField}. - */ -public class TestResolvedJavaField extends FieldUniverse { - - public TestResolvedJavaField() { - } - - @Test - public void getModifiersTest() { - for (Map.Entry e : fields.entrySet()) { - int expected = e.getKey().getModifiers(); - int actual = e.getValue().getModifiers(); - assertEquals(expected, actual); - } - } - - @Test - public void isSyntheticTest() { - for (Map.Entry e : fields.entrySet()) { - boolean expected = e.getKey().isSynthetic(); - boolean actual = e.getValue().isSynthetic(); - assertEquals(expected, actual); - } - } - - @Test - public void getAnnotationTest() { - for (Map.Entry e : fields.entrySet()) { - for (Annotation expected : e.getKey().getAnnotations()) { - if (expected != null) { - Annotation actual = e.getValue().getAnnotation(expected.annotationType()); - assertEquals(expected, actual); - } - } - } - } - - @Test - public void getLocationIdentityTest() { - for (Map.Entry e : fields.entrySet()) { - LocationIdentity identity = e.getValue().getLocationIdentity(); - assertTrue(identity != null); - } - } - - @Test - public void readConstantValueTest() throws NoSuchFieldException { - ResolvedJavaField field = metaAccess.lookupJavaField(getClass().getDeclaredField("stringField")); - for (Object receiver : new Object[]{this, null, new String()}) { - JavaConstant value = constantReflection.readConstantFieldValue(field, snippetReflection.forObject(receiver)); - assertNull(value); - } - - ResolvedJavaField constField = metaAccess.lookupJavaField(getClass().getDeclaredField("constantStringField")); - for (Object receiver : new Object[]{this, null, new String()}) { - JavaConstant value = constantReflection.readConstantFieldValue(constField, snippetReflection.forObject(receiver)); - if (value != null) { - Object expected = "constantField"; - assertTrue(snippetReflection.asObject(Object.class, value) == expected); - } - } - } - - String stringField = "field"; - final String constantStringField = "constantField"; - - private Method findTestMethod(Method apiMethod) { - String testName = apiMethod.getName() + "Test"; - for (Method m : getClass().getDeclaredMethods()) { - if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { - return m; - } - } - return null; - } - - // @formatter:off - private static final String[] untestedApiMethods = { - "getDeclaringClass", - "isInternal" - }; - // @formatter:on - - /** - * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written - * for them or are added to {@link #untestedApiMethods}. - */ - @Test - public void testCoverage() { - Set known = new HashSet<>(Arrays.asList(untestedApiMethods)); - for (Method m : ResolvedJavaField.class.getDeclaredMethods()) { - if (m.isSynthetic()) { - continue; - } - if (findTestMethod(m) == null) { - assertTrue("test missing for " + m, known.contains(m.getName())); - } else { - assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); - } - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestResolvedJavaMethod.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestResolvedJavaMethod.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,381 +0,0 @@ -/* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.ExceptionHandler; -import com.oracle.jvmci.meta.ResolvedJavaMethod; -import com.oracle.jvmci.meta.ConstantPool; -import static org.junit.Assert.*; - -import java.lang.annotation.*; -import java.lang.reflect.*; -import java.util.*; - -import org.junit.*; - -/** - * Tests for {@link ResolvedJavaMethod}. - */ -public class TestResolvedJavaMethod extends MethodUniverse { - - public TestResolvedJavaMethod() { - } - - /** - * @see ResolvedJavaMethod#getCode() - */ - @Test - public void getCodeTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - byte[] code = m.getCode(); - if (code == null) { - assertTrue(m.getCodeSize() == 0); - } else { - if (m.isAbstract()) { - assertTrue(code.length == 0); - } else if (!m.isNative()) { - assertTrue(code.length > 0); - } - } - } - } - - /** - * @see ResolvedJavaMethod#getCodeSize() - */ - @Test - public void getCodeSizeTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - int codeSize = m.getCodeSize(); - if (m.isAbstract()) { - assertTrue(codeSize == 0); - } else if (!m.isNative()) { - assertTrue(codeSize > 0); - } - } - } - - @Test - public void getModifiersTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - int expected = e.getKey().getModifiers() & Modifier.methodModifiers(); - int actual = m.getModifiers(); - assertEquals(expected, actual); - } - } - - /** - * @see ResolvedJavaMethod#isClassInitializer() - */ - @Test - public void isClassInitializerTest() { - for (Map.Entry e : methods.entrySet()) { - // Class initializers are hidden from reflection - ResolvedJavaMethod m = e.getValue(); - assertFalse(m.isClassInitializer()); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertFalse(m.isClassInitializer()); - } - } - - @Test - public void isConstructorTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertFalse(m.isConstructor()); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertTrue(m.isConstructor()); - } - } - - @Test - public void isSyntheticTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertEquals(e.getKey().isSynthetic(), m.isSynthetic()); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertEquals(e.getKey().isSynthetic(), m.isSynthetic()); - } - } - - @Test - public void isSynchronizedTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertEquals(Modifier.isSynchronized(e.getKey().getModifiers()), m.isSynchronized()); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertEquals(Modifier.isSynchronized(e.getKey().getModifiers()), m.isSynchronized()); - } - } - - @Test - public void canBeStaticallyBoundTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertEquals(m.canBeStaticallyBound(), canBeStaticallyBound(e.getKey())); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertEquals(m.canBeStaticallyBound(), canBeStaticallyBound(e.getKey())); - } - } - - private static boolean canBeStaticallyBound(Member method) { - int modifiers = method.getModifiers(); - return (Modifier.isFinal(modifiers) || Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers) || Modifier.isFinal(method.getDeclaringClass().getModifiers())) && - !Modifier.isAbstract(modifiers); - } - - private static String methodWithExceptionHandlers(String p1, Object o2) { - try { - return p1.substring(100) + o2.toString(); - } catch (IndexOutOfBoundsException e) { - e.printStackTrace(); - } catch (NullPointerException e) { - e.printStackTrace(); - } catch (RuntimeException e) { - e.printStackTrace(); - } - return null; - } - - @Test - public void getExceptionHandlersTest() throws NoSuchMethodException { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithExceptionHandlers", String.class, Object.class)); - ExceptionHandler[] handlers = method.getExceptionHandlers(); - assertNotNull(handlers); - assertEquals(handlers.length, 3); - handlers[0].getCatchType().equals(metaAccess.lookupJavaType(IndexOutOfBoundsException.class)); - handlers[1].getCatchType().equals(metaAccess.lookupJavaType(NullPointerException.class)); - handlers[2].getCatchType().equals(metaAccess.lookupJavaType(RuntimeException.class)); - } - - private static String nullPointerExceptionOnFirstLine(Object o, String ignored) { - return o.toString() + ignored; - } - - @Test - public void asStackTraceElementTest() throws NoSuchMethodException { - try { - nullPointerExceptionOnFirstLine(null, "ignored"); - Assert.fail("should not reach here"); - } catch (NullPointerException e) { - StackTraceElement expected = e.getStackTrace()[0]; - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("nullPointerExceptionOnFirstLine", Object.class, String.class)); - StackTraceElement actual = method.asStackTraceElement(0); - assertEquals(expected, actual); - } - } - - @Test - public void getConstantPoolTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - ConstantPool cp = m.getConstantPool(); - assertTrue(cp.length() > 0); - } - } - - @Test(timeout = 1000L) - public void getAnnotationTest() throws NoSuchMethodException { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("getAnnotationTest")); - Test annotation = method.getAnnotation(Test.class); - assertNotNull(annotation); - assertEquals(1000L, annotation.timeout()); - } - - @Test(timeout = 1000L) - public void getAnnotationsTest() throws NoSuchMethodException { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("getAnnotationsTest")); - Annotation[] annotations = method.getAnnotations(); - assertNotNull(annotations); - assertEquals(1, annotations.length); - assertEquals(1000L, ((Test) annotations[0]).timeout()); - } - - @Retention(RetentionPolicy.RUNTIME) - @Target(ElementType.PARAMETER) - @interface NonNull { - } - - @Retention(RetentionPolicy.RUNTIME) - @Target(ElementType.PARAMETER) - @interface Special { - } - - private static native void methodWithAnnotatedParameters(@NonNull HashMap p1, @Special @NonNull Class p2); - - @Test - public void getParameterAnnotationsTest() throws NoSuchMethodException { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); - Annotation[][] annotations = method.getParameterAnnotations(); - assertEquals(2, annotations.length); - assertEquals(1, annotations[0].length); - assertEquals(NonNull.class, annotations[0][0].annotationType()); - assertEquals(2, annotations[1].length); - assertEquals(Special.class, annotations[1][0].annotationType()); - assertEquals(NonNull.class, annotations[1][1].annotationType()); - } - - @Test - public void getGenericParameterTypesTest() throws NoSuchMethodException { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); - Type[] genericParameterTypes = method.getGenericParameterTypes(); - assertEquals(2, genericParameterTypes.length); - assertEquals("java.util.HashMap", genericParameterTypes[0].toString()); - assertEquals("java.lang.Class", genericParameterTypes[1].toString()); - } - - @Test - public void getMaxLocalsTest() throws NoSuchMethodException { - ResolvedJavaMethod method1 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); - ResolvedJavaMethod method2 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("nullPointerExceptionOnFirstLine", Object.class, String.class)); - assertEquals(0, method1.getMaxLocals()); - assertEquals(2, method2.getMaxLocals()); - - } - - @Test - public void getMaxStackSizeTest() throws NoSuchMethodException { - ResolvedJavaMethod method1 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); - ResolvedJavaMethod method2 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("nullPointerExceptionOnFirstLine", Object.class, String.class)); - assertEquals(0, method1.getMaxStackSize()); - // some versions of javac produce bytecode with a stacksize of 2 for this method - // JSR 292 also sometimes need one more stack slot - int method2StackSize = method2.getMaxStackSize(); - assertTrue(2 <= method2StackSize && method2StackSize <= 4); - } - - @Test - public void isDefaultTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertEquals(e.getKey().isDefault(), m.isDefault()); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertFalse(m.isDefault()); - } - } - - @Test - public void hasReceiverTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertTrue(m.hasReceiver() != Modifier.isStatic(e.getKey().getModifiers())); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertTrue(m.hasReceiver()); - } - } - - @Test - public void hasBytecodesTest() { - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertTrue(m.hasBytecodes() == (m.isConcrete() && !m.isNative())); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertTrue(m.hasBytecodes()); - } - } - - @Test - public void isJavaLangObjectInitTest() throws NoSuchMethodException { - ResolvedJavaMethod method = metaAccess.lookupJavaMethod(Object.class.getConstructor()); - assertTrue(method.isJavaLangObjectInit()); - for (Map.Entry e : methods.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - assertFalse(m.isJavaLangObjectInit()); - } - for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { - ResolvedJavaMethod m = e.getValue(); - Constructor key = e.getKey(); - if (key.getDeclaringClass() == Object.class && key.getParameters().length == 0) { - assertTrue(m.isJavaLangObjectInit()); - } else { - assertFalse(m.isJavaLangObjectInit()); - } - } - } - - private Method findTestMethod(Method apiMethod) { - String testName = apiMethod.getName() + "Test"; - for (Method m : getClass().getDeclaredMethods()) { - if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { - return m; - } - } - return null; - } - - // @formatter:off - private static final String[] untestedApiMethods = { - "invoke", - "newInstance", - "getDeclaringClass", - "getEncoding", - "getProfilingInfo", - "reprofile", - "getCompilerStorage", - "canBeInlined", - "shouldBeInlined", - "getLineNumberTable", - "getLocalVariableTable", - "isInVirtualMethodTable", - "toParameterTypes", - "getParameterAnnotation", - "getSpeculationLog", - "$jacocoInit" - }; - // @formatter:on - - /** - * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written - * for them or are added to {@link #untestedApiMethods}. - */ - @Test - public void testCoverage() { - Set known = new HashSet<>(Arrays.asList(untestedApiMethods)); - for (Method m : ResolvedJavaMethod.class.getDeclaredMethods()) { - if (findTestMethod(m) == null) { - assertTrue("test missing for " + m, known.contains(m.getName())); - } else { - assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); - } - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestResolvedJavaType.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TestResolvedJavaType.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,879 +0,0 @@ -/* - * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import com.oracle.jvmci.meta.JavaConstant; -import com.oracle.jvmci.meta.Kind; -import com.oracle.jvmci.meta.ResolvedJavaMethod; -import com.oracle.jvmci.meta.ResolvedJavaField; -import com.oracle.jvmci.meta.TrustedInterface; -import com.oracle.jvmci.meta.ResolvedJavaType; -import static java.lang.reflect.Modifier.*; -import static org.junit.Assert.*; - -import java.lang.annotation.*; -import java.lang.reflect.*; -import java.net.*; -import java.util.*; - -import org.junit.*; - -import sun.reflect.ConstantPool; - -import com.oracle.jvmci.meta.Assumptions.AssumptionResult; -import com.oracle.jvmci.common.*; - -/** - * Tests for {@link ResolvedJavaType}. - */ -public class TestResolvedJavaType extends TypeUniverse { - - public TestResolvedJavaType() { - } - - @Test - public void findInstanceFieldWithOffsetTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - Set reflectionFields = getInstanceFields(c, true); - for (Field f : reflectionFields) { - ResolvedJavaField rf = lookupField(type.getInstanceFields(true), f); - assertNotNull(rf); - long offset = isStatic(f.getModifiers()) ? unsafe.staticFieldOffset(f) : unsafe.objectFieldOffset(f); - ResolvedJavaField result = type.findInstanceFieldWithOffset(offset, rf.getKind()); - assertNotNull(result); - assertTrue(fieldsEqual(f, result)); - } - } - } - - @Test - public void isInterfaceTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - boolean expected = c.isInterface(); - boolean actual = type.isInterface(); - assertEquals(expected, actual); - } - } - - @Test - public void isInstanceClassTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - boolean expected = !c.isArray() && !c.isPrimitive() && !c.isInterface(); - boolean actual = type.isInstanceClass(); - assertEquals(expected, actual); - } - } - - @Test - public void isArrayTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - boolean expected = c.isArray(); - boolean actual = type.isArray(); - assertEquals(expected, actual); - } - } - - @Test - public void getModifiersTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - int expected = c.getModifiers(); - int actual = type.getModifiers(); - assertEquals(expected, actual); - } - } - - @Test - public void isAssignableFromTest() { - Class[] all = classes.toArray(new Class[classes.size()]); - for (int i = 0; i < all.length; i++) { - Class c1 = all[i]; - for (int j = i; j < all.length; j++) { - Class c2 = all[j]; - ResolvedJavaType t1 = metaAccess.lookupJavaType(c1); - ResolvedJavaType t2 = metaAccess.lookupJavaType(c2); - boolean expected = c1.isAssignableFrom(c2); - boolean actual = t1.isAssignableFrom(t2); - assertEquals(expected, actual); - if (expected && t1 != t2) { - assertFalse(t2.isAssignableFrom(t1)); - } - } - } - } - - @Test - public void isInstanceTest() { - for (JavaConstant c : constants) { - if (c.getKind() == Kind.Object && !c.isNull()) { - Object o = snippetReflection.asObject(Object.class, c); - Class cls = o.getClass(); - while (cls != null) { - ResolvedJavaType type = metaAccess.lookupJavaType(cls); - boolean expected = cls.isInstance(o); - boolean actual = type.isInstance(c); - assertEquals(expected, actual); - cls = cls.getSuperclass(); - } - } - } - } - - private static Class asExactClass(Class c) { - if (c.isArray()) { - if (asExactClass(c.getComponentType()) != null) { - return c; - } - } else { - if (c.isPrimitive() || Modifier.isFinal(c.getModifiers())) { - return c; - } - } - return null; - } - - @Test - public void asExactTypeTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - ResolvedJavaType exactType = type.asExactType(); - Class expected = asExactClass(c); - if (expected == null) { - assertTrue("exact(" + c.getName() + ") != null", exactType == null); - } else { - assertNotNull(exactType); - assertTrue(exactType.equals(metaAccess.lookupJavaType(expected))); - } - } - } - - @Test - public void getSuperclassTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - Class expected = c.getSuperclass(); - ResolvedJavaType actual = type.getSuperclass(); - if (expected == null) { - assertTrue(actual == null); - } else { - assertNotNull(actual); - assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); - } - } - } - - @Test - public void getInterfacesTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - Class[] expected = c.getInterfaces(); - ResolvedJavaType[] actual = type.getInterfaces(); - assertEquals(expected.length, actual.length); - for (int i = 0; i < expected.length; i++) { - assertTrue(actual[i].equals(metaAccess.lookupJavaType(expected[i]))); - } - } - } - - public Class getSupertype(Class c) { - assert !c.isPrimitive(); - if (c.isArray()) { - Class componentType = c.getComponentType(); - if (componentType.isPrimitive() || componentType == Object.class) { - return Object.class; - } - return getArrayClass(getSupertype(componentType)); - } - if (c.isInterface()) { - return Object.class; - } - return c.getSuperclass(); - } - - public Class findLeastCommonAncestor(Class c1Initial, Class c2Initial) { - if (c1Initial.isPrimitive() || c2Initial.isPrimitive()) { - return null; - } else { - Class c1 = c1Initial; - Class c2 = c2Initial; - while (true) { - if (c1.isAssignableFrom(c2)) { - return c1; - } - if (c2.isAssignableFrom(c1)) { - return c2; - } - c1 = getSupertype(c1); - c2 = getSupertype(c2); - } - } - } - - @Test - public void findLeastCommonAncestorTest() { - Class[] all = classes.toArray(new Class[classes.size()]); - for (int i = 0; i < all.length; i++) { - Class c1 = all[i]; - for (int j = i; j < all.length; j++) { - Class c2 = all[j]; - ResolvedJavaType t1 = metaAccess.lookupJavaType(c1); - ResolvedJavaType t2 = metaAccess.lookupJavaType(c2); - Class expected = findLeastCommonAncestor(c1, c2); - ResolvedJavaType actual = t1.findLeastCommonAncestor(t2); - if (expected == null) { - assertTrue(actual == null); - } else { - assertNotNull(actual); - assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); - } - } - } - } - - private static class Base { - } - - abstract static class Abstract1 extends Base { - } - - interface Interface1 { - } - - static class Concrete1 extends Abstract1 { - } - - static class Concrete2 extends Abstract1 implements Interface1 { - } - - static class Concrete3 extends Concrete2 { - } - - static final class Final1 extends Abstract1 { - } - - abstract static class Abstract4 extends Concrete3 { - } - - void checkConcreteSubtype(ResolvedJavaType type, ResolvedJavaType expected) { - AssumptionResult leafConcreteSubtype = type.findLeafConcreteSubtype(); - if (leafConcreteSubtype == null) { - // findLeafConcreteSubtype() is conservative - } else { - if (expected == null) { - assertNull(leafConcreteSubtype); - } else { - assertTrue(leafConcreteSubtype.getResult().equals(expected)); - } - } - - if (!type.isArray()) { - ResolvedJavaType arrayType = type.getArrayClass(); - AssumptionResult arraySubtype = arrayType.findLeafConcreteSubtype(); - if (arraySubtype != null) { - assertEquals(arraySubtype.getResult(), arrayType); - } else { - // findLeafConcreteSubtype() method is conservative - } - } - } - - @Test - public void findLeafConcreteSubtypeTest() { - ResolvedJavaType base = metaAccess.lookupJavaType(Base.class); - checkConcreteSubtype(base, base); - - ResolvedJavaType a1 = metaAccess.lookupJavaType(Abstract1.class); - ResolvedJavaType c1 = metaAccess.lookupJavaType(Concrete1.class); - - checkConcreteSubtype(base, null); - checkConcreteSubtype(a1, c1); - checkConcreteSubtype(c1, c1); - - ResolvedJavaType i1 = metaAccess.lookupJavaType(Interface1.class); - ResolvedJavaType c2 = metaAccess.lookupJavaType(Concrete2.class); - - checkConcreteSubtype(base, null); - checkConcreteSubtype(a1, null); - checkConcreteSubtype(c1, c1); - checkConcreteSubtype(i1, c2); - checkConcreteSubtype(c2, c2); - - ResolvedJavaType c3 = metaAccess.lookupJavaType(Concrete3.class); - checkConcreteSubtype(c2, null); - checkConcreteSubtype(c3, c3); - - ResolvedJavaType a4 = metaAccess.lookupJavaType(Abstract4.class); - checkConcreteSubtype(c3, null); - checkConcreteSubtype(a4, null); - - ResolvedJavaType a1a = metaAccess.lookupJavaType(Abstract1[].class); - checkConcreteSubtype(a1a, null); - ResolvedJavaType c1a = metaAccess.lookupJavaType(Concrete1[].class); - checkConcreteSubtype(c1a, null); - ResolvedJavaType f1a = metaAccess.lookupJavaType(Final1[].class); - checkConcreteSubtype(f1a, f1a); - - ResolvedJavaType obja = metaAccess.lookupJavaType(Object[].class); - checkConcreteSubtype(obja, null); - - ResolvedJavaType inta = metaAccess.lookupJavaType(int[].class); - checkConcreteSubtype(inta, inta); - } - - interface NoImplementor { - } - - interface SingleImplementorInterface { - } - - static class SingleConcreteImplementor implements SingleImplementorInterface { - } - - interface SingleAbstractImplementorInterface { - } - - abstract static class SingleAbstractImplementor implements SingleAbstractImplementorInterface { - } - - interface MultiImplementorInterface { - } - - static class ConcreteImplementor1 implements MultiImplementorInterface { - } - - static class ConcreteImplementor2 implements MultiImplementorInterface { - } - - interface MultipleAbstractImplementorInterface { - } - - abstract static class MultiAbstractImplementor1 implements MultipleAbstractImplementorInterface { - } - - abstract static class MultiAbstractImplementor2 implements MultipleAbstractImplementorInterface { - } - - interface SingleAbstractImplementorInterface2 { - } - - interface ExtendedSingleImplementorInterface { - } - - abstract static class SingleAbstractImplementor2 implements SingleAbstractImplementorInterface2 { - } - - static class ConcreteTransitiveImplementor1 extends SingleAbstractImplementor2 implements ExtendedSingleImplementorInterface { - } - - static class ConcreteTransitiveImplementor2 extends SingleAbstractImplementor2 implements ExtendedSingleImplementorInterface { - } - - @Test - public void getSingleImplementorTest() { - ResolvedJavaType iNi = metaAccess.lookupJavaType(NoImplementor.class); - assertNull(iNi.getSingleImplementor()); - - ResolvedJavaType iSi = metaAccess.lookupJavaType(SingleImplementorInterface.class); - ResolvedJavaType cSi = metaAccess.lookupJavaType(SingleConcreteImplementor.class); - assertEquals(cSi, iSi.getSingleImplementor()); - - ResolvedJavaType iSai = metaAccess.lookupJavaType(SingleAbstractImplementorInterface.class); - ResolvedJavaType aSai = metaAccess.lookupJavaType(SingleAbstractImplementor.class); - assertEquals(aSai, iSai.getSingleImplementor()); - - ResolvedJavaType iMi = metaAccess.lookupJavaType(MultiImplementorInterface.class); - metaAccess.lookupJavaType(ConcreteImplementor1.class); - metaAccess.lookupJavaType(ConcreteImplementor2.class); - assertEquals(iMi, iMi.getSingleImplementor()); - - ResolvedJavaType iMai = metaAccess.lookupJavaType(MultipleAbstractImplementorInterface.class); - metaAccess.lookupJavaType(MultiAbstractImplementor1.class); - metaAccess.lookupJavaType(MultiAbstractImplementor2.class); - assertEquals(iMai, iMai.getSingleImplementor()); - - ResolvedJavaType iSai2 = metaAccess.lookupJavaType(SingleAbstractImplementorInterface2.class); - ResolvedJavaType aSai2 = metaAccess.lookupJavaType(SingleAbstractImplementor2.class); - metaAccess.lookupJavaType(ConcreteTransitiveImplementor1.class); - metaAccess.lookupJavaType(ConcreteTransitiveImplementor2.class); - assertEquals(aSai2, iSai2.getSingleImplementor()); - } - - @Test(expected = JVMCIError.class) - public void getSingleImplementorTestClassReceiver() { - ResolvedJavaType base = metaAccess.lookupJavaType(Base.class); - base.getSingleImplementor(); - } - - @Test(expected = JVMCIError.class) - public void getSingleImplementorTestPrimitiveReceiver() { - ResolvedJavaType primitive = metaAccess.lookupJavaType(int.class); - primitive.getSingleImplementor(); - } - - @Test - public void getComponentTypeTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - Class expected = c.getComponentType(); - ResolvedJavaType actual = type.getComponentType(); - if (expected == null) { - assertNull(actual); - } else { - assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); - } - } - } - - @Test - public void getArrayClassTest() { - for (Class c : classes) { - if (c != void.class) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - Class expected = getArrayClass(c); - ResolvedJavaType actual = type.getArrayClass(); - assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); - } - } - } - - static class Declarations { - - final Method implementation; - final Set declarations; - - public Declarations(Method impl) { - this.implementation = impl; - declarations = new HashSet<>(); - } - } - - /** - * See Method - * overriding. - */ - static boolean isOverriderOf(Method impl, Method m) { - if (!isPrivate(m.getModifiers()) && !isFinal(m.getModifiers())) { - if (m.getName().equals(impl.getName())) { - if (m.getReturnType() == impl.getReturnType()) { - if (Arrays.equals(m.getParameterTypes(), impl.getParameterTypes())) { - if (isPublic(m.getModifiers()) || isProtected(m.getModifiers())) { - // m is public or protected - return isPublic(impl.getModifiers()) || isProtected(impl.getModifiers()); - } else { - // m is package-private - return impl.getDeclaringClass().getPackage() == m.getDeclaringClass().getPackage(); - } - } - } - } - } - return false; - } - - static final Map, VTable> vtables = new HashMap<>(); - - static class VTable { - - final Map methods = new HashMap<>(); - } - - static synchronized VTable getVTable(Class c) { - VTable vtable = vtables.get(c); - if (vtable == null) { - vtable = new VTable(); - if (c != Object.class) { - VTable superVtable = getVTable(c.getSuperclass()); - vtable.methods.putAll(superVtable.methods); - } - for (Method m : c.getDeclaredMethods()) { - if (!isStatic(m.getModifiers()) && !isPrivate(m.getModifiers())) { - if (isAbstract(m.getModifiers())) { - // A subclass makes a concrete method in a superclass abstract - vtable.methods.remove(new NameAndSignature(m)); - } else { - vtable.methods.put(new NameAndSignature(m), m); - } - } - } - vtables.put(c, vtable); - } - return vtable; - } - - static Set findDeclarations(Method impl, Class c) { - Set declarations = new HashSet<>(); - NameAndSignature implSig = new NameAndSignature(impl); - if (c != null) { - for (Method m : c.getDeclaredMethods()) { - if (new NameAndSignature(m).equals(implSig)) { - declarations.add(m); - break; - } - } - if (!c.isInterface()) { - declarations.addAll(findDeclarations(impl, c.getSuperclass())); - } - for (Class i : c.getInterfaces()) { - declarations.addAll(findDeclarations(impl, i)); - } - } - return declarations; - } - - private static void checkResolveMethod(ResolvedJavaType type, ResolvedJavaType context, ResolvedJavaMethod decl, ResolvedJavaMethod expected) { - ResolvedJavaMethod impl = type.resolveConcreteMethod(decl, context); - assertEquals(expected, impl); - } - - @Test - public void resolveMethodTest() { - ResolvedJavaType context = metaAccess.lookupJavaType(TestResolvedJavaType.class); - for (Class c : classes) { - if (c.isInterface() || c.isPrimitive()) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - for (Method m : c.getDeclaredMethods()) { - if (JAVA_VERSION <= 1.7D || (!isStatic(m.getModifiers()) && !isPrivate(m.getModifiers()))) { - ResolvedJavaMethod resolved = metaAccess.lookupJavaMethod(m); - ResolvedJavaMethod impl = type.resolveMethod(resolved, context, true); - ResolvedJavaMethod expected = resolved.isDefault() || resolved.isAbstract() ? resolved : null; - assertEquals(m.toString(), expected, impl); - } else { - // As of JDK 8, interfaces can have static and private methods - } - } - } else { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - VTable vtable = getVTable(c); - for (Method impl : vtable.methods.values()) { - Set decls = findDeclarations(impl, c); - for (Method decl : decls) { - ResolvedJavaMethod m = metaAccess.lookupJavaMethod(decl); - if (m.isPublic()) { - ResolvedJavaMethod i = metaAccess.lookupJavaMethod(impl); - checkResolveMethod(type, context, m, i); - } - } - } - } - } - } - - @Test - public void resolveConcreteMethodTest() { - ResolvedJavaType context = metaAccess.lookupJavaType(TestResolvedJavaType.class); - for (Class c : classes) { - if (c.isInterface() || c.isPrimitive()) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - for (Method m : c.getDeclaredMethods()) { - if (JAVA_VERSION <= 1.7D || (!isStatic(m.getModifiers()) && !isPrivate(m.getModifiers()))) { - ResolvedJavaMethod resolved = metaAccess.lookupJavaMethod(m); - ResolvedJavaMethod impl = type.resolveConcreteMethod(resolved, context); - ResolvedJavaMethod expected = resolved.isDefault() ? resolved : null; - assertEquals(m.toString(), expected, impl); - } else { - // As of JDK 8, interfaces can have static and private methods - } - } - } else { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - VTable vtable = getVTable(c); - for (Method impl : vtable.methods.values()) { - Set decls = findDeclarations(impl, c); - for (Method decl : decls) { - ResolvedJavaMethod m = metaAccess.lookupJavaMethod(decl); - if (m.isPublic()) { - ResolvedJavaMethod i = metaAccess.lookupJavaMethod(impl); - checkResolveMethod(type, context, m, i); - } - } - } - for (Method m : c.getDeclaredMethods()) { - ResolvedJavaMethod impl = type.resolveConcreteMethod(metaAccess.lookupJavaMethod(m), context); - ResolvedJavaMethod expected = isAbstract(m.getModifiers()) ? null : impl; - assertEquals(type + " " + m.toString(), expected, impl); - } - } - } - } - - @Test - public void findUniqueConcreteMethodTest() throws NoSuchMethodException { - ResolvedJavaMethod thisMethod = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("findUniqueConcreteMethodTest")); - ResolvedJavaMethod ucm = metaAccess.lookupJavaType(getClass()).findUniqueConcreteMethod(thisMethod).getResult(); - assertEquals(thisMethod, ucm); - } - - public static Set getInstanceFields(Class c, boolean includeSuperclasses) { - if (c.isArray() || c.isPrimitive() || c.isInterface()) { - return Collections.emptySet(); - } - Set result = new HashSet<>(); - for (Field f : c.getDeclaredFields()) { - if (!Modifier.isStatic(f.getModifiers())) { - result.add(f); - } - } - if (includeSuperclasses && c != Object.class) { - result.addAll(getInstanceFields(c.getSuperclass(), true)); - } - return result; - } - - public static Set getStaticFields(Class c) { - Set result = new HashSet<>(); - for (Field f : c.getDeclaredFields()) { - if (Modifier.isStatic(f.getModifiers())) { - result.add(f); - } - } - return result; - } - - public boolean fieldsEqual(Field f, ResolvedJavaField rjf) { - return rjf.getDeclaringClass().equals(metaAccess.lookupJavaType(f.getDeclaringClass())) && rjf.getName().equals(f.getName()) && - rjf.getType().resolve(rjf.getDeclaringClass()).equals(metaAccess.lookupJavaType(f.getType())); - } - - public ResolvedJavaField lookupField(ResolvedJavaField[] fields, Field key) { - for (ResolvedJavaField rf : fields) { - if (fieldsEqual(key, rf)) { - return rf; - } - } - return null; - } - - public Field lookupField(Set fields, ResolvedJavaField key) { - for (Field f : fields) { - if (fieldsEqual(f, key)) { - return f; - } - } - return null; - } - - private boolean isHiddenFromReflection(ResolvedJavaField f) { - if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Throwable.class)) && f.getName().equals("backtrace")) { - return true; - } - if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(ConstantPool.class)) && f.getName().equals("constantPoolOop")) { - return true; - } - if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Class.class)) && f.getName().equals("classLoader")) { - return true; - } - return false; - } - - @Test - public void getInstanceFieldsTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - for (boolean includeSuperclasses : new boolean[]{true, false}) { - Set expected = getInstanceFields(c, includeSuperclasses); - ResolvedJavaField[] actual = type.getInstanceFields(includeSuperclasses); - for (Field f : expected) { - assertNotNull(lookupField(actual, f)); - } - for (ResolvedJavaField rf : actual) { - if (!isHiddenFromReflection(rf)) { - assertEquals(rf.toString(), lookupField(expected, rf) != null, !rf.isInternal()); - } - } - - // Test stability of getInstanceFields - ResolvedJavaField[] actual2 = type.getInstanceFields(includeSuperclasses); - assertArrayEquals(actual, actual2); - } - } - } - - @Test - public void getStaticFieldsTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - Set expected = getStaticFields(c); - ResolvedJavaField[] actual = type.getStaticFields(); - for (Field f : expected) { - assertNotNull(lookupField(actual, f)); - } - for (ResolvedJavaField rf : actual) { - if (!isHiddenFromReflection(rf)) { - assertEquals(lookupField(expected, rf) != null, !rf.isInternal()); - } - } - - // Test stability of getStaticFields - ResolvedJavaField[] actual2 = type.getStaticFields(); - assertArrayEquals(actual, actual2); - } - } - - @Test - public void getDeclaredMethodsTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - Method[] raw = c.getDeclaredMethods(); - Set expected = new HashSet<>(); - for (Method m : raw) { - ResolvedJavaMethod resolvedMethod = metaAccess.lookupJavaMethod(m); - assertNotNull(resolvedMethod); - expected.add(resolvedMethod); - } - Set actual = new HashSet<>(Arrays.asList(type.getDeclaredMethods())); - assertEquals(expected, actual); - } - } - - static class A { - static String name = "foo"; - } - - static class B extends A { - } - - static class C { - } - - static class D { - void foo() { - // use of assertions causes the class to have a - assert getClass() != null; - } - } - - @Test - public void getClassInitializerTest() { - assertNotNull(metaAccess.lookupJavaType(A.class).getClassInitializer()); - assertNotNull(metaAccess.lookupJavaType(D.class).getClassInitializer()); - assertNull(metaAccess.lookupJavaType(B.class).getClassInitializer()); - assertNull(metaAccess.lookupJavaType(C.class).getClassInitializer()); - assertNull(metaAccess.lookupJavaType(int.class).getClassInitializer()); - assertNull(metaAccess.lookupJavaType(void.class).getClassInitializer()); - } - - @Test - public void getAnnotationTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - for (Annotation a : c.getAnnotations()) { - assertEquals(a, type.getAnnotation(a.annotationType())); - } - } - } - - @Test - public void memberClassesTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - assertEquals(c.isLocalClass(), type.isLocal()); - assertEquals(c.isMemberClass(), type.isMember()); - Class enclc = c.getEnclosingClass(); - ResolvedJavaType enclt = type.getEnclosingType(); - assertFalse(enclc == null ^ enclt == null); - if (enclc != null) { - assertEquals(enclt, metaAccess.lookupJavaType(enclc)); - } - } - } - - @Test - public void classFilePathTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - URL path = type.getClassFilePath(); - if (type.isPrimitive() || type.isArray()) { - assertEquals(null, path); - } else { - assertNotNull(path); - String pathString = path.getPath(); - if (type.isLocal() || type.isMember()) { - assertTrue(pathString.indexOf('$') > 0); - } - } - } - } - - @Test - public void isTrustedInterfaceTypeTest() { - for (Class c : classes) { - ResolvedJavaType type = metaAccess.lookupJavaType(c); - if (TrustedInterface.class.isAssignableFrom(c)) { - assertTrue(type.isTrustedInterfaceType()); - } - } - } - - private Method findTestMethod(Method apiMethod) { - String testName = apiMethod.getName() + "Test"; - for (Method m : getClass().getDeclaredMethods()) { - if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { - return m; - } - } - return null; - } - - // @formatter:off - private static final String[] untestedApiMethods = { - "initialize", - "isPrimitive", - "newArray", - "getDeclaredConstructors", - "isInitialized", - "isLinked", - "getJavaClass", - "getObjectHub", - "hasFinalizableSubclass", - "hasFinalizer", - "getSourceFileName", - "getClassFilePath", - "isLocal", - "isJavaLangObject", - "isMember", - "getElementalType", - "getEnclosingType", - "$jacocoInit" - }; - // @formatter:on - - /** - * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written - * for them or are added to {@link #untestedApiMethods}. - */ - @Test - public void testCoverage() { - Set known = new HashSet<>(Arrays.asList(untestedApiMethods)); - for (Method m : ResolvedJavaType.class.getDeclaredMethods()) { - if (findTestMethod(m) == null) { - assertTrue("test missing for " + m, known.contains(m.getName())); - } else { - assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); - } - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TypeUniverse.java --- a/graal/com.oracle.graal.java.test/src/com/oracle/graal/java/test/TypeUniverse.java Tue Jun 02 15:15:23 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,150 +0,0 @@ -/* - * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.oracle.graal.java.test; - -import java.io.*; -import java.lang.reflect.*; -import java.util.*; -import java.util.Queue; - -import org.junit.*; - -import sun.misc.*; - -import com.oracle.graal.api.replacements.*; -import com.oracle.graal.api.runtime.*; -import com.oracle.graal.phases.util.*; -import com.oracle.graal.runtime.*; -import com.oracle.jvmci.meta.*; - -/** - * Context for type related tests. - */ -public class TypeUniverse { - - public final Unsafe unsafe; - public static final double JAVA_VERSION = Double.valueOf(System.getProperty("java.specification.version")); - - public final MetaAccessProvider metaAccess; - public final ConstantReflectionProvider constantReflection; - public final SnippetReflectionProvider snippetReflection; - public final Collection> classes = new HashSet<>(); - public final Map, Class> arrayClasses = new HashMap<>(); - public final List constants = new ArrayList<>(); - - public TypeUniverse() { - Providers providers = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getProviders(); - metaAccess = providers.getMetaAccess(); - constantReflection = providers.getConstantReflection(); - snippetReflection = Graal.getRequiredCapability(SnippetReflectionProvider.class); - Unsafe theUnsafe = null; - try { - theUnsafe = Unsafe.getUnsafe(); - } catch (Exception e) { - try { - Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); - theUnsafeField.setAccessible(true); - theUnsafe = (Unsafe) theUnsafeField.get(null); - } catch (Exception e1) { - throw (InternalError) new InternalError("unable to initialize unsafe").initCause(e1); - } - } - unsafe = theUnsafe; - - Class[] initialClasses = {void.class, boolean.class, byte.class, short.class, char.class, int.class, float.class, long.class, double.class, Object.class, Class.class, ClassLoader.class, - String.class, Serializable.class, Cloneable.class, Test.class, TestMetaAccessProvider.class, List.class, Collection.class, Map.class, Queue.class, HashMap.class, - LinkedHashMap.class, IdentityHashMap.class, AbstractCollection.class, AbstractList.class, ArrayList.class, TrustedInterface.class}; - for (Class c : initialClasses) { - addClass(c); - } - for (Field f : JavaConstant.class.getDeclaredFields()) { - int mods = f.getModifiers(); - if (f.getType() == JavaConstant.class && Modifier.isPublic(mods) && Modifier.isStatic(mods) && Modifier.isFinal(mods)) { - try { - JavaConstant c = (JavaConstant) f.get(null); - if (c != null) { - constants.add(c); - } - } catch (Exception e) { - } - } - } - for (Class c : classes) { - if (c != void.class && !c.isArray()) { - constants.add(snippetReflection.forObject(Array.newInstance(c, 42))); - } - } - constants.add(snippetReflection.forObject(new ArrayList<>())); - constants.add(snippetReflection.forObject(new IdentityHashMap<>())); - constants.add(snippetReflection.forObject(new LinkedHashMap<>())); - constants.add(snippetReflection.forObject(new TreeMap<>())); - constants.add(snippetReflection.forObject(new ArrayDeque<>())); - constants.add(snippetReflection.forObject(new LinkedList<>())); - constants.add(snippetReflection.forObject("a string")); - constants.add(snippetReflection.forObject(42)); - constants.add(snippetReflection.forObject(String.class)); - constants.add(snippetReflection.forObject(String[].class)); - } - - public synchronized Class getArrayClass(Class componentType) { - Class arrayClass = arrayClasses.get(componentType); - if (arrayClass == null) { - arrayClass = Array.newInstance(componentType, 0).getClass(); - arrayClasses.put(componentType, arrayClass); - } - return arrayClass; - } - - public static int dimensions(Class c) { - if (c.getComponentType() != null) { - return 1 + dimensions(c.getComponentType()); - } - return 0; - } - - private void addClass(Class c) { - if (classes.add(c)) { - if (c.getSuperclass() != null) { - addClass(c.getSuperclass()); - } - for (Class sc : c.getInterfaces()) { - addClass(sc); - } - for (Class dc : c.getDeclaredClasses()) { - addClass(dc); - } - for (Method m : c.getDeclaredMethods()) { - addClass(m.getReturnType()); - for (Class p : m.getParameterTypes()) { - addClass(p); - } - } - - if (c != void.class && dimensions(c) < 2) { - Class arrayClass = Array.newInstance(c, 0).getClass(); - arrayClasses.put(c, arrayClass); - addClass(arrayClass); - } - } - } -} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/ConstantTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/ConstantTest.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import com.oracle.jvmci.meta.JavaConstant; +import com.oracle.jvmci.meta.Kind; +import org.junit.*; + +public class ConstantTest extends FieldUniverse { + + @Test + public void testNegativeZero() { + Assert.assertTrue("Constant for 0.0f must be different from -0.0f", JavaConstant.FLOAT_0 != JavaConstant.forFloat(-0.0F)); + Assert.assertTrue("Constant for 0.0d must be different from -0.0d", JavaConstant.DOUBLE_0 != JavaConstant.forDouble(-0.0d)); + } + + @Test + public void testNullIsNull() { + Assert.assertTrue(JavaConstant.NULL_POINTER.isNull()); + } + + @Test + public void testOne() { + for (Kind kind : Kind.values()) { + if (kind.isNumericInteger() || kind.isNumericFloat()) { + Assert.assertTrue(JavaConstant.one(kind).getKind() == kind); + } + } + Assert.assertEquals(1, JavaConstant.one(Kind.Int).asInt()); + Assert.assertEquals(1L, JavaConstant.one(Kind.Long).asLong()); + Assert.assertEquals(1, JavaConstant.one(Kind.Byte).asInt()); + Assert.assertEquals(1, JavaConstant.one(Kind.Short).asInt()); + Assert.assertEquals(1, JavaConstant.one(Kind.Char).asInt()); + Assert.assertTrue(1F == JavaConstant.one(Kind.Float).asFloat()); + Assert.assertTrue(1D == JavaConstant.one(Kind.Double).asDouble()); + } + + @Test(expected = IllegalArgumentException.class) + public void testIllegalOne() { + JavaConstant.one(Kind.Illegal); + } + + @Test(expected = IllegalArgumentException.class) + public void testVoidOne() { + JavaConstant.one(Kind.Void); + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/FieldUniverse.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/FieldUniverse.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import com.oracle.jvmci.meta.ResolvedJavaField; + +import java.lang.reflect.*; +import java.util.*; + +/** + * Context for field related tests. + */ +public class FieldUniverse extends TypeUniverse { + + public static final Map fields = new HashMap<>(); + + { + for (Class c : classes) { + for (Field f : c.getDeclaredFields()) { + ResolvedJavaField field = metaAccess.lookupJavaField(f); + fields.put(f, field); + } + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/MethodUniverse.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/MethodUniverse.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import com.oracle.jvmci.meta.ResolvedJavaMethod; +import java.lang.reflect.*; +import java.util.*; + +/** + * Context for method related tests. + */ +public class MethodUniverse extends TypeUniverse { + + public static final Map methods = new HashMap<>(); + public static final Map, ResolvedJavaMethod> constructors = new HashMap<>(); + + { + for (Class c : classes) { + for (Method m : c.getDeclaredMethods()) { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m); + methods.put(m, method); + } + for (Constructor m : c.getDeclaredConstructors()) { + constructors.put(m, metaAccess.lookupJavaMethod(m)); + } + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/NameAndSignature.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/NameAndSignature.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import java.lang.reflect.*; +import java.util.*; + +import com.oracle.jvmci.meta.*; +import com.oracle.jvmci.runtime.*; + +class NameAndSignature { + + public static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); + + final String name; + final Class returnType; + final Class[] parameterTypes; + + public NameAndSignature(Method m) { + this.name = m.getName(); + this.returnType = m.getReturnType(); + this.parameterTypes = m.getParameterTypes(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof NameAndSignature) { + NameAndSignature s = (NameAndSignature) obj; + return s.returnType == returnType && name.equals(s.name) && Arrays.equals(s.parameterTypes, parameterTypes); + } + return false; + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(name + "("); + String sep = ""; + for (Class p : parameterTypes) { + sb.append(sep); + sep = ", "; + sb.append(p.getName()); + } + return sb.append(')').append(returnType.getName()).toString(); + } + + public boolean signatureEquals(ResolvedJavaMethod m) { + Signature s = m.getSignature(); + ResolvedJavaType declaringClass = m.getDeclaringClass(); + if (!s.getReturnType(declaringClass).resolve(declaringClass).equals(metaAccess.lookupJavaType(returnType))) { + return false; + } + if (s.getParameterCount(false) != parameterTypes.length) { + return false; + } + for (int i = 0; i < parameterTypes.length; i++) { + if (!s.getParameterType(i, declaringClass).resolve(declaringClass).equals(metaAccess.lookupJavaType(parameterTypes[i]))) { + return false; + } + } + return true; + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/ResolvedJavaTypeResolveConcreteMethodTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/ResolvedJavaTypeResolveConcreteMethodTest.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.jvmci.meta.*; +import com.oracle.jvmci.runtime.*; + +public class ResolvedJavaTypeResolveConcreteMethodTest { + public final MetaAccessProvider metaAccess; + + public ResolvedJavaTypeResolveConcreteMethodTest() { + metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); + } + + protected abstract static class A { + @SuppressWarnings("unused") + private void priv() { + } + + public void v1() { + } + + public void v2() { + } + + public abstract void abs(); + } + + protected static class B extends A implements I { + public void i() { + } + + @Override + public void v2() { + } + + @Override + public void abs() { + + } + } + + protected static class C extends B { + public void d() { + } + } + + protected abstract static class D extends A { + + } + + protected static class E extends D { + @Override + public void abs() { + } + } + + protected interface I { + void i(); + + default void d() { + } + } + + @Test + public void testDefaultMethod() { + ResolvedJavaType i = getType(I.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaMethod di = getMethod(i, "d"); + ResolvedJavaMethod dc = getMethod(c, "d"); + + assertEquals(di, i.resolveConcreteMethod(di, c)); + assertEquals(di, b.resolveConcreteMethod(di, c)); + assertEquals(dc, c.resolveConcreteMethod(di, c)); + } + + @Test + public void testPrivateMethod() { + ResolvedJavaType a = getType(A.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaMethod priv = getMethod(a, "priv"); + + assertNull(a.resolveConcreteMethod(priv, c)); + assertNull(b.resolveConcreteMethod(priv, c)); + } + + @Test + public void testAbstractMethod() { + ResolvedJavaType a = getType(A.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaType d = getType(D.class); + ResolvedJavaType e = getType(E.class); + ResolvedJavaMethod absa = getMethod(a, "abs"); + ResolvedJavaMethod absb = getMethod(b, "abs"); + ResolvedJavaMethod abse = getMethod(e, "abs"); + + assertNull(a.resolveConcreteMethod(absa, c)); + assertNull(d.resolveConcreteMethod(absa, c)); + + assertEquals(absb, b.resolveConcreteMethod(absa, c)); + assertEquals(absb, b.resolveConcreteMethod(absb, c)); + assertEquals(absb, c.resolveConcreteMethod(absa, c)); + assertEquals(absb, c.resolveConcreteMethod(absb, c)); + assertEquals(abse, e.resolveConcreteMethod(absa, c)); + assertNull(e.resolveConcreteMethod(absb, c)); + assertEquals(abse, e.resolveConcreteMethod(abse, c)); + } + + @Test + public void testVirtualMethod() { + ResolvedJavaType a = getType(A.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaMethod v1a = getMethod(a, "v1"); + ResolvedJavaMethod v2a = getMethod(a, "v2"); + ResolvedJavaMethod v2b = getMethod(b, "v2"); + + assertEquals(v1a, a.resolveConcreteMethod(v1a, c)); + assertEquals(v1a, b.resolveConcreteMethod(v1a, c)); + assertEquals(v1a, c.resolveConcreteMethod(v1a, c)); + assertEquals(v2a, a.resolveConcreteMethod(v2a, c)); + assertEquals(v2b, b.resolveConcreteMethod(v2a, c)); + assertEquals(v2b, b.resolveConcreteMethod(v2b, c)); + assertEquals(v2b, c.resolveConcreteMethod(v2a, c)); + assertEquals(v2b, c.resolveConcreteMethod(v2b, c)); + + } + + static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) { + for (ResolvedJavaMethod method : type.getDeclaredMethods()) { + if (method.getName().equals(methodName)) { + return method; + } + } + throw new IllegalArgumentException(); + } + + protected ResolvedJavaType getType(Class clazz) { + ResolvedJavaType type = metaAccess.lookupJavaType(clazz); + type.initialize(); + return type; + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/ResolvedJavaTypeResolveMethodTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/ResolvedJavaTypeResolveMethodTest.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.jvmci.meta.*; +import com.oracle.jvmci.runtime.*; + +public class ResolvedJavaTypeResolveMethodTest { + public final MetaAccessProvider metaAccess; + + public ResolvedJavaTypeResolveMethodTest() { + metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); + } + + protected abstract static class A { + @SuppressWarnings("unused") + private void priv() { + } + + public void v1() { + } + + public void v2() { + } + + public abstract void abs(); + } + + protected static class B extends A implements I { + public void i() { + } + + @Override + public void v2() { + } + + @Override + public void abs() { + + } + } + + protected static class C extends B { + public void d() { + } + } + + protected abstract static class D extends A { + + } + + protected static class E extends D { + @Override + public void abs() { + } + } + + protected interface I { + void i(); + + default void d() { + } + } + + @Test + public void testDefaultMethod() { + ResolvedJavaType i = getType(I.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaMethod di = getMethod(i, "d"); + ResolvedJavaMethod dc = getMethod(c, "d"); + + assertEquals(di, i.resolveMethod(di, c, true)); + assertEquals(di, b.resolveMethod(di, c, true)); + assertEquals(dc, c.resolveMethod(di, c, true)); + } + + @Test + public void testPrivateMethod() { + ResolvedJavaType a = getType(A.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaMethod priv = getMethod(a, "priv"); + + assertNull(a.resolveMethod(priv, c, true)); + assertNull(b.resolveMethod(priv, c, true)); + } + + @Test + public void testAbstractMethod() { + ResolvedJavaType a = getType(A.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaType d = getType(D.class); + ResolvedJavaType e = getType(E.class); + ResolvedJavaMethod absa = getMethod(a, "abs"); + ResolvedJavaMethod absb = getMethod(b, "abs"); + ResolvedJavaMethod abse = getMethod(e, "abs"); + + assertEquals(absa, a.resolveMethod(absa, c, true)); + assertEquals(absa, d.resolveMethod(absa, c, true)); + + assertEquals(absb, b.resolveMethod(absa, c, true)); + assertEquals(absb, b.resolveMethod(absb, c, true)); + assertEquals(absb, c.resolveMethod(absa, c, true)); + assertEquals(absb, c.resolveMethod(absb, c, true)); + assertEquals(abse, e.resolveMethod(absa, c, true)); + assertNull(e.resolveMethod(absb, c, true)); + assertEquals(abse, e.resolveMethod(abse, c, true)); + } + + @Test + public void testVirtualMethod() { + ResolvedJavaType a = getType(A.class); + ResolvedJavaType b = getType(B.class); + ResolvedJavaType c = getType(C.class); + ResolvedJavaMethod v1a = getMethod(a, "v1"); + ResolvedJavaMethod v2a = getMethod(a, "v2"); + ResolvedJavaMethod v2b = getMethod(b, "v2"); + + assertEquals(v1a, a.resolveMethod(v1a, c, true)); + assertEquals(v1a, b.resolveMethod(v1a, c, true)); + assertEquals(v1a, c.resolveMethod(v1a, c, true)); + assertEquals(v2a, a.resolveMethod(v2a, c, true)); + assertEquals(v2b, b.resolveMethod(v2a, c, true)); + assertEquals(v2b, b.resolveMethod(v2b, c, true)); + assertEquals(v2b, c.resolveMethod(v2a, c, true)); + assertEquals(v2b, c.resolveMethod(v2b, c, true)); + + } + + static ResolvedJavaMethod getMethod(ResolvedJavaType type, String methodName) { + for (ResolvedJavaMethod method : type.getDeclaredMethods()) { + if (method.getName().equals(methodName)) { + return method; + } + } + throw new IllegalArgumentException(); + } + + protected ResolvedJavaType getType(Class clazz) { + ResolvedJavaType type = metaAccess.lookupJavaType(clazz); + type.initialize(); + return type; + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestConstantReflectionProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestConstantReflectionProvider.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static org.junit.Assert.*; + +import java.lang.reflect.*; +import java.util.*; + +import org.junit.*; + +import com.oracle.jvmci.meta.*; + +/** + * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that + * actually returns non-null results for access operations that are possible, i.e., the tests will + * fail for an implementation that spuriously returns null (which is allowed by the specification). + */ +public class TestConstantReflectionProvider extends TypeUniverse { + + @Test + public void constantEqualsTest() { + for (ConstantValue c1 : constants()) { + for (ConstantValue c2 : constants()) { + // test symmetry + assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value)); + if (c1.value.getKind() != Kind.Object && c2.value.getKind() != Kind.Object) { + assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value)); + } + } + } + } + + @Test + public void readArrayLengthTest() { + for (ConstantValue cv : constants()) { + JavaConstant c = cv.value; + Integer actual = constantReflection.readArrayLength(c); + if (c.getKind() != Kind.Object || c.isNull() || !cv.boxed.getClass().isArray()) { + assertNull(actual); + } else { + assertNotNull(actual); + int actualInt = actual; + assertEquals(Array.getLength(cv.boxed), actualInt); + } + } + } + + static class PrimitiveConstants { + static final long LONG_CONST = 42; + static final int INT_CONST = 66; + static final byte BYTE_CONST = 123; + static final boolean BOOL_CONST = true; + } + + static class BoxedConstants { + static final Long LONG_CONST = 42L; + static final Integer INT_CONST = 66; + static final Byte BYTE_CONST = 123; + static final Boolean BOOL_CONST = true; + } + + @Test + public void boxTest() { + for (ConstantValue cv : constants()) { + JavaConstant c = cv.value; + JavaConstant boxed = constantReflection.boxPrimitive(c); + if (boxed != null && c.getKind().isPrimitive()) { + assertTrue(boxed.getKind().isObject()); + assertFalse(boxed.isNull()); + } + } + + List primitiveConstants = readConstants(PrimitiveConstants.class); + List boxedConstants = readConstants(BoxedConstants.class); + for (int i = 0; i < primitiveConstants.size(); i++) { + ConstantValue prim = primitiveConstants.get(i); + ConstantValue box = boxedConstants.get(i); + assertEquals(box.value, constantReflection.boxPrimitive(prim.value)); + } + + assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER)); + } + + @Test + public void unboxTest() { + for (ConstantValue cv : constants()) { + JavaConstant c = cv.value; + JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c); + if (unboxed != null) { + assertFalse(unboxed.getKind().isObject()); + } + } + List primitiveConstants = readConstants(PrimitiveConstants.class); + List boxedConstants = readConstants(BoxedConstants.class); + for (int i = 0; i < primitiveConstants.size(); i++) { + ConstantValue prim = primitiveConstants.get(i); + ConstantValue box = boxedConstants.get(i); + assert prim.getSimpleName().equals(box.getSimpleName()); + assertEquals(prim.value, constantReflection.unboxPrimitive(box.value)); + } + + assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER)); + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestJavaField.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestJavaField.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static org.junit.Assert.*; + +import java.lang.reflect.*; +import java.util.*; + +import org.junit.*; + +import com.oracle.jvmci.meta.*; + +/** + * Tests for {@link JavaField}. + */ +public class TestJavaField extends FieldUniverse { + + @Test + public void getNameTest() { + for (Map.Entry e : fields.entrySet()) { + String expected = e.getKey().getName(); + String actual = e.getValue().getName(); + assertEquals(expected, actual); + } + } + + @Test + public void getTypeTest() { + for (Map.Entry e : fields.entrySet()) { + // Must resolve types first as a resolved types != unresolved types + ResolvedJavaField rf = e.getValue(); + JavaType expected = metaAccess.lookupJavaType(e.getKey().getType()).resolve(rf.getDeclaringClass()); + JavaType actual = rf.getType().resolve(rf.getDeclaringClass()); + assertEquals(expected, actual); + } + } + + @Test + public void getKindTest() { + for (Map.Entry e : fields.entrySet()) { + Kind expected = metaAccess.lookupJavaType(e.getKey().getType()).getKind(); + Kind actual = e.getValue().getKind(); + assertEquals(expected, actual); + } + } + + @Test + public void getDeclaringClassTest() { + for (Map.Entry e : fields.entrySet()) { + Class expected = e.getKey().getDeclaringClass(); + ResolvedJavaType actual = e.getValue().getDeclaringClass(); + assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestJavaMethod.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestJavaMethod.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static org.junit.Assert.*; + +import java.lang.reflect.*; +import java.util.*; + +import org.junit.*; + +import com.oracle.jvmci.meta.*; + +/** + * Tests for {@link JavaMethod}. + */ +public class TestJavaMethod extends MethodUniverse { + + @Test + public void getNameTest() { + for (Map.Entry e : methods.entrySet()) { + String expected = e.getKey().getName(); + String actual = e.getValue().getName(); + assertEquals(expected, actual); + } + } + + @Test + public void getDeclaringClassTest() { + for (Map.Entry e : methods.entrySet()) { + Class expected = e.getKey().getDeclaringClass(); + ResolvedJavaType actual = e.getValue().getDeclaringClass(); + assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); + } + } + + @Test + public void getSignatureTest() { + for (Map.Entry e : methods.entrySet()) { + assertTrue(new NameAndSignature(e.getKey()).signatureEquals(e.getValue())); + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestJavaType.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestJavaType.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import com.oracle.jvmci.meta.JavaType; +import com.oracle.jvmci.meta.Kind; +import static org.junit.Assert.*; + +import org.junit.*; + +/** + * Tests for {@link JavaType}. + */ +public class TestJavaType extends TypeUniverse { + + public TestJavaType() { + } + + @Test + public void getKindTest() { + for (Class c : classes) { + JavaType type = metaAccess.lookupJavaType(c); + Kind expected = Kind.fromJavaClass(c); + Kind actual = type.getKind(); + assertEquals(expected, actual); + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestMetaAccessProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestMetaAccessProvider.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static com.oracle.jvmci.meta.MetaUtil.*; +import static org.junit.Assert.*; + +import java.lang.reflect.*; + +import org.junit.*; + +import com.oracle.jvmci.meta.*; + +/** + * Tests for {@link MetaAccessProvider}. + */ +public class TestMetaAccessProvider extends TypeUniverse { + + @Test + public void lookupJavaTypeTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + assertNotNull(type); + assertEquals(c.getModifiers(), type.getModifiers()); + if (!type.isArray()) { + assertEquals(type.getName(), toInternalName(c.getName())); + assertEquals(type.toJavaName(), c.getName()); + } + } + } + + @Test + public void lookupJavaMethodTest() { + for (Class c : classes) { + for (Method reflect : c.getDeclaredMethods()) { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(reflect); + assertNotNull(method); + int expected = reflect.getModifiers() & Modifier.methodModifiers(); + int actual = method.getModifiers(); + assertEquals(String.format("%s: 0x%x != 0x%x", reflect, expected, actual), expected, actual); + assertTrue(method.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass()))); + } + } + } + + @Test + public void lookupJavaFieldTest() { + for (Class c : classes) { + for (Field reflect : c.getDeclaredFields()) { + ResolvedJavaField field = metaAccess.lookupJavaField(reflect); + assertNotNull(field); + int expected = reflect.getModifiers(); + int actual = field.getModifiers(); + assertEquals(String.format("%s: 0x%x != 0x%x", reflect, expected, actual), expected, actual); + assertTrue(field.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass()))); + } + } + } + + @Test + public void lookupJavaTypeConstantTest() { + for (ConstantValue cv : constants()) { + JavaConstant c = cv.value; + if (c.getKind() == Kind.Object && !c.isNull()) { + Object o = cv.boxed; + ResolvedJavaType type = metaAccess.lookupJavaType(c); + assertNotNull(type); + assertTrue(type.equals(metaAccess.lookupJavaType(o.getClass()))); + } else { + assertEquals(metaAccess.lookupJavaType(c), null); + } + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestResolvedJavaField.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestResolvedJavaField.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static org.junit.Assert.*; + +import java.lang.annotation.*; +import java.lang.reflect.*; +import java.util.*; + +import org.junit.*; + +import com.oracle.jvmci.meta.*; + +/** + * Tests for {@link ResolvedJavaField}. + */ +public class TestResolvedJavaField extends FieldUniverse { + + public TestResolvedJavaField() { + } + + @Test + public void getModifiersTest() { + for (Map.Entry e : fields.entrySet()) { + int expected = e.getKey().getModifiers(); + int actual = e.getValue().getModifiers(); + assertEquals(expected, actual); + } + } + + @Test + public void isSyntheticTest() { + for (Map.Entry e : fields.entrySet()) { + boolean expected = e.getKey().isSynthetic(); + boolean actual = e.getValue().isSynthetic(); + assertEquals(expected, actual); + } + } + + @Test + public void getAnnotationTest() { + for (Map.Entry e : fields.entrySet()) { + for (Annotation expected : e.getKey().getAnnotations()) { + if (expected != null) { + Annotation actual = e.getValue().getAnnotation(expected.annotationType()); + assertEquals(expected, actual); + } + } + } + } + + @Test + public void getLocationIdentityTest() { + for (Map.Entry e : fields.entrySet()) { + LocationIdentity identity = e.getValue().getLocationIdentity(); + assertTrue(identity != null); + } + } + + static class ReadConstantValueTestConstants { + String stringField = "field"; + final String constantStringField = "constantField"; + + static final Object CONST1 = new ReadConstantValueTestConstants(); + static final Object CONST2 = null; + static final Object CONST3 = new String(); + } + + @Test + public void readConstantValueTest() throws NoSuchFieldException { + ResolvedJavaField field = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("stringField")); + List receiverConstants = readConstants(ReadConstantValueTestConstants.class); + for (ConstantValue receiver : receiverConstants) { + JavaConstant value = constantReflection.readConstantFieldValue(field, receiver.value); + assertNull(value); + } + + ResolvedJavaField constField = metaAccess.lookupJavaField(ReadConstantValueTestConstants.class.getDeclaredField("constantStringField")); + for (ConstantValue receiver : receiverConstants) { + JavaConstant value = constantReflection.readConstantFieldValue(constField, receiver.value); + if (value != null) { + Object expected = "constantField"; + String actual = ((ReadConstantValueTestConstants) receiver.boxed).constantStringField; + assertTrue(actual + " != " + expected, actual == expected); + } + } + } + + private Method findTestMethod(Method apiMethod) { + String testName = apiMethod.getName() + "Test"; + for (Method m : getClass().getDeclaredMethods()) { + if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { + return m; + } + } + return null; + } + + // @formatter:off + private static final String[] untestedApiMethods = { + "getDeclaringClass", + "isInternal" + }; + // @formatter:on + + /** + * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written + * for them or are added to {@link #untestedApiMethods}. + */ + @Test + public void testCoverage() { + Set known = new HashSet<>(Arrays.asList(untestedApiMethods)); + for (Method m : ResolvedJavaField.class.getDeclaredMethods()) { + if (m.isSynthetic()) { + continue; + } + if (findTestMethod(m) == null) { + assertTrue("test missing for " + m, known.contains(m.getName())); + } else { + assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); + } + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestResolvedJavaMethod.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestResolvedJavaMethod.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,381 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import com.oracle.jvmci.meta.ExceptionHandler; +import com.oracle.jvmci.meta.ResolvedJavaMethod; +import com.oracle.jvmci.meta.ConstantPool; +import static org.junit.Assert.*; + +import java.lang.annotation.*; +import java.lang.reflect.*; +import java.util.*; + +import org.junit.*; + +/** + * Tests for {@link ResolvedJavaMethod}. + */ +public class TestResolvedJavaMethod extends MethodUniverse { + + public TestResolvedJavaMethod() { + } + + /** + * @see ResolvedJavaMethod#getCode() + */ + @Test + public void getCodeTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + byte[] code = m.getCode(); + if (code == null) { + assertTrue(m.getCodeSize() == 0); + } else { + if (m.isAbstract()) { + assertTrue(code.length == 0); + } else if (!m.isNative()) { + assertTrue(code.length > 0); + } + } + } + } + + /** + * @see ResolvedJavaMethod#getCodeSize() + */ + @Test + public void getCodeSizeTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + int codeSize = m.getCodeSize(); + if (m.isAbstract()) { + assertTrue(codeSize == 0); + } else if (!m.isNative()) { + assertTrue(codeSize > 0); + } + } + } + + @Test + public void getModifiersTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + int expected = e.getKey().getModifiers() & Modifier.methodModifiers(); + int actual = m.getModifiers(); + assertEquals(expected, actual); + } + } + + /** + * @see ResolvedJavaMethod#isClassInitializer() + */ + @Test + public void isClassInitializerTest() { + for (Map.Entry e : methods.entrySet()) { + // Class initializers are hidden from reflection + ResolvedJavaMethod m = e.getValue(); + assertFalse(m.isClassInitializer()); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertFalse(m.isClassInitializer()); + } + } + + @Test + public void isConstructorTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertFalse(m.isConstructor()); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertTrue(m.isConstructor()); + } + } + + @Test + public void isSyntheticTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertEquals(e.getKey().isSynthetic(), m.isSynthetic()); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertEquals(e.getKey().isSynthetic(), m.isSynthetic()); + } + } + + @Test + public void isSynchronizedTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertEquals(Modifier.isSynchronized(e.getKey().getModifiers()), m.isSynchronized()); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertEquals(Modifier.isSynchronized(e.getKey().getModifiers()), m.isSynchronized()); + } + } + + @Test + public void canBeStaticallyBoundTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertEquals(m.canBeStaticallyBound(), canBeStaticallyBound(e.getKey())); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertEquals(m.canBeStaticallyBound(), canBeStaticallyBound(e.getKey())); + } + } + + private static boolean canBeStaticallyBound(Member method) { + int modifiers = method.getModifiers(); + return (Modifier.isFinal(modifiers) || Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers) || Modifier.isFinal(method.getDeclaringClass().getModifiers())) && + !Modifier.isAbstract(modifiers); + } + + private static String methodWithExceptionHandlers(String p1, Object o2) { + try { + return p1.substring(100) + o2.toString(); + } catch (IndexOutOfBoundsException e) { + e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); + } catch (RuntimeException e) { + e.printStackTrace(); + } + return null; + } + + @Test + public void getExceptionHandlersTest() throws NoSuchMethodException { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithExceptionHandlers", String.class, Object.class)); + ExceptionHandler[] handlers = method.getExceptionHandlers(); + assertNotNull(handlers); + assertEquals(handlers.length, 3); + handlers[0].getCatchType().equals(metaAccess.lookupJavaType(IndexOutOfBoundsException.class)); + handlers[1].getCatchType().equals(metaAccess.lookupJavaType(NullPointerException.class)); + handlers[2].getCatchType().equals(metaAccess.lookupJavaType(RuntimeException.class)); + } + + private static String nullPointerExceptionOnFirstLine(Object o, String ignored) { + return o.toString() + ignored; + } + + @Test + public void asStackTraceElementTest() throws NoSuchMethodException { + try { + nullPointerExceptionOnFirstLine(null, "ignored"); + Assert.fail("should not reach here"); + } catch (NullPointerException e) { + StackTraceElement expected = e.getStackTrace()[0]; + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("nullPointerExceptionOnFirstLine", Object.class, String.class)); + StackTraceElement actual = method.asStackTraceElement(0); + assertEquals(expected, actual); + } + } + + @Test + public void getConstantPoolTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + ConstantPool cp = m.getConstantPool(); + assertTrue(cp.length() > 0); + } + } + + @Test(timeout = 1000L) + public void getAnnotationTest() throws NoSuchMethodException { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("getAnnotationTest")); + Test annotation = method.getAnnotation(Test.class); + assertNotNull(annotation); + assertEquals(1000L, annotation.timeout()); + } + + @Test(timeout = 1000L) + public void getAnnotationsTest() throws NoSuchMethodException { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("getAnnotationsTest")); + Annotation[] annotations = method.getAnnotations(); + assertNotNull(annotations); + assertEquals(1, annotations.length); + assertEquals(1000L, ((Test) annotations[0]).timeout()); + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.PARAMETER) + @interface NonNull { + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.PARAMETER) + @interface Special { + } + + private static native void methodWithAnnotatedParameters(@NonNull HashMap p1, @Special @NonNull Class p2); + + @Test + public void getParameterAnnotationsTest() throws NoSuchMethodException { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); + Annotation[][] annotations = method.getParameterAnnotations(); + assertEquals(2, annotations.length); + assertEquals(1, annotations[0].length); + assertEquals(NonNull.class, annotations[0][0].annotationType()); + assertEquals(2, annotations[1].length); + assertEquals(Special.class, annotations[1][0].annotationType()); + assertEquals(NonNull.class, annotations[1][1].annotationType()); + } + + @Test + public void getGenericParameterTypesTest() throws NoSuchMethodException { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); + Type[] genericParameterTypes = method.getGenericParameterTypes(); + assertEquals(2, genericParameterTypes.length); + assertEquals("java.util.HashMap", genericParameterTypes[0].toString()); + assertEquals("java.lang.Class", genericParameterTypes[1].toString()); + } + + @Test + public void getMaxLocalsTest() throws NoSuchMethodException { + ResolvedJavaMethod method1 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); + ResolvedJavaMethod method2 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("nullPointerExceptionOnFirstLine", Object.class, String.class)); + assertEquals(0, method1.getMaxLocals()); + assertEquals(2, method2.getMaxLocals()); + + } + + @Test + public void getMaxStackSizeTest() throws NoSuchMethodException { + ResolvedJavaMethod method1 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("methodWithAnnotatedParameters", HashMap.class, Class.class)); + ResolvedJavaMethod method2 = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("nullPointerExceptionOnFirstLine", Object.class, String.class)); + assertEquals(0, method1.getMaxStackSize()); + // some versions of javac produce bytecode with a stacksize of 2 for this method + // JSR 292 also sometimes need one more stack slot + int method2StackSize = method2.getMaxStackSize(); + assertTrue(2 <= method2StackSize && method2StackSize <= 4); + } + + @Test + public void isDefaultTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertEquals(e.getKey().isDefault(), m.isDefault()); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertFalse(m.isDefault()); + } + } + + @Test + public void hasReceiverTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertTrue(m.hasReceiver() != Modifier.isStatic(e.getKey().getModifiers())); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertTrue(m.hasReceiver()); + } + } + + @Test + public void hasBytecodesTest() { + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertTrue(m.hasBytecodes() == (m.isConcrete() && !m.isNative())); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertTrue(m.hasBytecodes()); + } + } + + @Test + public void isJavaLangObjectInitTest() throws NoSuchMethodException { + ResolvedJavaMethod method = metaAccess.lookupJavaMethod(Object.class.getConstructor()); + assertTrue(method.isJavaLangObjectInit()); + for (Map.Entry e : methods.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + assertFalse(m.isJavaLangObjectInit()); + } + for (Map.Entry, ResolvedJavaMethod> e : constructors.entrySet()) { + ResolvedJavaMethod m = e.getValue(); + Constructor key = e.getKey(); + if (key.getDeclaringClass() == Object.class && key.getParameters().length == 0) { + assertTrue(m.isJavaLangObjectInit()); + } else { + assertFalse(m.isJavaLangObjectInit()); + } + } + } + + private Method findTestMethod(Method apiMethod) { + String testName = apiMethod.getName() + "Test"; + for (Method m : getClass().getDeclaredMethods()) { + if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { + return m; + } + } + return null; + } + + // @formatter:off + private static final String[] untestedApiMethods = { + "invoke", + "newInstance", + "getDeclaringClass", + "getEncoding", + "getProfilingInfo", + "reprofile", + "getCompilerStorage", + "canBeInlined", + "shouldBeInlined", + "getLineNumberTable", + "getLocalVariableTable", + "isInVirtualMethodTable", + "toParameterTypes", + "getParameterAnnotation", + "getSpeculationLog", + "$jacocoInit" + }; + // @formatter:on + + /** + * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written + * for them or are added to {@link #untestedApiMethods}. + */ + @Test + public void testCoverage() { + Set known = new HashSet<>(Arrays.asList(untestedApiMethods)); + for (Method m : ResolvedJavaMethod.class.getDeclaredMethods()) { + if (findTestMethod(m) == null) { + assertTrue("test missing for " + m, known.contains(m.getName())); + } else { + assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); + } + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestResolvedJavaType.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TestResolvedJavaType.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,874 @@ +/* + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static java.lang.reflect.Modifier.*; +import static org.junit.Assert.*; + +import java.lang.annotation.*; +import java.lang.reflect.*; +import java.net.*; +import java.util.*; + +import org.junit.*; + +import sun.reflect.ConstantPool; + +import com.oracle.jvmci.common.*; +import com.oracle.jvmci.meta.Assumptions.AssumptionResult; +import com.oracle.jvmci.meta.*; + +/** + * Tests for {@link ResolvedJavaType}. + */ +public class TestResolvedJavaType extends TypeUniverse { + + public TestResolvedJavaType() { + } + + @Test + public void findInstanceFieldWithOffsetTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + Set reflectionFields = getInstanceFields(c, true); + for (Field f : reflectionFields) { + ResolvedJavaField rf = lookupField(type.getInstanceFields(true), f); + assertNotNull(rf); + long offset = isStatic(f.getModifiers()) ? unsafe.staticFieldOffset(f) : unsafe.objectFieldOffset(f); + ResolvedJavaField result = type.findInstanceFieldWithOffset(offset, rf.getKind()); + assertNotNull(result); + assertTrue(fieldsEqual(f, result)); + } + } + } + + @Test + public void isInterfaceTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + boolean expected = c.isInterface(); + boolean actual = type.isInterface(); + assertEquals(expected, actual); + } + } + + @Test + public void isInstanceClassTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + boolean expected = !c.isArray() && !c.isPrimitive() && !c.isInterface(); + boolean actual = type.isInstanceClass(); + assertEquals(expected, actual); + } + } + + @Test + public void isArrayTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + boolean expected = c.isArray(); + boolean actual = type.isArray(); + assertEquals(expected, actual); + } + } + + @Test + public void getModifiersTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + int expected = c.getModifiers(); + int actual = type.getModifiers(); + assertEquals(expected, actual); + } + } + + @Test + public void isAssignableFromTest() { + Class[] all = classes.toArray(new Class[classes.size()]); + for (int i = 0; i < all.length; i++) { + Class c1 = all[i]; + for (int j = i; j < all.length; j++) { + Class c2 = all[j]; + ResolvedJavaType t1 = metaAccess.lookupJavaType(c1); + ResolvedJavaType t2 = metaAccess.lookupJavaType(c2); + boolean expected = c1.isAssignableFrom(c2); + boolean actual = t1.isAssignableFrom(t2); + assertEquals(expected, actual); + if (expected && t1 != t2) { + assertFalse(t2.isAssignableFrom(t1)); + } + } + } + } + + @Test + public void isInstanceTest() { + for (ConstantValue cv : constants()) { + JavaConstant c = cv.value; + if (c.getKind() == Kind.Object && !c.isNull()) { + ResolvedJavaType cType = metaAccess.lookupJavaType(c); + for (ResolvedJavaType t : javaTypes) { + if (t.isAssignableFrom(cType)) { + assertTrue(t.isInstance(c)); + } else { + assertFalse(t.isInstance(c)); + } + } + } + } + } + + private static Class asExactClass(Class c) { + if (c.isArray()) { + if (asExactClass(c.getComponentType()) != null) { + return c; + } + } else { + if (c.isPrimitive() || Modifier.isFinal(c.getModifiers())) { + return c; + } + } + return null; + } + + @Test + public void asExactTypeTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + ResolvedJavaType exactType = type.asExactType(); + Class expected = asExactClass(c); + if (expected == null) { + assertTrue("exact(" + c.getName() + ") != null", exactType == null); + } else { + assertNotNull(exactType); + assertTrue(exactType.equals(metaAccess.lookupJavaType(expected))); + } + } + } + + @Test + public void getSuperclassTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + Class expected = c.getSuperclass(); + ResolvedJavaType actual = type.getSuperclass(); + if (expected == null) { + assertTrue(actual == null); + } else { + assertNotNull(actual); + assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); + } + } + } + + @Test + public void getInterfacesTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + Class[] expected = c.getInterfaces(); + ResolvedJavaType[] actual = type.getInterfaces(); + assertEquals(expected.length, actual.length); + for (int i = 0; i < expected.length; i++) { + assertTrue(actual[i].equals(metaAccess.lookupJavaType(expected[i]))); + } + } + } + + public Class getSupertype(Class c) { + assert !c.isPrimitive(); + if (c.isArray()) { + Class componentType = c.getComponentType(); + if (componentType.isPrimitive() || componentType == Object.class) { + return Object.class; + } + return getArrayClass(getSupertype(componentType)); + } + if (c.isInterface()) { + return Object.class; + } + return c.getSuperclass(); + } + + public Class findLeastCommonAncestor(Class c1Initial, Class c2Initial) { + if (c1Initial.isPrimitive() || c2Initial.isPrimitive()) { + return null; + } else { + Class c1 = c1Initial; + Class c2 = c2Initial; + while (true) { + if (c1.isAssignableFrom(c2)) { + return c1; + } + if (c2.isAssignableFrom(c1)) { + return c2; + } + c1 = getSupertype(c1); + c2 = getSupertype(c2); + } + } + } + + @Test + public void findLeastCommonAncestorTest() { + Class[] all = classes.toArray(new Class[classes.size()]); + for (int i = 0; i < all.length; i++) { + Class c1 = all[i]; + for (int j = i; j < all.length; j++) { + Class c2 = all[j]; + ResolvedJavaType t1 = metaAccess.lookupJavaType(c1); + ResolvedJavaType t2 = metaAccess.lookupJavaType(c2); + Class expected = findLeastCommonAncestor(c1, c2); + ResolvedJavaType actual = t1.findLeastCommonAncestor(t2); + if (expected == null) { + assertTrue(actual == null); + } else { + assertNotNull(actual); + assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); + } + } + } + } + + private static class Base { + } + + abstract static class Abstract1 extends Base { + } + + interface Interface1 { + } + + static class Concrete1 extends Abstract1 { + } + + static class Concrete2 extends Abstract1 implements Interface1 { + } + + static class Concrete3 extends Concrete2 { + } + + static final class Final1 extends Abstract1 { + } + + abstract static class Abstract4 extends Concrete3 { + } + + void checkConcreteSubtype(ResolvedJavaType type, ResolvedJavaType expected) { + AssumptionResult leafConcreteSubtype = type.findLeafConcreteSubtype(); + if (leafConcreteSubtype == null) { + // findLeafConcreteSubtype() is conservative + } else { + if (expected == null) { + assertNull(leafConcreteSubtype); + } else { + assertTrue(leafConcreteSubtype.getResult().equals(expected)); + } + } + + if (!type.isArray()) { + ResolvedJavaType arrayType = type.getArrayClass(); + AssumptionResult arraySubtype = arrayType.findLeafConcreteSubtype(); + if (arraySubtype != null) { + assertEquals(arraySubtype.getResult(), arrayType); + } else { + // findLeafConcreteSubtype() method is conservative + } + } + } + + @Test + public void findLeafConcreteSubtypeTest() { + ResolvedJavaType base = metaAccess.lookupJavaType(Base.class); + checkConcreteSubtype(base, base); + + ResolvedJavaType a1 = metaAccess.lookupJavaType(Abstract1.class); + ResolvedJavaType c1 = metaAccess.lookupJavaType(Concrete1.class); + + checkConcreteSubtype(base, null); + checkConcreteSubtype(a1, c1); + checkConcreteSubtype(c1, c1); + + ResolvedJavaType i1 = metaAccess.lookupJavaType(Interface1.class); + ResolvedJavaType c2 = metaAccess.lookupJavaType(Concrete2.class); + + checkConcreteSubtype(base, null); + checkConcreteSubtype(a1, null); + checkConcreteSubtype(c1, c1); + checkConcreteSubtype(i1, c2); + checkConcreteSubtype(c2, c2); + + ResolvedJavaType c3 = metaAccess.lookupJavaType(Concrete3.class); + checkConcreteSubtype(c2, null); + checkConcreteSubtype(c3, c3); + + ResolvedJavaType a4 = metaAccess.lookupJavaType(Abstract4.class); + checkConcreteSubtype(c3, null); + checkConcreteSubtype(a4, null); + + ResolvedJavaType a1a = metaAccess.lookupJavaType(Abstract1[].class); + checkConcreteSubtype(a1a, null); + ResolvedJavaType c1a = metaAccess.lookupJavaType(Concrete1[].class); + checkConcreteSubtype(c1a, null); + ResolvedJavaType f1a = metaAccess.lookupJavaType(Final1[].class); + checkConcreteSubtype(f1a, f1a); + + ResolvedJavaType obja = metaAccess.lookupJavaType(Object[].class); + checkConcreteSubtype(obja, null); + + ResolvedJavaType inta = metaAccess.lookupJavaType(int[].class); + checkConcreteSubtype(inta, inta); + } + + interface NoImplementor { + } + + interface SingleImplementorInterface { + } + + static class SingleConcreteImplementor implements SingleImplementorInterface { + } + + interface SingleAbstractImplementorInterface { + } + + abstract static class SingleAbstractImplementor implements SingleAbstractImplementorInterface { + } + + interface MultiImplementorInterface { + } + + static class ConcreteImplementor1 implements MultiImplementorInterface { + } + + static class ConcreteImplementor2 implements MultiImplementorInterface { + } + + interface MultipleAbstractImplementorInterface { + } + + abstract static class MultiAbstractImplementor1 implements MultipleAbstractImplementorInterface { + } + + abstract static class MultiAbstractImplementor2 implements MultipleAbstractImplementorInterface { + } + + interface SingleAbstractImplementorInterface2 { + } + + interface ExtendedSingleImplementorInterface { + } + + abstract static class SingleAbstractImplementor2 implements SingleAbstractImplementorInterface2 { + } + + static class ConcreteTransitiveImplementor1 extends SingleAbstractImplementor2 implements ExtendedSingleImplementorInterface { + } + + static class ConcreteTransitiveImplementor2 extends SingleAbstractImplementor2 implements ExtendedSingleImplementorInterface { + } + + @Test + public void getSingleImplementorTest() { + ResolvedJavaType iNi = metaAccess.lookupJavaType(NoImplementor.class); + assertNull(iNi.getSingleImplementor()); + + ResolvedJavaType iSi = metaAccess.lookupJavaType(SingleImplementorInterface.class); + ResolvedJavaType cSi = metaAccess.lookupJavaType(SingleConcreteImplementor.class); + assertEquals(cSi, iSi.getSingleImplementor()); + + ResolvedJavaType iSai = metaAccess.lookupJavaType(SingleAbstractImplementorInterface.class); + ResolvedJavaType aSai = metaAccess.lookupJavaType(SingleAbstractImplementor.class); + assertEquals(aSai, iSai.getSingleImplementor()); + + ResolvedJavaType iMi = metaAccess.lookupJavaType(MultiImplementorInterface.class); + metaAccess.lookupJavaType(ConcreteImplementor1.class); + metaAccess.lookupJavaType(ConcreteImplementor2.class); + assertEquals(iMi, iMi.getSingleImplementor()); + + ResolvedJavaType iMai = metaAccess.lookupJavaType(MultipleAbstractImplementorInterface.class); + metaAccess.lookupJavaType(MultiAbstractImplementor1.class); + metaAccess.lookupJavaType(MultiAbstractImplementor2.class); + assertEquals(iMai, iMai.getSingleImplementor()); + + ResolvedJavaType iSai2 = metaAccess.lookupJavaType(SingleAbstractImplementorInterface2.class); + ResolvedJavaType aSai2 = metaAccess.lookupJavaType(SingleAbstractImplementor2.class); + metaAccess.lookupJavaType(ConcreteTransitiveImplementor1.class); + metaAccess.lookupJavaType(ConcreteTransitiveImplementor2.class); + assertEquals(aSai2, iSai2.getSingleImplementor()); + } + + @Test(expected = JVMCIError.class) + public void getSingleImplementorTestClassReceiver() { + ResolvedJavaType base = metaAccess.lookupJavaType(Base.class); + base.getSingleImplementor(); + } + + @Test(expected = JVMCIError.class) + public void getSingleImplementorTestPrimitiveReceiver() { + ResolvedJavaType primitive = metaAccess.lookupJavaType(int.class); + primitive.getSingleImplementor(); + } + + @Test + public void getComponentTypeTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + Class expected = c.getComponentType(); + ResolvedJavaType actual = type.getComponentType(); + if (expected == null) { + assertNull(actual); + } else { + assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); + } + } + } + + @Test + public void getArrayClassTest() { + for (Class c : classes) { + if (c != void.class) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + Class expected = getArrayClass(c); + ResolvedJavaType actual = type.getArrayClass(); + assertTrue(actual.equals(metaAccess.lookupJavaType(expected))); + } + } + } + + static class Declarations { + + final Method implementation; + final Set declarations; + + public Declarations(Method impl) { + this.implementation = impl; + declarations = new HashSet<>(); + } + } + + /** + * See Method + * overriding. + */ + static boolean isOverriderOf(Method impl, Method m) { + if (!isPrivate(m.getModifiers()) && !isFinal(m.getModifiers())) { + if (m.getName().equals(impl.getName())) { + if (m.getReturnType() == impl.getReturnType()) { + if (Arrays.equals(m.getParameterTypes(), impl.getParameterTypes())) { + if (isPublic(m.getModifiers()) || isProtected(m.getModifiers())) { + // m is public or protected + return isPublic(impl.getModifiers()) || isProtected(impl.getModifiers()); + } else { + // m is package-private + return impl.getDeclaringClass().getPackage() == m.getDeclaringClass().getPackage(); + } + } + } + } + } + return false; + } + + static final Map, VTable> vtables = new HashMap<>(); + + static class VTable { + + final Map methods = new HashMap<>(); + } + + static synchronized VTable getVTable(Class c) { + VTable vtable = vtables.get(c); + if (vtable == null) { + vtable = new VTable(); + if (c != Object.class) { + VTable superVtable = getVTable(c.getSuperclass()); + vtable.methods.putAll(superVtable.methods); + } + for (Method m : c.getDeclaredMethods()) { + if (!isStatic(m.getModifiers()) && !isPrivate(m.getModifiers())) { + if (isAbstract(m.getModifiers())) { + // A subclass makes a concrete method in a superclass abstract + vtable.methods.remove(new NameAndSignature(m)); + } else { + vtable.methods.put(new NameAndSignature(m), m); + } + } + } + vtables.put(c, vtable); + } + return vtable; + } + + static Set findDeclarations(Method impl, Class c) { + Set declarations = new HashSet<>(); + NameAndSignature implSig = new NameAndSignature(impl); + if (c != null) { + for (Method m : c.getDeclaredMethods()) { + if (new NameAndSignature(m).equals(implSig)) { + declarations.add(m); + break; + } + } + if (!c.isInterface()) { + declarations.addAll(findDeclarations(impl, c.getSuperclass())); + } + for (Class i : c.getInterfaces()) { + declarations.addAll(findDeclarations(impl, i)); + } + } + return declarations; + } + + private static void checkResolveMethod(ResolvedJavaType type, ResolvedJavaType context, ResolvedJavaMethod decl, ResolvedJavaMethod expected) { + ResolvedJavaMethod impl = type.resolveConcreteMethod(decl, context); + assertEquals(expected, impl); + } + + @Test + public void resolveMethodTest() { + ResolvedJavaType context = metaAccess.lookupJavaType(TestResolvedJavaType.class); + for (Class c : classes) { + if (c.isInterface() || c.isPrimitive()) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + for (Method m : c.getDeclaredMethods()) { + if (JAVA_VERSION <= 1.7D || (!isStatic(m.getModifiers()) && !isPrivate(m.getModifiers()))) { + ResolvedJavaMethod resolved = metaAccess.lookupJavaMethod(m); + ResolvedJavaMethod impl = type.resolveMethod(resolved, context, true); + ResolvedJavaMethod expected = resolved.isDefault() || resolved.isAbstract() ? resolved : null; + assertEquals(m.toString(), expected, impl); + } else { + // As of JDK 8, interfaces can have static and private methods + } + } + } else { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + VTable vtable = getVTable(c); + for (Method impl : vtable.methods.values()) { + Set decls = findDeclarations(impl, c); + for (Method decl : decls) { + ResolvedJavaMethod m = metaAccess.lookupJavaMethod(decl); + if (m.isPublic()) { + ResolvedJavaMethod i = metaAccess.lookupJavaMethod(impl); + checkResolveMethod(type, context, m, i); + } + } + } + } + } + } + + @Test + public void resolveConcreteMethodTest() { + ResolvedJavaType context = metaAccess.lookupJavaType(TestResolvedJavaType.class); + for (Class c : classes) { + if (c.isInterface() || c.isPrimitive()) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + for (Method m : c.getDeclaredMethods()) { + if (JAVA_VERSION <= 1.7D || (!isStatic(m.getModifiers()) && !isPrivate(m.getModifiers()))) { + ResolvedJavaMethod resolved = metaAccess.lookupJavaMethod(m); + ResolvedJavaMethod impl = type.resolveConcreteMethod(resolved, context); + ResolvedJavaMethod expected = resolved.isDefault() ? resolved : null; + assertEquals(m.toString(), expected, impl); + } else { + // As of JDK 8, interfaces can have static and private methods + } + } + } else { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + VTable vtable = getVTable(c); + for (Method impl : vtable.methods.values()) { + Set decls = findDeclarations(impl, c); + for (Method decl : decls) { + ResolvedJavaMethod m = metaAccess.lookupJavaMethod(decl); + if (m.isPublic()) { + ResolvedJavaMethod i = metaAccess.lookupJavaMethod(impl); + checkResolveMethod(type, context, m, i); + } + } + } + for (Method m : c.getDeclaredMethods()) { + ResolvedJavaMethod impl = type.resolveConcreteMethod(metaAccess.lookupJavaMethod(m), context); + ResolvedJavaMethod expected = isAbstract(m.getModifiers()) ? null : impl; + assertEquals(type + " " + m.toString(), expected, impl); + } + } + } + } + + @Test + public void findUniqueConcreteMethodTest() throws NoSuchMethodException { + ResolvedJavaMethod thisMethod = metaAccess.lookupJavaMethod(getClass().getDeclaredMethod("findUniqueConcreteMethodTest")); + ResolvedJavaMethod ucm = metaAccess.lookupJavaType(getClass()).findUniqueConcreteMethod(thisMethod).getResult(); + assertEquals(thisMethod, ucm); + } + + public static Set getInstanceFields(Class c, boolean includeSuperclasses) { + if (c.isArray() || c.isPrimitive() || c.isInterface()) { + return Collections.emptySet(); + } + Set result = new HashSet<>(); + for (Field f : c.getDeclaredFields()) { + if (!Modifier.isStatic(f.getModifiers())) { + result.add(f); + } + } + if (includeSuperclasses && c != Object.class) { + result.addAll(getInstanceFields(c.getSuperclass(), true)); + } + return result; + } + + public static Set getStaticFields(Class c) { + Set result = new HashSet<>(); + for (Field f : c.getDeclaredFields()) { + if (Modifier.isStatic(f.getModifiers())) { + result.add(f); + } + } + return result; + } + + public boolean fieldsEqual(Field f, ResolvedJavaField rjf) { + return rjf.getDeclaringClass().equals(metaAccess.lookupJavaType(f.getDeclaringClass())) && rjf.getName().equals(f.getName()) && + rjf.getType().resolve(rjf.getDeclaringClass()).equals(metaAccess.lookupJavaType(f.getType())); + } + + public ResolvedJavaField lookupField(ResolvedJavaField[] fields, Field key) { + for (ResolvedJavaField rf : fields) { + if (fieldsEqual(key, rf)) { + return rf; + } + } + return null; + } + + public Field lookupField(Set fields, ResolvedJavaField key) { + for (Field f : fields) { + if (fieldsEqual(f, key)) { + return f; + } + } + return null; + } + + private static boolean isHiddenFromReflection(ResolvedJavaField f) { + if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Throwable.class)) && f.getName().equals("backtrace")) { + return true; + } + if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(ConstantPool.class)) && f.getName().equals("constantPoolOop")) { + return true; + } + if (f.getDeclaringClass().equals(metaAccess.lookupJavaType(Class.class)) && f.getName().equals("classLoader")) { + return true; + } + return false; + } + + @Test + public void getInstanceFieldsTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + for (boolean includeSuperclasses : new boolean[]{true, false}) { + Set expected = getInstanceFields(c, includeSuperclasses); + ResolvedJavaField[] actual = type.getInstanceFields(includeSuperclasses); + for (Field f : expected) { + assertNotNull(lookupField(actual, f)); + } + for (ResolvedJavaField rf : actual) { + if (!isHiddenFromReflection(rf)) { + assertEquals(rf.toString(), lookupField(expected, rf) != null, !rf.isInternal()); + } + } + + // Test stability of getInstanceFields + ResolvedJavaField[] actual2 = type.getInstanceFields(includeSuperclasses); + assertArrayEquals(actual, actual2); + } + } + } + + @Test + public void getStaticFieldsTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + Set expected = getStaticFields(c); + ResolvedJavaField[] actual = type.getStaticFields(); + for (Field f : expected) { + assertNotNull(lookupField(actual, f)); + } + for (ResolvedJavaField rf : actual) { + if (!isHiddenFromReflection(rf)) { + assertEquals(lookupField(expected, rf) != null, !rf.isInternal()); + } + } + + // Test stability of getStaticFields + ResolvedJavaField[] actual2 = type.getStaticFields(); + assertArrayEquals(actual, actual2); + } + } + + @Test + public void getDeclaredMethodsTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + Method[] raw = c.getDeclaredMethods(); + Set expected = new HashSet<>(); + for (Method m : raw) { + ResolvedJavaMethod resolvedMethod = metaAccess.lookupJavaMethod(m); + assertNotNull(resolvedMethod); + expected.add(resolvedMethod); + } + Set actual = new HashSet<>(Arrays.asList(type.getDeclaredMethods())); + assertEquals(expected, actual); + } + } + + static class A { + static String name = "foo"; + } + + static class B extends A { + } + + static class C { + } + + static class D { + void foo() { + // use of assertions causes the class to have a + assert getClass() != null; + } + } + + @Test + public void getClassInitializerTest() { + assertNotNull(metaAccess.lookupJavaType(A.class).getClassInitializer()); + assertNotNull(metaAccess.lookupJavaType(D.class).getClassInitializer()); + assertNull(metaAccess.lookupJavaType(B.class).getClassInitializer()); + assertNull(metaAccess.lookupJavaType(C.class).getClassInitializer()); + assertNull(metaAccess.lookupJavaType(int.class).getClassInitializer()); + assertNull(metaAccess.lookupJavaType(void.class).getClassInitializer()); + } + + @Test + public void getAnnotationTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + for (Annotation a : c.getAnnotations()) { + assertEquals(a, type.getAnnotation(a.annotationType())); + } + } + } + + @Test + public void memberClassesTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + assertEquals(c.isLocalClass(), type.isLocal()); + assertEquals(c.isMemberClass(), type.isMember()); + Class enclc = c.getEnclosingClass(); + ResolvedJavaType enclt = type.getEnclosingType(); + assertFalse(enclc == null ^ enclt == null); + if (enclc != null) { + assertEquals(enclt, metaAccess.lookupJavaType(enclc)); + } + } + } + + @Test + public void classFilePathTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + URL path = type.getClassFilePath(); + if (type.isPrimitive() || type.isArray()) { + assertEquals(null, path); + } else { + assertNotNull(path); + String pathString = path.getPath(); + if (type.isLocal() || type.isMember()) { + assertTrue(pathString.indexOf('$') > 0); + } + } + } + } + + @Test + public void isTrustedInterfaceTypeTest() { + for (Class c : classes) { + ResolvedJavaType type = metaAccess.lookupJavaType(c); + if (TrustedInterface.class.isAssignableFrom(c)) { + assertTrue(type.isTrustedInterfaceType()); + } + } + } + + private Method findTestMethod(Method apiMethod) { + String testName = apiMethod.getName() + "Test"; + for (Method m : getClass().getDeclaredMethods()) { + if (m.getName().equals(testName) && m.getAnnotation(Test.class) != null) { + return m; + } + } + return null; + } + + // @formatter:off + private static final String[] untestedApiMethods = { + "initialize", + "isPrimitive", + "newArray", + "getDeclaredConstructors", + "isInitialized", + "isLinked", + "getJavaClass", + "getObjectHub", + "hasFinalizableSubclass", + "hasFinalizer", + "getSourceFileName", + "getClassFilePath", + "isLocal", + "isJavaLangObject", + "isMember", + "getElementalType", + "getEnclosingType", + "$jacocoInit" + }; + // @formatter:on + + /** + * Ensures that any new methods added to {@link ResolvedJavaMethod} either have a test written + * for them or are added to {@link #untestedApiMethods}. + */ + @Test + public void testCoverage() { + Set known = new HashSet<>(Arrays.asList(untestedApiMethods)); + for (Method m : ResolvedJavaType.class.getDeclaredMethods()) { + if (findTestMethod(m) == null) { + assertTrue("test missing for " + m, known.contains(m.getName())); + } else { + assertFalse("test should be removed from untestedApiMethods" + m, known.contains(m.getName())); + } + } + } +} diff -r b45e0f791465 -r 381ab4105afe graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TypeUniverse.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.jvmci.runtime.test/src/com/oracle/jvmci/runtime/test/TypeUniverse.java Tue Jun 02 15:15:58 2015 +0200 @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.jvmci.runtime.test; + +import static java.lang.reflect.Modifier.*; + +import java.io.*; +import java.lang.reflect.*; +import java.util.*; +import java.util.Queue; +import java.util.stream.*; + +import org.junit.*; + +import sun.misc.*; + +import com.oracle.jvmci.meta.*; +import com.oracle.jvmci.runtime.*; + +/** + * Context for type related tests. + */ +public class TypeUniverse { + + public static final Unsafe unsafe; + public static final double JAVA_VERSION = Double.valueOf(System.getProperty("java.specification.version")); + + public static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); + public static final ConstantReflectionProvider constantReflection = JVMCI.getRuntime().getHostJVMCIBackend().getConstantReflection(); + public static final Collection> classes = new HashSet<>(); + public static final Set javaTypes; + public static final Map, Class> arrayClasses = new HashMap<>(); + + private static List constants; + + static { + Unsafe theUnsafe = null; + try { + theUnsafe = Unsafe.getUnsafe(); + } catch (Exception e) { + try { + Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); + theUnsafeField.setAccessible(true); + theUnsafe = (Unsafe) theUnsafeField.get(null); + } catch (Exception e1) { + throw (InternalError) new InternalError("unable to initialize unsafe").initCause(e1); + } + } + unsafe = theUnsafe; + + Class[] initialClasses = {void.class, boolean.class, byte.class, short.class, char.class, int.class, float.class, long.class, double.class, Object.class, Class.class, ClassLoader.class, + String.class, Serializable.class, Cloneable.class, Test.class, TestMetaAccessProvider.class, List.class, Collection.class, Map.class, Queue.class, HashMap.class, + LinkedHashMap.class, IdentityHashMap.class, AbstractCollection.class, AbstractList.class, ArrayList.class, TrustedInterface.class}; + for (Class c : initialClasses) { + addClass(c); + } + + javaTypes = Collections.unmodifiableSet(classes.stream().map(c -> metaAccess.lookupJavaType(c)).collect(Collectors.toSet())); + } + + static class ConstantsUniverse { + static final Object[] ARRAYS = classes.stream().map(c -> c != void.class && !c.isArray() ? Array.newInstance(c, 42) : null).filter(o -> o != null).collect(Collectors.toList()).toArray(); + static final Object CONST1 = new ArrayList<>(); + static final Object CONST2 = new ArrayList<>(); + static final Object CONST3 = new IdentityHashMap<>(); + static final Object CONST4 = new LinkedHashMap<>(); + static final Object CONST5 = new TreeMap<>(); + static final Object CONST6 = new ArrayDeque<>(); + static final Object CONST7 = new LinkedList<>(); + static final Object CONST8 = "a string"; + static final Object CONST9 = 42; + static final Object CONST10 = String.class; + static final Object CONST11 = String[].class; + } + + public static List constants() { + if (constants == null) { + List res = readConstants(JavaConstant.class); + res.addAll(readConstants(ConstantsUniverse.class)); + constants = res; + } + return constants; + } + + public static class ConstantValue { + public final String name; + public final JavaConstant value; + public final Object boxed; + + public ConstantValue(String name, JavaConstant value, Object boxed) { + this.name = name; + this.value = value; + this.boxed = boxed; + } + + @Override + public String toString() { + return name + "=" + value; + } + + public String getSimpleName() { + return name.substring(name.lastIndexOf('.') + 1); + } + } + + /** + * Reads the value of all {@code static final} fields from a given class into an array of + * {@link ConstantValue}s. + */ + public static List readConstants(Class fromClass) { + try { + List res = new ArrayList<>(); + for (Field field : fromClass.getDeclaredFields()) { + if (isStatic(field.getModifiers()) && isFinal(field.getModifiers())) { + JavaField javaField = metaAccess.lookupJavaField(field); + Object boxed = field.get(null); + if (boxed instanceof JavaConstant) { + res.add(new ConstantValue(javaField.format("%H.%n"), (JavaConstant) boxed, boxed)); + } else { + JavaConstant value = constantReflection.readConstantFieldValue(javaField, null); + if (value != null) { + res.add(new ConstantValue(javaField.format("%H.%n"), value, boxed)); + if (boxed instanceof Object[]) { + Object[] arr = (Object[]) boxed; + for (int i = 0; i < arr.length; i++) { + JavaConstant element = constantReflection.readArrayElement(value, i); + if (element != null) { + res.add(new ConstantValue(javaField.format("%H.%n[" + i + "]"), element, arr[i])); + } + } + } + } + } + } + } + return res; + } catch (Exception e) { + throw new AssertionError(e); + } + } + + public synchronized Class getArrayClass(Class componentType) { + Class arrayClass = arrayClasses.get(componentType); + if (arrayClass == null) { + arrayClass = Array.newInstance(componentType, 0).getClass(); + arrayClasses.put(componentType, arrayClass); + } + return arrayClass; + } + + public static int dimensions(Class c) { + if (c.getComponentType() != null) { + return 1 + dimensions(c.getComponentType()); + } + return 0; + } + + private static void addClass(Class c) { + if (classes.add(c)) { + if (c.getSuperclass() != null) { + addClass(c.getSuperclass()); + } + for (Class sc : c.getInterfaces()) { + addClass(sc); + } + for (Class dc : c.getDeclaredClasses()) { + addClass(dc); + } + for (Method m : c.getDeclaredMethods()) { + addClass(m.getReturnType()); + for (Class p : m.getParameterTypes()) { + addClass(p); + } + } + + if (c != void.class && dimensions(c) < 2) { + Class arrayClass = Array.newInstance(c, 0).getClass(); + arrayClasses.put(c, arrayClass); + addClass(arrayClass); + } + } + } +} diff -r b45e0f791465 -r 381ab4105afe mx/suite.py --- a/mx/suite.py Tue Jun 02 15:15:23 2015 +0200 +++ b/mx/suite.py Tue Jun 02 15:15:58 2015 +0200 @@ -180,6 +180,19 @@ "workingSets" : "API,JVMCI", }, + "com.oracle.jvmci.runtime.test" : { + "subDir" : "graal", + "sourceDirs" : ["src"], + "dependencies" : [ + "JUNIT", + "com.oracle.jvmci.common", + "com.oracle.jvmci.runtime", + ], + "checkstyle" : "com.oracle.graal.graph", + "javaCompliance" : "1.8", + "workingSets" : "API,JVMCI", + }, + "com.oracle.jvmci.hotspot" : { "subDir" : "graal", "sourceDirs" : ["src"], @@ -845,6 +858,7 @@ "sourceDirs" : ["src"], "dependencies" : [ "JUNIT", + "com.oracle.jvmci.runtime.test", "com.oracle.graal.runtime", "com.oracle.graal.java", ],