TestConstantReflectionProvider.java revision 12651:6ef01bd40ce2
1234353Sdim/*
2224133Sdim * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3224133Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4224133Sdim *
5224133Sdim * This code is free software; you can redistribute it and/or modify it
6224133Sdim * under the terms of the GNU General Public License version 2 only, as
7224133Sdim * published by the Free Software Foundation.
8224133Sdim *
9224133Sdim * This code is distributed in the hope that it will be useful, but WITHOUT
10224133Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11224133Sdim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12224133Sdim * version 2 for more details (a copy is included in the LICENSE file that
13224133Sdim * accompanied this code).
14224133Sdim *
15249423Sdim * You should have received a copy of the GNU General Public License version
16224133Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17226633Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18224133Sdim *
19224133Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20224133Sdim * or visit www.oracle.com if you need additional information or have any
21226633Sdim * questions.
22224133Sdim */
23224133Sdim
24224133Sdim/**
25224133Sdim * @test
26224133Sdim * @requires vm.jvmci
27224133Sdim * @library ../../../../../
28224133Sdim * @modules jdk.internal.vm.ci/jdk.vm.ci.meta
29224133Sdim *          jdk.internal.vm.ci/jdk.vm.ci.runtime
30224133Sdim *          java.base/jdk.internal.misc
31224133Sdim * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestConstantReflectionProvider
32224133Sdim */
33224133Sdim
34224133Sdimpackage jdk.vm.ci.runtime.test;
35224133Sdim
36224133Sdimimport jdk.vm.ci.meta.ConstantReflectionProvider;
37224133Sdimimport jdk.vm.ci.meta.JavaConstant;
38224133Sdimimport jdk.vm.ci.meta.JavaKind;
39224133Sdimimport org.junit.Test;
40226633Sdim
41226633Sdimimport java.lang.reflect.Array;
42226633Sdimimport java.util.List;
43226633Sdim
44224133Sdimimport static org.junit.Assert.assertEquals;
45224133Sdimimport static org.junit.Assert.assertFalse;
46224133Sdimimport static org.junit.Assert.assertNotNull;
47224133Sdimimport static org.junit.Assert.assertNull;
48224133Sdimimport static org.junit.Assert.assertTrue;
49224133Sdim
50224133Sdim/**
51224133Sdim * Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that
52224133Sdim * actually returns non-null results for access operations that are possible, i.e., the tests will
53226633Sdim * fail for an implementation that spuriously returns null (which is allowed by the specification).
54234353Sdim */
55234353Sdimpublic class TestConstantReflectionProvider extends TypeUniverse {
56226633Sdim
57234353Sdim    @Test
58226633Sdim    public void constantEqualsTest() {
59226633Sdim        for (ConstantValue c1 : constants()) {
60226633Sdim            for (ConstantValue c2 : constants()) {
61226633Sdim                // test symmetry
62226633Sdim                assertEquals(constantReflection.constantEquals(c1.value, c2.value), constantReflection.constantEquals(c2.value, c1.value));
63226633Sdim                if (c1.value.getJavaKind() != JavaKind.Object && c2.value.getJavaKind() != JavaKind.Object) {
64234353Sdim                    assertEquals(c1.value.equals(c2.value), constantReflection.constantEquals(c2.value, c1.value));
65234353Sdim                }
66226633Sdim            }
67226633Sdim        }
68234353Sdim    }
69226633Sdim
70226633Sdim    @Test
71226633Sdim    public void readArrayLengthTest() {
72226633Sdim        for (ConstantValue cv : constants()) {
73226633Sdim            JavaConstant c = cv.value;
74226633Sdim            Integer actual = constantReflection.readArrayLength(c);
75226633Sdim            if (c.getJavaKind() != JavaKind.Object || c.isNull() || !cv.boxed.getClass().isArray()) {
76226633Sdim                assertNull(actual);
77226633Sdim            } else {
78226633Sdim                assertNotNull(actual);
79226633Sdim                int actualInt = actual;
80226633Sdim                assertEquals(Array.getLength(cv.boxed), actualInt);
81226633Sdim            }
82226633Sdim        }
83226633Sdim    }
84226633Sdim
85226633Sdim    static class PrimitiveConstants {
86226633Sdim        static final long LONG_CONST = 42;
87226633Sdim        static final int INT_CONST = 66;
88224133Sdim        static final byte BYTE_CONST = 123;
89224133Sdim        static final boolean BOOL_CONST = true;
90224133Sdim    }
91226633Sdim
92226633Sdim    static class BoxedConstants {
93226633Sdim        static final Long LONG_CONST = 42L;
94224133Sdim        static final Integer INT_CONST = 66;
95        static final Byte BYTE_CONST = 123;
96        static final Boolean BOOL_CONST = true;
97    }
98
99    @Test
100    public void boxTest() {
101        for (ConstantValue cv : constants()) {
102            JavaConstant c = cv.value;
103            JavaConstant boxed = constantReflection.boxPrimitive(c);
104            if (boxed != null && c.getJavaKind().isPrimitive()) {
105                assertTrue(boxed.getJavaKind().isObject());
106                assertFalse(boxed.isNull());
107            }
108        }
109
110        List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
111        List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
112        for (int i = 0; i < primitiveConstants.size(); i++) {
113            ConstantValue prim = primitiveConstants.get(i);
114            ConstantValue box = boxedConstants.get(i);
115            assertEquals(box.value, constantReflection.boxPrimitive(prim.value));
116        }
117
118        assertNull(constantReflection.boxPrimitive(JavaConstant.NULL_POINTER));
119    }
120
121    @Test
122    public void unboxTest() {
123        for (ConstantValue cv : constants()) {
124            JavaConstant c = cv.value;
125            JavaConstant unboxed = c.isNull() ? null : constantReflection.unboxPrimitive(c);
126            if (unboxed != null) {
127                assertFalse(unboxed.getJavaKind().isObject());
128            }
129        }
130        List<ConstantValue> primitiveConstants = readConstants(PrimitiveConstants.class);
131        List<ConstantValue> boxedConstants = readConstants(BoxedConstants.class);
132        for (int i = 0; i < primitiveConstants.size(); i++) {
133            ConstantValue prim = primitiveConstants.get(i);
134            ConstantValue box = boxedConstants.get(i);
135            assert prim.getSimpleName().equals(box.getSimpleName());
136            assertEquals(prim.value, constantReflection.unboxPrimitive(box.value));
137        }
138
139        assertNull(constantReflection.unboxPrimitive(JavaConstant.NULL_POINTER));
140    }
141}
142