📄 constpool.java
字号:
/* * Javassist, a Java-bytecode translator toolkit. * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved. * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. Alternatively, the contents of this file may be used under * the terms of the GNU Lesser General Public License Version 2.1 or later. * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. */package javassist.bytecode;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.ByteArrayOutputStream;import java.io.PrintWriter;import java.io.IOException;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.Set;import javassist.CtClass;/** * Constant pool table. */public final class ConstPool { LongVector items; int numOfItems; HashMap classes; HashMap strings; int thisClassInfo; /** * <code>CONSTANT_Class</code> */ public static final int CONST_Class = ClassInfo.tag; /** * <code>CONSTANT_Fieldref</code> */ public static final int CONST_Fieldref = FieldrefInfo.tag; /** * <code>CONSTANT_Methodref</code> */ public static final int CONST_Methodref = MethodrefInfo.tag; /** * <code>CONSTANT_InterfaceMethodref</code> */ public static final int CONST_InterfaceMethodref = InterfaceMethodrefInfo.tag; /** * <code>CONSTANT_String</code> */ public static final int CONST_String = StringInfo.tag; /** * <code>CONSTANT_Integer</code> */ public static final int CONST_Integer = IntegerInfo.tag; /** * <code>CONSTANT_Float</code> */ public static final int CONST_Float = FloatInfo.tag; /** * <code>CONSTANT_Long</code> */ public static final int CONST_Long = LongInfo.tag; /** * <code>CONSTANT_Double</code> */ public static final int CONST_Double = DoubleInfo.tag; /** * <code>CONSTANT_NameAndType</code> */ public static final int CONST_NameAndType = NameAndTypeInfo.tag; /** * <code>CONSTANT_Utf8</code> */ public static final int CONST_Utf8 = Utf8Info.tag; /** * Represents the class using this constant pool table. */ public static final CtClass THIS = null; /** * Constructs a constant pool table. * * @param thisclass the name of the class using this constant * pool table */ public ConstPool(String thisclass) { items = new LongVector(); numOfItems = 0; addItem(null); // index 0 is reserved by the JVM. classes = new HashMap(); strings = new HashMap(); thisClassInfo = addClassInfo(thisclass); } /** * Constructs a constant pool table from the given byte stream. * * @param in byte stream. */ public ConstPool(DataInputStream in) throws IOException { classes = new HashMap(); strings = new HashMap(); thisClassInfo = 0; /* read() initializes items and numOfItems, and do addItem(null). */ read(in); } void prune() { classes = new HashMap(); strings = new HashMap(); } /** * Returns the number of entries in this table. */ public int getSize() { return numOfItems; } /** * Returns the name of the class using this constant pool table. */ public String getClassName() { return getClassInfo(thisClassInfo); } /** * Returns the index of <code>CONSTANT_Class_info</code> structure * specifying the class using this constant pool table. */ public int getThisClassInfo() { return thisClassInfo; } void setThisClassInfo(int i) { thisClassInfo = i; } ConstInfo getItem(int n) { return (ConstInfo)items.elementAt(n); } /** * Returns the <code>tag</code> field of the constant pool table * entry at the given index. */ public int getTag(int index) { return getItem(index).getTag(); } /** * Reads <code>CONSTANT_Class_info</code> structure * at the given index. * * @return a fully-qualified class or interface name specified * by <code>name_index</code>. */ public String getClassInfo(int index) { ClassInfo c = (ClassInfo)getItem(index); if (c == null) return null; else return Descriptor.toJavaName(getUtf8Info(c.name)); } /** * Reads the <code>name_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * at the given index. */ public int getNameAndTypeName(int index) { NameAndTypeInfo ntinfo = (NameAndTypeInfo)getItem(index); return ntinfo.memberName; } /** * Reads the <code>descriptor_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * at the given index. */ public int getNameAndTypeDescriptor(int index) { NameAndTypeInfo ntinfo = (NameAndTypeInfo)getItem(index); return ntinfo.typeDescriptor; } /** * Reads the <code>class_index</code> field of the * <code>CONSTANT_Fieldref_info</code> structure * at the given index. */ public int getFieldrefClass(int index) { FieldrefInfo finfo = (FieldrefInfo)getItem(index); return finfo.classIndex; } /** * Reads the <code>class_index</code> field of the * <code>CONSTANT_Fieldref_info</code> structure * at the given index. * * @return the name of the class at that <code>class_index</code>. */ public String getFieldrefClassName(int index) { FieldrefInfo f = (FieldrefInfo)getItem(index); if (f == null) return null; else return getClassInfo(f.classIndex); } /** * Reads the <code>name_and_type_index</code> field of the * <code>CONSTANT_Fieldref_info</code> structure * at the given index. */ public int getFieldrefNameAndType(int index) { FieldrefInfo finfo = (FieldrefInfo)getItem(index); return finfo.nameAndTypeIndex; } /** * Reads the <code>name_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * indirectly specified by the given index. * * @param index an index to a <code>CONSTANT_Fieldref_info</code>. * @return the name of the field. */ public String getFieldrefName(int index) { FieldrefInfo f = (FieldrefInfo)getItem(index); if (f == null) return null; else { NameAndTypeInfo n = (NameAndTypeInfo)getItem(f.nameAndTypeIndex); if(n == null) return null; else return getUtf8Info(n.memberName); } } /** * Reads the <code>descriptor_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * indirectly specified by the given index. * * @param index an index to a <code>CONSTANT_Fieldref_info</code>. * @return the type descriptor of the field. */ public String getFieldrefType(int index) { FieldrefInfo f = (FieldrefInfo)getItem(index); if (f == null) return null; else { NameAndTypeInfo n = (NameAndTypeInfo) getItem(f.nameAndTypeIndex); if(n == null) return null; else return getUtf8Info(n.typeDescriptor); } } /** * Reads the <code>class_index</code> field of the * <code>CONSTANT_Methodref_info</code> structure * at the given index. */ public int getMethodrefClass(int index) { MethodrefInfo minfo = (MethodrefInfo)getItem(index); return minfo.classIndex; } /** * Reads the <code>class_index</code> field of the * <code>CONSTANT_Methodref_info</code> structure * at the given index. * * @return the name of the class at that <code>class_index</code>. */ public String getMethodrefClassName(int index) { MethodrefInfo minfo = (MethodrefInfo)getItem(index); if (minfo == null) return null; else return getClassInfo(minfo.classIndex); } /** * Reads the <code>name_and_type_index</code> field of the * <code>CONSTANT_Methodref_info</code> structure * at the given index. */ public int getMethodrefNameAndType(int index) { MethodrefInfo minfo = (MethodrefInfo)getItem(index); return minfo.nameAndTypeIndex; } /** * Reads the <code>name_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * indirectly specified by the given index. * * @param index an index to a <code>CONSTANT_Methodref_info</code>. * @return the name of the method. */ public String getMethodrefName(int index) { MethodrefInfo minfo = (MethodrefInfo)getItem(index); if (minfo == null) return null; else { NameAndTypeInfo n = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex); if(n == null) return null; else return getUtf8Info(n.memberName); } } /** * Reads the <code>descriptor_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * indirectly specified by the given index. * * @param index an index to a <code>CONSTANT_Methodref_info</code>. * @return the descriptor of the method. */ public String getMethodrefType(int index) { MethodrefInfo minfo = (MethodrefInfo)getItem(index); if (minfo == null) return null; else { NameAndTypeInfo n = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex); if(n == null) return null; else return getUtf8Info(n.typeDescriptor); } } /** * Reads the <code>class_index</code> field of the * <code>CONSTANT_InterfaceMethodref_info</code> structure * at the given index. */ public int getInterfaceMethodrefClass(int index) { InterfaceMethodrefInfo minfo = (InterfaceMethodrefInfo)getItem(index); return minfo.classIndex; } /** * Reads the <code>class_index</code> field of the * <code>CONSTANT_InterfaceMethodref_info</code> structure * at the given index. * * @return the name of the class at that <code>class_index</code>. */ public String getInterfaceMethodrefClassName(int index) { InterfaceMethodrefInfo minfo = (InterfaceMethodrefInfo)getItem(index); return getClassInfo(minfo.classIndex); } /** * Reads the <code>name_and_type_index</code> field of the * <code>CONSTANT_InterfaceMethodref_info</code> structure * at the given index. */ public int getInterfaceMethodrefNameAndType(int index) { InterfaceMethodrefInfo minfo = (InterfaceMethodrefInfo)getItem(index); return minfo.nameAndTypeIndex; } /** * Reads the <code>name_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * indirectly specified by the given index. * * @param index an index to * a <code>CONSTANT_InterfaceMethodref_info</code>. * @return the name of the method. */ public String getInterfaceMethodrefName(int index) { InterfaceMethodrefInfo minfo = (InterfaceMethodrefInfo)getItem(index); if (minfo == null) return null; else { NameAndTypeInfo n = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex); if(n == null) return null; else return getUtf8Info(n.memberName); } } /** * Reads the <code>descriptor_index</code> field of the * <code>CONSTANT_NameAndType_info</code> structure * indirectly specified by the given index. * * @param index an index to * a <code>CONSTANT_InterfaceMethodref_info</code>. * @return the descriptor of the method. */ public String getInterfaceMethodrefType(int index) { InterfaceMethodrefInfo minfo = (InterfaceMethodrefInfo)getItem(index); if (minfo == null) return null; else { NameAndTypeInfo n = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex); if(n == null) return null; else return getUtf8Info(n.typeDescriptor); } } /** * Reads <code>CONSTANT_Integer_info</code>, <code>_Float_info</code>, * <code>_Long_info</code>, <code>_Double_info</code>, or * <code>_String_info</code> structure. * These are used with the LDC instruction. * * @return a <code>String</code> value or a wrapped primitive-type * value. */ public Object getLdcValue(int index) { ConstInfo constInfo = this.getItem(index); Object value = null; if (constInfo instanceof StringInfo) value = this.getStringInfo(index); else if (constInfo instanceof FloatInfo) value = new Float(getFloatInfo(index)); else if (constInfo instanceof IntegerInfo) value = new Integer(getIntegerInfo(index)); else if (constInfo instanceof LongInfo) value = new Long(getLongInfo(index)); else if (constInfo instanceof DoubleInfo) value = new Double(getDoubleInfo(index)); else value = null; return value; } /** * Reads <code>CONSTANT_Integer_info</code> structure * at the given index. * * @return the value specified by this entry. */ public int getIntegerInfo(int index) { IntegerInfo i = (IntegerInfo)getItem(index); return i.value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -