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 sun.reflect.ConstantPool
  28  * @modules java.base/sun.reflect
  29  * @library /lib/testlibrary
  30  * @compile ConstantPoolTestDummy.jasm
  31  * @run main sun.reflect.constantPool.ConstantPoolTest
  32  */
  33 
  34 package sun.reflect.constantPool;
  35 
  36 import java.util.HashMap;
  37 import java.util.Map;
  38 import jdk.internal.misc.SharedSecrets;
  39 import jdk.testlibrary.Asserts;
  40 import sun.reflect.ConstantPool;
  41 
  42 public class ConstantPoolTest {
  43 
  44     private static final Class<?> TEST_CLASS = ConstantPoolTestDummy.class;
  45     private static final ConstantPool CP = SharedSecrets.getJavaLangAccess()
  46             .getConstantPool(TEST_CLASS);
  47 
  48     public static void main(String[] s) {
  49         for (TestCase testCase : TestCase.values()) {
  50             testCase.test();
  51         }
  52     }
  53 
  54     public static enum TestCase {
  55         GET_TAG_AT {
  56             {
  57                 referenceMap.put(1, ConstantPool.Tag.METHODREF);
  58                 referenceMap.put(2, ConstantPool.Tag.CLASS);
  59                 referenceMap.put(4, ConstantPool.Tag.UTF8);
  60                 referenceMap.put(10, ConstantPool.Tag.NAMEANDTYPE);
  61                 referenceMap.put(13, ConstantPool.Tag.LONG);
  62                 referenceMap.put(15, ConstantPool.Tag.INTEGER);
  63                 referenceMap.put(16, ConstantPool.Tag.INTERFACEMETHODREF);
  64                 referenceMap.put(21, ConstantPool.Tag.DOUBLE);
  65                 referenceMap.put(23, ConstantPool.Tag.STRING);
  66                 referenceMap.put(25, ConstantPool.Tag.INVOKEDYNAMIC);
  67                 referenceMap.put(29, ConstantPool.Tag.METHODHANDLE);
  68                 referenceMap.put(30, ConstantPool.Tag.METHODTYPE);
  69                 referenceMap.put(48, ConstantPool.Tag.FIELDREF);
  70                 referenceMap.put(52, ConstantPool.Tag.FLOAT);
  71             }
  72             @Override
  73             void testIndex(int cpi, Object reference) {
  74                 ConstantPool.Tag tagToVerify = CP.getTagAt(cpi);
  75                 ConstantPool.Tag tagToRefer = (ConstantPool.Tag) reference;
  76                 String msg = String.format("Method getTagAt works not as expected"
  77                         + "at CP entry #%d: got CP tag %s, but should be %s",
  78                         cpi, tagToVerify.name(), tagToRefer.name());
  79                 Asserts.assertEquals(tagToVerify, tagToRefer, msg);
  80             }
  81         },
  82         GET_CLASS_REF_INDEX_AT {
  83             {
  84                 referenceMap.put(1, 3);
  85                 referenceMap.put(16, 17);
  86                 referenceMap.put(32, 35);
  87                 referenceMap.put(34, 3);
  88                 referenceMap.put(48, 2);
  89             }
  90             @Override
  91             void testIndex(int cpi, Object reference) {
  92                 int indexToVerify = CP.getClassRefIndexAt(cpi);
  93                 int indexToRefer = (int) reference;
  94                 String msg = String.format("Method getClassRefIndexAt works not"
  95                         + " as expected at CP entry #%d:"
  96                         + " got index %d, but should be %d",
  97                         cpi, indexToVerify, indexToRefer);
  98                 Asserts.assertEquals(indexToVerify, indexToRefer, msg);
  99             }
 100         },
 101         GET_NAME_AND_TYPE_REF_INDEX_AT {
 102             {
 103                 referenceMap.put(1, 10);
 104                 referenceMap.put(16, 18);
 105                 referenceMap.put(25, 26);
 106                 referenceMap.put(32, 36);
 107                 referenceMap.put(34, 37);
 108                 referenceMap.put(48, 49);
 109             }
 110             @Override
 111             void testIndex(int cpi, Object reference) {
 112                 int indexToRefer = (int) reference;
 113                 int indexToVerify = CP.getNameAndTypeRefIndexAt(cpi);
 114                 String msg = String.format("Method getNameAndTypeRefIndexAt works"
 115                         + " not as expected at CP entry #%d:"
 116                         + " got index %d, but should be %d",
 117                         cpi, indexToVerify, indexToRefer);
 118                 Asserts.assertEquals(indexToVerify, indexToRefer, msg);
 119             }
 120         },
 121         GET_NAME_AND_TYPE_REF_INFO_AT {
 122             {
 123                 referenceMap.put(10, new String[]{"<init>", "()V"});
 124                 referenceMap.put(18, new String[]{"run", "()V"});
 125                 referenceMap.put(26, new String[]{"accept", "()Ljava/util/function/Consumer;"});
 126                 referenceMap.put(36, new String[]{"metafactory",
 127                         "(Ljava/lang/invoke/MethodHandles$Lookup;"
 128                         + "Ljava/lang/String;Ljava/lang/invoke/MethodType;"
 129                         + "Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;"
 130                         + "Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"});
 131                 referenceMap.put(37, new String[]{"toString", "()Ljava/lang/String;"});
 132                 referenceMap.put(49, new String[]{"myField", "I"});
 133             }
 134             @Override
 135             void testIndex(int cpi, Object reference) {
 136                 String[] natInfo = CP.getNameAndTypeRefInfoAt(cpi);
 137                 String msg = String.format("Method getNameAndTypeRefInfoAt"
 138                         + " works not as expected at CP entry #%d:"
 139                         + " returned value should not be null", cpi);
 140                 Asserts.assertNotNull(natInfo, msg);
 141                 String[] castedReference = (String[]) reference;
 142                 int natInfoLength = natInfo.length;
 143                 msg = String.format("Method getNameAndTypeRefInfoAt"
 144                         + " works not as expected at CP entry #%d:"
 145                         + " length of the returned string array is %d, but should be 2",
 146                         cpi, natInfoLength);
 147                 Asserts.assertEquals(natInfoLength, 2, msg);
 148                 String[] nameOrType = new String[]{"name", "type"};
 149                 for (int i = 0; i < 2; i++) {
 150                     String infoToVerify = natInfo[i];
 151                     String infoToRefer = castedReference[i];
 152                     msg = String.format("Method getNameAndTypeRefInfoAt"
 153                             + " works not as expected at CP entry #%d:"
 154                             + " got %s info %s, but should be %s",
 155                             cpi, nameOrType[i], infoToVerify, infoToRefer);
 156                     Asserts.assertEquals(infoToVerify, infoToRefer, msg);
 157                 }
 158             }
 159         };
 160 
 161         protected final Map<Integer, Object> referenceMap;
 162         TestCase() {
 163             this.referenceMap = new HashMap<>();
 164         }
 165         abstract void testIndex(int cpi, Object reference);
 166         public void test() {
 167             referenceMap.forEach(this::testIndex);
 168         }
 169     }
 170 }