Path: blob/master/src/java.base/share/classes/jdk/internal/reflect/ConstantPool.java
41159 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.reflect;2627import java.lang.reflect.*;28import java.util.Set;2930/** Provides reflective access to the constant pools of classes.31Currently this is needed to provide reflective access to annotations32but may be used by other internal subsystems in the future. */3334public class ConstantPool {35// Number of entries in this constant pool (= maximum valid constant pool index)36public int getSize() { return getSize0 (constantPoolOop); }37public Class<?> getClassAt (int index) { return getClassAt0 (constantPoolOop, index); }38public Class<?> getClassAtIfLoaded (int index) { return getClassAtIfLoaded0 (constantPoolOop, index); }39// Returns a class reference index for a method or a field.40public int getClassRefIndexAt(int index) {41return getClassRefIndexAt0(constantPoolOop, index);42}43// Returns either a Method or Constructor.44// Static initializers are returned as Method objects.45public Member getMethodAt (int index) { return getMethodAt0 (constantPoolOop, index); }46public Member getMethodAtIfLoaded(int index) { return getMethodAtIfLoaded0(constantPoolOop, index); }47public Field getFieldAt (int index) { return getFieldAt0 (constantPoolOop, index); }48public Field getFieldAtIfLoaded (int index) { return getFieldAtIfLoaded0 (constantPoolOop, index); }49// Fetches the class name, member (field, method or interface50// method) name, and type descriptor as an array of three Strings51public String[] getMemberRefInfoAt (int index) { return getMemberRefInfoAt0 (constantPoolOop, index); }52// Returns a name and type reference index for a method, a field or an invokedynamic.53public int getNameAndTypeRefIndexAt(int index) {54return getNameAndTypeRefIndexAt0(constantPoolOop, index);55}56// Fetches the name and type from name_and_type index as an array of two Strings57public String[] getNameAndTypeRefInfoAt(int index) {58return getNameAndTypeRefInfoAt0(constantPoolOop, index);59}60public int getIntAt (int index) { return getIntAt0 (constantPoolOop, index); }61public long getLongAt (int index) { return getLongAt0 (constantPoolOop, index); }62public float getFloatAt (int index) { return getFloatAt0 (constantPoolOop, index); }63public double getDoubleAt (int index) { return getDoubleAt0 (constantPoolOop, index); }64public String getStringAt (int index) { return getStringAt0 (constantPoolOop, index); }65public String getUTF8At (int index) { return getUTF8At0 (constantPoolOop, index); }66public Tag getTagAt(int index) {67return Tag.valueOf(getTagAt0(constantPoolOop, index));68}6970public static enum Tag {71UTF8(1),72INTEGER(3),73FLOAT(4),74LONG(5),75DOUBLE(6),76CLASS(7),77STRING(8),78FIELDREF(9),79METHODREF(10),80INTERFACEMETHODREF(11),81NAMEANDTYPE(12),82METHODHANDLE(15),83METHODTYPE(16),84INVOKEDYNAMIC(18),85INVALID(0);8687private final int tagCode;8889private Tag(int tagCode) {90this.tagCode = tagCode;91}9293private static Tag valueOf(byte v) {94for (Tag tag : Tag.values()) {95if (tag.tagCode == v) {96return tag;97}98}99throw new IllegalArgumentException("Unknown constant pool tag code " + v);100}101}102//---------------------------------------------------------------------------103// Internals only below this point104//105106static {107Reflection.registerFieldsToFilter(ConstantPool.class, Set.of("constantPoolOop"));108}109110// HotSpot-internal constant pool object (set by the VM, name known to the VM)111private Object constantPoolOop;112113private native int getSize0 (Object constantPoolOop);114private native Class<?> getClassAt0 (Object constantPoolOop, int index);115private native Class<?> getClassAtIfLoaded0 (Object constantPoolOop, int index);116private native int getClassRefIndexAt0 (Object constantPoolOop, int index);117private native Member getMethodAt0 (Object constantPoolOop, int index);118private native Member getMethodAtIfLoaded0(Object constantPoolOop, int index);119private native Field getFieldAt0 (Object constantPoolOop, int index);120private native Field getFieldAtIfLoaded0 (Object constantPoolOop, int index);121private native String[] getMemberRefInfoAt0 (Object constantPoolOop, int index);122private native int getNameAndTypeRefIndexAt0(Object constantPoolOop, int index);123private native String[] getNameAndTypeRefInfoAt0(Object constantPoolOop, int index);124private native int getIntAt0 (Object constantPoolOop, int index);125private native long getLongAt0 (Object constantPoolOop, int index);126private native float getFloatAt0 (Object constantPoolOop, int index);127private native double getDoubleAt0 (Object constantPoolOop, int index);128private native String getStringAt0 (Object constantPoolOop, int index);129private native String getUTF8At0 (Object constantPoolOop, int index);130private native byte getTagAt0 (Object constantPoolOop, int index);131}132133134