1 /* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64") 27 * @compile TestConstantReflectionProvider.java TypeUniverse.java TestMetaAccessProvider.java 28 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestConstantReflectionProvider 29 */ 30 31 package jdk.vm.ci.runtime.test; 32 33 import static org.junit.Assert.assertEquals; 34 import static org.junit.Assert.assertFalse; 35 import static org.junit.Assert.assertNotNull; 36 import static org.junit.Assert.assertNull; 37 import static org.junit.Assert.assertTrue; 38 39 import java.lang.reflect.Array; 40 import java.util.List; 41 42 import jdk.vm.ci.meta.ConstantReflectionProvider; 43 import jdk.vm.ci.meta.JavaConstant; 44 import jdk.vm.ci.meta.JavaKind; 45 46 import org.junit.Test; 47 48 /** 49 * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that 50 * actually returns non-null results for access operations that are possible, i.e., the tests will 51 * fail for an implementation that spuriously returns null (which is allowed by the specification). 52 */ 53 public class TestConstantReflectionProvider extends TypeUniverse { 54 55 @Test 56 public void constantEqualsTest() { 57 for (ConstantValue c1 : constants()) { 58 for (ConstantValue c2 : constants()) { 59 // test symmetry 60 assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value)); 61 if (c1.value.getJavaKind() != JavaKind.Object && c2.value.getJavaKind() != JavaKind.Object) { 62 assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value)); 63 } 64 } 65 } 66 } 67 68 @Test 69 public void readArrayLengthTest() { 70 for (ConstantValue cv : constants()) { 71 JavaConstant c = cv.value; 72 Integer actual = constantReflection.readArrayLength(c); 73 if (c.getJavaKind() != JavaKind.Object || c.isNull() || !cv.boxed.getClass().isArray()) { 74 assertNull(actual); 75 } else { 76 assertNotNull(actual); 77 int actualInt = actual; 78 assertEquals(Array.getLength(cv.boxed), actualInt); 79 } 80 } 81 } 82 83 static class PrimitiveConstants { 84 static final long LONG_CONST = 42; 85 static final int INT_CONST = 66; 86 static final byte BYTE_CONST = 123; 87 static final boolean BOOL_CONST = true; 88 } 89 90 static class BoxedConstants { 91 static final Long LONG_CONST = 42L; 92 static final Integer INT_CONST = 66; 93 static final Byte BYTE_CONST = 123; 94 static final Boolean BOOL_CONST = true; 95 } 96 97 @Test 98 public void boxTest() { 99 for (ConstantValue cv : constants()) { 100 JavaConstant c = cv.value; 101 JavaConstant boxed = constantReflection.boxPrimitive(c); 102 if (boxed != null && c.getJavaKind().isPrimitive()) { 103 assertTrue(boxed.getJavaKind().isObject()); 104 assertFalse(boxed.isNull()); 105 } 106 } 107 108 List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class); 109 List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class); 110 for (int i = 0; i < primitiveConstants.size(); i++) { 111 ConstantValue prim = primitiveConstants.get(i); 112 ConstantValue box = boxedConstants.get(i); 113 assertEquals(box.value, constantReflection.boxPrimitive(prim.value)); 114 } 115 116 assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER)); 117 } 118 119 @Test 120 public void unboxTest() { 121 for (ConstantValue cv : constants()) { 122 JavaConstant c = cv.value; 123 JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c); 124 if (unboxed != null) { 125 assertFalse(unboxed.getJavaKind().isObject()); 126 } 127 } 128 List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class); 129 List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class); 130 for (int i = 0; i < primitiveConstants.size(); i++) { 131 ConstantValue prim = primitiveConstants.get(i); 132 ConstantValue box = boxedConstants.get(i); 133 assert prim.getSimpleName().equals(box.getSimpleName()); 134 assertEquals(prim.value, constantReflection.unboxPrimitive(box.value)); 135 } 136 137 assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER)); 138 } 139 }