1 /*
2 * Copyright (c) 2012, 2019, 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 */
51 import static org.junit.Assert.assertEquals;
52 import static org.junit.Assert.assertNotNull;
53 import static org.junit.Assert.assertNull;
54 import static org.junit.Assert.assertTrue;
55
56 /**
57 * Tests for {@link MetaAccessProvider}.
58 */
59 public class TestMetaAccessProvider extends TypeUniverse {
60 private static final DeoptimizationAction DEOPT_ACTION = DeoptimizationAction.InvalidateRecompile;
61 private static final DeoptimizationReason DEOPT_REASON = DeoptimizationReason.Aliasing;
62 private static final int INT_23BITS_SET = 0x7FFFFF;
63 private static final int[] DEBUG_IDS = new int[]{0, 1, 42, INT_23BITS_SET};
64 private static final int[] VALID_ENCODED_VALUES = new int[]{
65 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[0]).asInt(),
66 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[1]).asInt(),
67 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[2]).asInt(),
68 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[3]).asInt()
69 };
70
71 private static boolean isUnsafeAnoymous(ResolvedJavaType type) {
72 return type.getHostClass() != null;
73 }
74
75 @Test
76 public void lookupJavaTypeTest() {
77 for (Class<?> c : classes) {
78 ResolvedJavaType type = metaAccess.lookupJavaType(c);
79 assertNotNull(c.toString(), type);
80 if (!isUnsafeAnoymous(type)) {
81 assertEquals(c.toString(), type.getName(), toInternalName(c.getName()));
82 assertEquals(c.toString(), type.getName(), toInternalName(type.toJavaName()));
83 assertEquals(c.toString(), c.getName(), type.toClassName());
84 if (!type.isArray()) {
85 assertEquals(c.toString(), c.getName(), type.toJavaName());
86 }
87 }
88 }
89 }
90
91 @Test(expected = IllegalArgumentException.class)
92 public void lookupJavaTypeNegativeTest() {
93 metaAccess.lookupJavaType((Class<?>) null);
94 }
95
96 @Test
97 public void lookupJavaTypesTest() {
98 ResolvedJavaType[] result = metaAccess.lookupJavaTypes(classes.toArray(new Class<?>[classes.size()]));
99 int counter = 0;
100 for (Class<?> aClass : classes) {
101 if (!isUnsafeAnoymous(result[counter])) {
102 assertEquals("Unexpected javaType: " + result[counter] + " while expecting of class: " + aClass, result[counter].toClassName(), aClass.getName());
103 }
104 counter++;
105 }
106 }
107
108 @Test(expected = NullPointerException.class)
109 public void lookupJavaTypesNegative1Test() {
110 assertNull("Expected null", metaAccess.lookupJavaTypes(null));
111 }
112
113 @Test(expected = IllegalArgumentException.class)
114 public void lookupJavaTypesNegative2Test() {
115 ResolvedJavaType[] result = metaAccess.lookupJavaTypes(new Class<?>[]{null, null, null});
116 for (ResolvedJavaType aType : result) {
117 assertNull("Expected null javaType", aType);
118 }
119 result = metaAccess.lookupJavaTypes(new Class<?>[]{String.class, String.class});
120 assertEquals("Results not equals", result[0].getClass(), result[1].getClass());
121 assertEquals("Result is not String.class", result[0].getClass(), String.class);
|
1 /*
2 * Copyright (c) 2012, 2020, 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 */
51 import static org.junit.Assert.assertEquals;
52 import static org.junit.Assert.assertNotNull;
53 import static org.junit.Assert.assertNull;
54 import static org.junit.Assert.assertTrue;
55
56 /**
57 * Tests for {@link MetaAccessProvider}.
58 */
59 public class TestMetaAccessProvider extends TypeUniverse {
60 private static final DeoptimizationAction DEOPT_ACTION = DeoptimizationAction.InvalidateRecompile;
61 private static final DeoptimizationReason DEOPT_REASON = DeoptimizationReason.Aliasing;
62 private static final int INT_23BITS_SET = 0x7FFFFF;
63 private static final int[] DEBUG_IDS = new int[]{0, 1, 42, INT_23BITS_SET};
64 private static final int[] VALID_ENCODED_VALUES = new int[]{
65 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[0]).asInt(),
66 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[1]).asInt(),
67 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[2]).asInt(),
68 metaAccess.encodeDeoptActionAndReason(DEOPT_ACTION, DEOPT_REASON, DEBUG_IDS[3]).asInt()
69 };
70
71 private static boolean isHiddenClass(Class<?> cls) {
72 if (cls.isHiddenClass()) {
73 return true;
74 }
75
76 // Check array of hidden type.
77 while (cls.getComponentType() != null) {
78 cls = cls.getComponentType();
79 }
80 if (cls.isHiddenClass()) {
81 return true;
82 }
83 return false;
84 }
85
86 private static boolean isUnsafeAnonymous(ResolvedJavaType type) {
87 return type.getHostClass() != null;
88 }
89
90 @Test
91 public void lookupJavaTypeTest() {
92 for (Class<?> c : classes) {
93 ResolvedJavaType type = metaAccess.lookupJavaType(c);
94 assertNotNull(c.toString(), type);
95 if (!isHiddenClass(c) && !isUnsafeAnonymous(type)) {
96 assertEquals(c.toString(), type.getName(), toInternalName(c.getName()));
97 assertEquals(c.toString(), type.getName(), toInternalName(type.toJavaName()));
98 assertEquals(c.toString(), c.getName(), type.toClassName());
99 if (!type.isArray()) {
100 assertEquals(c.toString(), c.getName(), type.toJavaName());
101 }
102 }
103 }
104 }
105
106 @Test(expected = IllegalArgumentException.class)
107 public void lookupJavaTypeNegativeTest() {
108 metaAccess.lookupJavaType((Class<?>) null);
109 }
110
111 @Test
112 public void lookupJavaTypesTest() {
113 ResolvedJavaType[] result = metaAccess.lookupJavaTypes(classes.toArray(new Class<?>[classes.size()]));
114 int counter = 0;
115 for (Class<?> aClass : classes) {
116 if (!isHiddenClass(aClass) && !isUnsafeAnonymous(result[counter])) {
117 assertEquals("Unexpected javaType: " + result[counter] + " while expecting of class: " + aClass, result[counter].toClassName(), aClass.getName());
118 }
119 counter++;
120 }
121 }
122
123 @Test(expected = NullPointerException.class)
124 public void lookupJavaTypesNegative1Test() {
125 assertNull("Expected null", metaAccess.lookupJavaTypes(null));
126 }
127
128 @Test(expected = IllegalArgumentException.class)
129 public void lookupJavaTypesNegative2Test() {
130 ResolvedJavaType[] result = metaAccess.lookupJavaTypes(new Class<?>[]{null, null, null});
131 for (ResolvedJavaType aType : result) {
132 assertNull("Expected null javaType", aType);
133 }
134 result = metaAccess.lookupJavaTypes(new Class<?>[]{String.class, String.class});
135 assertEquals("Results not equals", result[0].getClass(), result[1].getClass());
136 assertEquals("Result is not String.class", result[0].getClass(), String.class);
|