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 * @bug 8141615
27 * @summary Tests new public methods at ConstantPool
28 * @modules java.base/jdk.internal.misc
29 * java.base/jdk.internal.reflect
30 * @library /test/lib
31 * @compile ConstantPoolTestDummy.jasm
32 * @run main jdk.internal.reflect.constantPool.ConstantPoolTest
33 */
34
35 package jdk.internal.reflect.constantPool;
36
37 import java.util.HashMap;
38 import java.util.Map;
39 import jdk.internal.misc.SharedSecrets;
40 import jdk.test.lib.Asserts;
41 import jdk.internal.reflect.ConstantPool;
42
43 public class ConstantPoolTest {
44
45 private static final Class<?> TEST_CLASS = ConstantPoolTestDummy.class;
46 private static final ConstantPool CP = SharedSecrets.getJavaLangAccess()
47 .getConstantPool(TEST_CLASS);
48
49 public static void main(String[] s) {
50 for (TestCase testCase : TestCase.values()) {
51 testCase.test();
52 }
53 }
54
55 public static enum TestCase {
56 GET_TAG_AT {
57 {
58 referenceMap.put(1, ConstantPool.Tag.METHODREF);
59 referenceMap.put(2, ConstantPool.Tag.CLASS);
60 referenceMap.put(4, ConstantPool.Tag.UTF8);
61 referenceMap.put(10, ConstantPool.Tag.NAMEANDTYPE);
62 referenceMap.put(13, ConstantPool.Tag.LONG);
63 referenceMap.put(15, ConstantPool.Tag.INTEGER);
64 referenceMap.put(16, ConstantPool.Tag.INTERFACEMETHODREF);
65 referenceMap.put(21, ConstantPool.Tag.DOUBLE);
66 referenceMap.put(23, ConstantPool.Tag.STRING);
67 referenceMap.put(25, ConstantPool.Tag.INVOKEDYNAMIC);
68 referenceMap.put(29, ConstantPool.Tag.METHODHANDLE);
69 referenceMap.put(30, ConstantPool.Tag.METHODTYPE);
70 referenceMap.put(48, ConstantPool.Tag.FIELDREF);
71 referenceMap.put(52, ConstantPool.Tag.FLOAT);
72 }
73 @Override
74 void testIndex(int cpi, Object reference) {
75 ConstantPool.Tag tagToVerify = CP.getTagAt(cpi);
76 ConstantPool.Tag tagToRefer = (ConstantPool.Tag) reference;
77 String msg = String.format("Method getTagAt works not as expected"
78 + "at CP entry #%d: got CP tag %s, but should be %s",
79 cpi, tagToVerify.name(), tagToRefer.name());
80 Asserts.assertEquals(tagToVerify, tagToRefer, msg);
81 }
82 },
83 GET_CLASS_REF_INDEX_AT {
84 {
85 referenceMap.put(1, 3);
86 referenceMap.put(16, 17);
87 referenceMap.put(32, 35);
88 referenceMap.put(34, 3);
89 referenceMap.put(48, 2);
90 }
91 @Override
92 void testIndex(int cpi, Object reference) {
93 int indexToVerify = CP.getClassRefIndexAt(cpi);
94 int indexToRefer = (int) reference;
95 String msg = String.format("Method getClassRefIndexAt works not"
96 + " as expected at CP entry #%d:"
97 + " got index %d, but should be %d",
98 cpi, indexToVerify, indexToRefer);
99 Asserts.assertEquals(indexToVerify, indexToRefer, msg);
100 }
101 },
102 GET_NAME_AND_TYPE_REF_INDEX_AT {
144 msg = String.format("Method getNameAndTypeRefInfoAt"
145 + " works not as expected at CP entry #%d:"
146 + " length of the returned string array is %d, but should be 2",
147 cpi, natInfoLength);
148 Asserts.assertEquals(natInfoLength, 2, msg);
149 String[] nameOrType = new String[]{"name", "type"};
150 for (int i = 0; i < 2; i++) {
151 String infoToVerify = natInfo[i];
152 String infoToRefer = castedReference[i];
153 msg = String.format("Method getNameAndTypeRefInfoAt"
154 + " works not as expected at CP entry #%d:"
155 + " got %s info %s, but should be %s",
156 cpi, nameOrType[i], infoToVerify, infoToRefer);
157 Asserts.assertEquals(infoToVerify, infoToRefer, msg);
158 }
159 }
160 };
161
162 protected final Map<Integer, Object> referenceMap;
163 TestCase() {
164 this.referenceMap = new HashMap<>();
165 }
166 abstract void testIndex(int cpi, Object reference);
167 public void test() {
168 referenceMap.forEach(this::testIndex);
169 }
170 }
171 }
|
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 * @bug 8141615
27 * @summary Tests new public methods at ConstantPool
28 * @modules java.base/jdk.internal.misc
29 * java.base/jdk.internal.reflect
30 * @library /test/lib
31 * @compile ConstantPoolTestDummy.jasm
32 * @run main jdk.internal.reflect.constantPool.ConstantPoolTest
33 */
34
35 package jdk.internal.reflect.constantPool;
36
37 import java.util.TreeMap;
38 import java.util.Map;
39 import jdk.internal.misc.SharedSecrets;
40 import jdk.test.lib.Asserts;
41 import jdk.internal.reflect.ConstantPool;
42
43 public class ConstantPoolTest {
44
45 private static final Class<?> TEST_CLASS = ConstantPoolTestDummy.class;
46 private static final ConstantPool CP = SharedSecrets.getJavaLangAccess()
47 .getConstantPool(TEST_CLASS);
48
49 public static void main(String[] s) throws Throwable {
50 TEST_CLASS.newInstance();
51 for (TestCase testCase : TestCase.values()) {
52 testCase.test();
53 }
54 }
55
56 public static enum TestCase {
57 GET_TAG_AT {
58 {
59 referenceMap.put(1, ConstantPool.Tag.METHODREF);
60 referenceMap.put(2, ConstantPool.Tag.CLASS);
61 referenceMap.put(4, ConstantPool.Tag.UTF8);
62 referenceMap.put(10, ConstantPool.Tag.NAMEANDTYPE);
63 referenceMap.put(13, ConstantPool.Tag.LONG);
64 referenceMap.put(15, ConstantPool.Tag.INTEGER);
65 referenceMap.put(16, ConstantPool.Tag.INTERFACEMETHODREF);
66 referenceMap.put(21, ConstantPool.Tag.DOUBLE);
67 referenceMap.put(23, ConstantPool.Tag.STRING);
68 referenceMap.put(25, ConstantPool.Tag.INVOKEDYNAMIC);
69 referenceMap.put(29, ConstantPool.Tag.METHODHANDLE);
70 referenceMap.put(30, ConstantPool.Tag.METHODTYPE);
71 referenceMap.put(48, ConstantPool.Tag.FIELDREF);
72 referenceMap.put(52, ConstantPool.Tag.FLOAT);
73 referenceMap.put(53, ConstantPool.Tag.DYNAMIC);
74 }
75 @Override
76 void testIndex(int cpi, Object reference) {
77 ConstantPool.Tag tagToVerify = CP.getTagAt(cpi);
78 ConstantPool.Tag tagToRefer = (ConstantPool.Tag) reference;
79 String msg = String.format("Method getTagAt works not as expected"
80 + "at CP entry #%d: got CP tag %s, but should be %s",
81 cpi, tagToVerify.name(), tagToRefer.name());
82 Asserts.assertEquals(tagToVerify, tagToRefer, msg);
83 }
84 },
85 GET_CONSTANT_AT {
86 {
87 referenceMap.put(3, Object.class);
88 referenceMap.put(13, (long)6);
89 referenceMap.put(15, (int)1);
90 referenceMap.put(17, Runnable.class);
91 referenceMap.put(21, (double)1.45);
92 referenceMap.put(23, "Hello");
93 referenceMap.put(31, "[a method handle]");
94 referenceMap.put(52, (float)1.34);
95 //referenceMap.put(53, "[a dynamic constant]");
96 }
97 @Override
98 void testIndex(int cpi, Object reference) {
99 Object entryToVerify = CP.getConstantAt(cpi);
100 Object entryToRefer = reference;
101 if (cpi == 31 && entryToVerify instanceof java.lang.invoke.MethodHandle) {
102 // const #31 = MethodHandle 5:#34; // REF_invokeVirtual:java/lang/Object.toString:"()Ljava/lang/String;"
103 entryToVerify = entryToRefer; // good enough
104 }
105 String msg = String.format("Method getConstantAt works not"
106 + " as expected at CP entry #%d:"
107 + " got %s, but should be %s",
108 cpi, entryToVerify, entryToRefer);
109 Asserts.assertEquals(entryToVerify, entryToRefer, msg);
110 }
111 },
112 GET_CONSTANTDESC_AT {
113 {
114 referenceMap.put(3, Object.class);
115 referenceMap.put(13, (long)6);
116 referenceMap.put(15, (int)1);
117 referenceMap.put(17, Runnable.class);
118 referenceMap.put(21, (double)1.45);
119 referenceMap.put(23, "Hello");
120 referenceMap.put(31, "[a method handle]");
121 referenceMap.put(52, (float)1.34);
122 referenceMap.put(53, "[a dynamic constant]");
123 }
124 @Override
125 void testIndex(int cpi, Object reference) {
126 Object entryToVerify = CP.getConstantDescAt(cpi);
127 Object entryToRefer = ((java.lang.constant.Constable<?>)reference).describeConstable().orElse(null);
128 if (cpi == 31 && entryToVerify instanceof java.lang.constant.MethodHandleDesc) {
129 String str = entryToVerify.toString();
130 // MethodHandleDesc[VIRTUAL/Object::toString()String]
131 if (str.contains("toString") &&
132 str.contains("Object") &&
133 str.contains("()") &&
134 (str.contains("virtual") || str.contains("Virtual") || str.contains("VIRTUAL")))
135 // close enough
136 entryToVerify = entryToRefer;
137 }
138 if (cpi == 53 && entryToVerify instanceof java.lang.constant.DynamicConstantDesc) {
139 // DynamicConstantDesc[LambdaMetafactory::metafactory(
140 // toString/MethodTypeDesc[(Object)void],
141 // MethodHandleDesc[VIRTUAL/Object::toString()String],
142 // MethodTypeDesc[(Object)void]
143 // )Object],
144 String str = entryToVerify.toString();
145 if (str.contains("toString") &&
146 str.contains("metafactory") &&
147 str.contains("(Object)void"))
148 // close enough
149 entryToVerify = entryToRefer;
150 }
151 String msg = String.format("Method getConstantDescAt works not"
152 + " as expected at CP entry #%d:"
153 + " got %s, but should be %s",
154 cpi, entryToVerify, entryToRefer);
155 Asserts.assertEquals(entryToVerify, entryToRefer, msg);
156 }
157 },
158 GET_CLASS_AT {
159 {
160 referenceMap.put(3, Object.class);
161 referenceMap.put(17, Runnable.class);
162 }
163 @Override
164 void testIndex(int cpi, Object reference) {
165 Object entryToVerify = CP.getClassAt(cpi);
166 Object entryToRefer = reference;
167 String msg = String.format("Method getClassAt works not"
168 + " as expected at CP entry #%d:"
169 + " got %s, but should be %s",
170 cpi, entryToVerify, entryToRefer);
171 Asserts.assertEquals(entryToVerify, entryToRefer, msg);
172 }
173 },
174 GET_CLASS_REF_INDEX_AT {
175 {
176 referenceMap.put(1, 3);
177 referenceMap.put(16, 17);
178 referenceMap.put(32, 35);
179 referenceMap.put(34, 3);
180 referenceMap.put(48, 2);
181 }
182 @Override
183 void testIndex(int cpi, Object reference) {
184 int indexToVerify = CP.getClassRefIndexAt(cpi);
185 int indexToRefer = (int) reference;
186 String msg = String.format("Method getClassRefIndexAt works not"
187 + " as expected at CP entry #%d:"
188 + " got index %d, but should be %d",
189 cpi, indexToVerify, indexToRefer);
190 Asserts.assertEquals(indexToVerify, indexToRefer, msg);
191 }
192 },
193 GET_NAME_AND_TYPE_REF_INDEX_AT {
235 msg = String.format("Method getNameAndTypeRefInfoAt"
236 + " works not as expected at CP entry #%d:"
237 + " length of the returned string array is %d, but should be 2",
238 cpi, natInfoLength);
239 Asserts.assertEquals(natInfoLength, 2, msg);
240 String[] nameOrType = new String[]{"name", "type"};
241 for (int i = 0; i < 2; i++) {
242 String infoToVerify = natInfo[i];
243 String infoToRefer = castedReference[i];
244 msg = String.format("Method getNameAndTypeRefInfoAt"
245 + " works not as expected at CP entry #%d:"
246 + " got %s info %s, but should be %s",
247 cpi, nameOrType[i], infoToVerify, infoToRefer);
248 Asserts.assertEquals(infoToVerify, infoToRefer, msg);
249 }
250 }
251 };
252
253 protected final Map<Integer, Object> referenceMap;
254 TestCase() {
255 // Use a TreeMap for deterministic test order.
256 this.referenceMap = new TreeMap<>();
257 }
258 abstract void testIndex(int cpi, Object reference);
259 public void test() {
260 System.out.println(this);
261 referenceMap.forEach(this::testIndex);
262 }
263 }
264 }
|