TestMetaAccessProvider.java revision 11707:ad7af1afda7a
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 (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64")
27 * @library ../../../../../
28 * @modules jdk.vm.ci/jdk.vm.ci.meta
29 *          jdk.vm.ci/jdk.vm.ci.runtime
30 *          java.base/jdk.internal.misc
31 * @build jdk.vm.ci.runtime.test.TestMetaAccessProvider
32 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.TestMetaAccessProvider
33 */
34
35package jdk.vm.ci.runtime.test;
36
37import jdk.vm.ci.meta.DeoptimizationAction;
38import jdk.vm.ci.meta.DeoptimizationReason;
39import jdk.vm.ci.meta.JavaConstant;
40import jdk.vm.ci.meta.JavaKind;
41import jdk.vm.ci.meta.MetaAccessProvider;
42import jdk.vm.ci.meta.ResolvedJavaField;
43import jdk.vm.ci.meta.ResolvedJavaMethod;
44import jdk.vm.ci.meta.ResolvedJavaType;
45import jdk.vm.ci.meta.Signature;
46import org.junit.Test;
47
48import java.lang.reflect.Field;
49import java.lang.reflect.Method;
50
51import static jdk.vm.ci.meta.MetaUtil.toInternalName;
52import static org.junit.Assert.assertEquals;
53import static org.junit.Assert.assertNotNull;
54import static org.junit.Assert.assertNull;
55import static org.junit.Assert.assertTrue;
56
57/**
58 * Tests for {@link MetaAccessProvider}.
59 */
60public class TestMetaAccessProvider extends TypeUniverse {
61    private static final DeoptimizationAction DEOPT_ACTION = DeoptimizationAction.InvalidateRecompile;
62    private static final DeoptimizationReason DEOPT_REASON = DeoptimizationReason.Aliasing;
63    private static final int INT_23BITS_SET = 0x7FFFFF;
64    private static final int[] DEBUG_IDS = new int[]{0, 1, 42, INT_23BITS_SET};
65    private static final int[] VALID_ENCODED_VALUES = new int[]{
66                    metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[0]).asInt(),
67                    metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[1]).asInt(),
68                    metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[2]).asInt(),
69                    metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[3]).asInt()
70    };
71
72    @Test
73    public void lookupJavaTypeTest() {
74        for (Class<?> c : classes) {
75            ResolvedJavaType type = metaAccess.lookupJavaType(c);
76            assertNotNull(c.toString(), type);
77            assertEquals(c.toString(), type.getName(), toInternalName(c.getName()));
78            assertEquals(c.toString(), type.getName(), toInternalName(type.toJavaName()));
79            assertEquals(c.toString(), c.getName(), type.toClassName());
80            if (!type.isArray()) {
81                assertEquals(c.toString(), c.getName(), type.toJavaName());
82            }
83        }
84    }
85
86    @Test(expected = IllegalArgumentException.class)
87    public void lookupJavaTypeNegativeTest() {
88        metaAccess.lookupJavaType((Class<?>) null);
89    }
90
91    @Test
92    public void lookupJavaTypesTest() {
93        ResolvedJavaType[] result = metaAccess.lookupJavaTypes(classes.toArray(new Class<?>[classes.size()]));
94        int counter = 0;
95        for (Class<?> aClass : classes) {
96            assertEquals("Unexpected javaType: " + result[counter] + " while expecting of class: " + aClass, result[counter].toClassName(), aClass.getName());
97            counter++;
98        }
99    }
100
101    @Test(expected = NullPointerException.class)
102    public void lookupJavaTypesNegative1Test() {
103        assertNull("Expected null", metaAccess.lookupJavaTypes(null));
104    }
105
106    @Test(expected = IllegalArgumentException.class)
107    public void lookupJavaTypesNegative2Test() {
108        ResolvedJavaType[] result = metaAccess.lookupJavaTypes(new Class<?>[]{null, null, null});
109        for (ResolvedJavaType aType : result) {
110            assertNull("Expected null javaType", aType);
111        }
112        result = metaAccess.lookupJavaTypes(new Class<?>[]{String.class, String.class});
113        assertEquals("Results not equals", result[0].getClass(), result[1].getClass());
114        assertEquals("Result is not String.class", result[0].getClass(), String.class);
115    }
116
117    @Test
118    public void lookupJavaMethodTest() {
119        for (Class<?> c : classes) {
120            for (Method reflect : c.getDeclaredMethods()) {
121                ResolvedJavaMethod method = metaAccess.lookupJavaMethod(reflect);
122                assertNotNull(method);
123                assertTrue(method.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
124            }
125        }
126    }
127
128    @Test(expected = NullPointerException.class)
129    public void lookupJavaMethodNegativeTest() {
130        metaAccess.lookupJavaMethod(null);
131    }
132
133    @Test
134    public void lookupJavaFieldTest() {
135        for (Class<?> c : classes) {
136            for (Field reflect : c.getDeclaredFields()) {
137                ResolvedJavaField field = metaAccess.lookupJavaField(reflect);
138                assertNotNull(field);
139                assertTrue(field.getDeclaringClass().equals(metaAccess.lookupJavaType(reflect.getDeclaringClass())));
140            }
141        }
142    }
143
144    @Test
145    public void lookupJavaTypeConstantTest() {
146        for (ConstantValue cv : constants()) {
147            JavaConstant c = cv.value;
148            if (c.getJavaKind() == JavaKind.Object && !c.isNull()) {
149                Object o = cv.boxed;
150                ResolvedJavaType type = metaAccess.lookupJavaType(c);
151                assertNotNull(type);
152                assertTrue(type.equals(metaAccess.lookupJavaType(o.getClass())));
153            } else {
154                assertEquals(metaAccess.lookupJavaType(c), null);
155            }
156        }
157    }
158
159    @Test(expected = NullPointerException.class)
160    public void lookupJavaTypeConstantNegativeTest() {
161        metaAccess.lookupJavaType((JavaConstant) null);
162    }
163
164    @Test
165    public void getMemorySizeTest() {
166        for (ConstantValue cv : constants()) {
167            JavaConstant c = cv.value;
168            long memSize = metaAccess.getMemorySize(c);
169            if (c.isNull()) {
170                assertEquals("Expected size = 0 for null", memSize, 0L);
171            } else {
172                assertTrue("Expected size != 0 for " + cv, memSize != 0L);
173            }
174        }
175    }
176
177    @Test(expected = NullPointerException.class)
178    public void getMemorySizeNegativeTest() {
179        metaAccess.getMemorySize(null);
180    }
181
182    @Test
183    public void parseMethodDescriptorTest() {
184        for (String retType : new String[]{"V", "Z", "Ljava/lang/String;"}) {
185            for (String paramTypes : new String[]{"", "B",
186                            "Ljava/lang/String;", "JLjava/lang/String;",
187                            "Ljava/lang/String;F",
188                            "[Ljava/lang/String;ZBCDFIJLS[ILjava/lang/Object;"}) {
189                String signature = "(" + paramTypes + ")" + retType;
190                Signature result = metaAccess.parseMethodDescriptor(signature);
191                assertEquals("Expected signatures to be equal", result.toMethodDescriptor(), signature);
192            }
193        }
194    }
195
196    @Test(expected = NullPointerException.class)
197    public void parseMethodDescriptorNegativeNullTest() {
198        metaAccess.parseMethodDescriptor(null);
199    }
200
201    @Test(expected = NullPointerException.class)
202    public void encodeDeoptActionAndReasonNegative1Test() {
203        metaAccess.encodeDeoptActionAndReason(null, DeoptimizationReason.Aliasing, 0);
204
205    }
206
207    @Test(expected = NullPointerException.class)
208    public void encodeDeoptActionAndReasonNegative2Test() {
209        metaAccess.encodeDeoptActionAndReason(DeoptimizationAction.InvalidateRecompile, null, 0);
210    }
211
212    @Test
213    public void decodeDeoptReasonTest() {
214        for (int encoded : VALID_ENCODED_VALUES) {
215            JavaConstant value = JavaConstant.forInt(encoded);
216            DeoptimizationReason reason = metaAccess.decodeDeoptReason(value);
217            assertEquals("Expected equal reasons", reason, DEOPT_REASON);
218        }
219    }
220
221    @Test
222    public void decodeDeoptReasonNegative1Test() {
223        int encoded = 42;
224        JavaConstant value = JavaConstant.forInt(encoded);
225        metaAccess.decodeDeoptReason(value);
226    }
227
228    @Test(expected = NullPointerException.class)
229    public void decodeDeoptReasonNegative2Test() {
230        metaAccess.decodeDeoptReason(null);
231    }
232
233    @Test
234    public void decodeDeoptActionTest() {
235        for (int encoded : VALID_ENCODED_VALUES) {
236            JavaConstant value = JavaConstant.forInt(encoded);
237            DeoptimizationAction action = metaAccess.decodeDeoptAction(value);
238            assertEquals("Expected equal actions", action, DEOPT_ACTION);
239        }
240    }
241
242    @Test
243    public void decodeDeoptActionNegative1Test() {
244        int encoded = 123456789;
245        JavaConstant value = JavaConstant.forInt(encoded);
246        metaAccess.decodeDeoptAction(value);
247    }
248
249    @Test(expected = NullPointerException.class)
250    public void decodeDeoptActionNegative2Test() {
251        metaAccess.decodeDeoptAction(null);
252    }
253
254    @Test
255    public void decodeDebugIdTest() {
256        for (int i = 0; i < VALID_ENCODED_VALUES.length; i++) {
257            JavaConstant value = JavaConstant.forInt(VALID_ENCODED_VALUES[i]);
258            assertEquals("Unexpected debugId", metaAccess.decodeDebugId(value), DEBUG_IDS[i]);
259        }
260    }
261}
262