1/*
2 * Copyright (c) 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 * @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 /lib/testlibrary
31 * @compile ConstantPoolTestDummy.jasm
32 * @run main jdk.internal.reflect.constantPool.ConstantPoolTest
33 */
34
35package jdk.internal.reflect.constantPool;
36
37import java.util.HashMap;
38import java.util.Map;
39import jdk.internal.misc.SharedSecrets;
40import jdk.testlibrary.Asserts;
41import jdk.internal.reflect.ConstantPool;
42
43public 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 {
103            {
104                referenceMap.put(1, 10);
105                referenceMap.put(16, 18);
106                referenceMap.put(25, 26);
107                referenceMap.put(32, 36);
108                referenceMap.put(34, 37);
109                referenceMap.put(48, 49);
110            }
111            @Override
112            void testIndex(int cpi, Object reference) {
113                int indexToRefer = (int) reference;
114                int indexToVerify = CP.getNameAndTypeRefIndexAt(cpi);
115                String msg = String.format("Method getNameAndTypeRefIndexAt works"
116                        + " not as expected at CP entry #%d:"
117                        + " got index %d, but should be %d",
118                        cpi, indexToVerify, indexToRefer);
119                Asserts.assertEquals(indexToVerify, indexToRefer, msg);
120            }
121        },
122        GET_NAME_AND_TYPE_REF_INFO_AT {
123            {
124                referenceMap.put(10, new String[]{"<init>", "()V"});
125                referenceMap.put(18, new String[]{"run", "()V"});
126                referenceMap.put(26, new String[]{"accept", "()Ljava/util/function/Consumer;"});
127                referenceMap.put(36, new String[]{"metafactory",
128                        "(Ljava/lang/invoke/MethodHandles$Lookup;"
129                        + "Ljava/lang/String;Ljava/lang/invoke/MethodType;"
130                        + "Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;"
131                        + "Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"});
132                referenceMap.put(37, new String[]{"toString", "()Ljava/lang/String;"});
133                referenceMap.put(49, new String[]{"myField", "I"});
134            }
135            @Override
136            void testIndex(int cpi, Object reference) {
137                String[] natInfo = CP.getNameAndTypeRefInfoAt(cpi);
138                String msg = String.format("Method getNameAndTypeRefInfoAt"
139                        + " works not as expected at CP entry #%d:"
140                        + " returned value should not be null", cpi);
141                Asserts.assertNotNull(natInfo, msg);
142                String[] castedReference = (String[]) reference;
143                int natInfoLength = natInfo.length;
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}
172