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 * @compile TestConstantReflectionProvider.java TypeUniverse.java TestMetaAccessProvider.java 27 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.internal.jvmci.runtime.test.TestConstantReflectionProvider 28 */ 29 30 package jdk.internal.jvmci.runtime.test; 31 32 import static org.junit.Assert.*; 33 34 import java.lang.reflect.*; 35 import java.util.*; 36 37 import jdk.internal.jvmci.meta.*; 38 39 import org.junit.*; 40 41 /** 42 * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that 43 * actually returns non-null results for access operations that are possible, i.e., the tests will 44 * fail for an implementation that spuriously returns null (which is allowed by the specification). 45 */ 46 public class TestConstantReflectionProvider extends TypeUniverse { 47 48 @Test 49 public void constantEqualsTest() { 50 for (ConstantValue c1 : constants()) { 51 for (ConstantValue c2 : constants()) { 52 // test symmetry 53 assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value)); 54 if (c1.value.getJavaKind() != JavaKind.Object && c2.value.getJavaKind() != JavaKind.Object) { 55 assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value)); 56 } 57 } 58 } 59 } 60 61 @Test 62 public void readArrayLengthTest() { 63 for (ConstantValue cv : constants()) { 64 JavaConstant c = cv.value; 65 Integer actual = constantReflection.readArrayLength(c); 66 if (c.getJavaKind() != JavaKind.Object || c.isNull() || !cv.boxed.getClass().isArray()) { 67 assertNull(actual); 68 } else { 69 assertNotNull(actual); 70 int actualInt = actual; 71 assertEquals(Array.getLength(cv.boxed), actualInt); 72 } 73 } 74 } 75 76 static class PrimitiveConstants { 77 static final long LONG_CONST = 42; 78 static final int INT_CONST = 66; 79 static final byte BYTE_CONST = 123; 80 static final boolean BOOL_CONST = true; 81 } 82 83 static class BoxedConstants { 84 static final Long LONG_CONST = 42L; 85 static final Integer INT_CONST = 66; 86 static final Byte BYTE_CONST = 123; 87 static final Boolean BOOL_CONST = true; 88 } 89 90 @Test 91 public void boxTest() { 92 for (ConstantValue cv : constants()) { 93 JavaConstant c = cv.value; 94 JavaConstant boxed = constantReflection.boxPrimitive(c); 95 if (boxed != null && c.getJavaKind().isPrimitive()) { 96 assertTrue(boxed.getJavaKind().isObject()); 97 assertFalse(boxed.isNull()); 98 } 99 } 100 101 List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class); 102 List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class); 103 for (int i = 0; i < primitiveConstants.size(); i++) { 104 ConstantValue prim = primitiveConstants.get(i); 105 ConstantValue box = boxedConstants.get(i); 106 assertEquals(box.value, constantReflection.boxPrimitive(prim.value)); 107 } 108 109 assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER)); 110 } 111 112 @Test 113 public void unboxTest() { 114 for (ConstantValue cv : constants()) { 115 JavaConstant c = cv.value; 116 JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c); 117 if (unboxed != null) { 118 assertFalse(unboxed.getJavaKind().isObject()); 119 } 120 } 121 List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class); 122 List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class); 123 for (int i = 0; i < primitiveConstants.size(); i++) { 124 ConstantValue prim = primitiveConstants.get(i); 125 ConstantValue box = boxedConstants.get(i); 126 assert prim.getSimpleName().equals(box.getSimpleName()); 127 assertEquals(prim.value, constantReflection.unboxPrimitive(box.value)); 128 } 129 130 assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER)); 131 } 132 }