📄 constpool.java
字号:
} /** * Reads <code>CONSTANT_Float_info</code> structure * at the given index. * * @return the value specified by this entry. */ public float getFloatInfo(int index) { FloatInfo i = (FloatInfo)getItem(index); return i.value; } /** * Reads <code>CONSTANT_Long_info</code> structure * at the given index. * * @return the value specified by this entry. */ public long getLongInfo(int index) { LongInfo i = (LongInfo)getItem(index); return i.value; } /** * Reads <code>CONSTANT_Double_info</code> structure * at the given index. * * @return the value specified by this entry. */ public double getDoubleInfo(int index) { DoubleInfo i = (DoubleInfo)getItem(index); return i.value; } /** * Reads <code>CONSTANT_String_info</code> structure * at the given index. * * @return the string specified by <code>string_index</code>. */ public String getStringInfo(int index) { StringInfo si = (StringInfo)getItem(index); return getUtf8Info(si.string); } /** * Reads <code>CONSTANT_utf8_info</code> structure * at the given index. * * @return the string specified by this entry. */ public String getUtf8Info(int index) { Utf8Info utf = (Utf8Info)getItem(index); return utf.string; } /** * Determines whether <code>CONSTANT_Methodref_info</code> * structure at the given index represents the constructor * of the given class. * * @return the <code>descriptor_index</code> specifying * the type descriptor of the that constructor. * If it is not that constructor, * <code>isConstructor()</code> returns 0. */ public int isConstructor(String classname, int index) { return isMember(classname, MethodInfo.nameInit, index); } /** * Determines whether <code>CONSTANT_Methodref_info</code>, * <code>CONSTANT_Fieldref_info</code>, or * <code>CONSTANT_InterfaceMethodref_info</code> structure * at the given index represents the member with the specified * name and declaring class. * * @param classname the class declaring the member * @param membername the member name * @param index the index into the constant pool table * * @return the <code>descriptor_index</code> specifying * the type descriptor of that member. * If it is not that member, * <code>isMember()</code> returns 0. */ public int isMember(String classname, String membername, int index) { MemberrefInfo minfo = (MemberrefInfo)getItem(index); if (getClassInfo(minfo.classIndex).equals(classname)) { NameAndTypeInfo ntinfo = (NameAndTypeInfo)getItem(minfo.nameAndTypeIndex); if (getUtf8Info(ntinfo.memberName).equals(membername)) return ntinfo.typeDescriptor; } return 0; // false } private int addItem(ConstInfo info) { items.addElement(info); return numOfItems++; } /** * Copies the n-th item in this ConstPool object into the destination * ConstPool object. * The class names that the item refers to are renamed according * to the given map. * * @param n the <i>n</i>-th item * @param dest destination constant pool table * @param classnames the map or null. * @return the index of the copied item into the destination ClassPool. */ public int copy(int n, ConstPool dest, Map classnames) { if (n == 0) return 0; ConstInfo info = getItem(n); return info.copy(this, dest, classnames); } int addConstInfoPadding() { return addItem(new ConstInfoPadding()); } /** * Adds a new <code>CONSTANT_Class_info</code> structure. * * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure * for storing the class name. * * @return the index of the added entry. */ public int addClassInfo(CtClass c) { if (c == THIS) return thisClassInfo; else if (!c.isArray()) return addClassInfo(c.getName()); else { // an array type is recorded in the hashtable with // the key "[L<classname>;" instead of "<classname>". // // note: toJvmName(toJvmName(c)) is equal to toJvmName(c). return addClassInfo(Descriptor.toJvmName(c)); } } /** * Adds a new <code>CONSTANT_Class_info</code> structure. * * <p>This also adds a <code>CONSTANT_Utf8_info</code> structure * for storing the class name. * * @param qname a fully-qualified class name * (or the JVM-internal representation of that name). * @return the index of the added entry. */ public int addClassInfo(String qname) { ClassInfo info = (ClassInfo)classes.get(qname); if (info != null) return info.index; else { int utf8 = addUtf8Info(Descriptor.toJvmName(qname)); info = new ClassInfo(utf8, numOfItems); classes.put(qname, info); return addItem(info); } } /** * Adds a new <code>CONSTANT_NameAndType_info</code> structure. * * <p>This also adds <code>CONSTANT_Utf8_info</code> structures. * * @param name <code>name_index</code> * @param type <code>descriptor_index</code> * @return the index of the added entry. */ public int addNameAndTypeInfo(String name, String type) { return addNameAndTypeInfo(addUtf8Info(name), addUtf8Info(type)); } /** * Adds a new <code>CONSTANT_NameAndType_info</code> structure. * * @param name <code>name_index</code> * @param type <code>descriptor_index</code> * @return the index of the added entry. */ public int addNameAndTypeInfo(int name, int type) { return addItem(new NameAndTypeInfo(name, type)); } /** * Adds a new <code>CONSTANT_Fieldref_info</code> structure. * * <p>This also adds a new <code>CONSTANT_NameAndType_info</code> * structure. * * @param classInfo <code>class_index</code> * @param name <code>name_index</code> * of <code>CONSTANT_NameAndType_info</code>. * @param type <code>descriptor_index</code> * of <code>CONSTANT_NameAndType_info</code>. * @return the index of the added entry. */ public int addFieldrefInfo(int classInfo, String name, String type) { int nt = addNameAndTypeInfo(name, type); return addFieldrefInfo(classInfo, nt); } /** * Adds a new <code>CONSTANT_Fieldref_info</code> structure. * * @param classInfo <code>class_index</code> * @param nameAndTypeInfo <code>name_and_type_index</code>. * @return the index of the added entry. */ public int addFieldrefInfo(int classInfo, int nameAndTypeInfo) { return addItem(new FieldrefInfo(classInfo, nameAndTypeInfo)); } /** * Adds a new <code>CONSTANT_Methodref_info</code> structure. * * <p>This also adds a new <code>CONSTANT_NameAndType_info</code> * structure. * * @param classInfo <code>class_index</code> * @param name <code>name_index</code> * of <code>CONSTANT_NameAndType_info</code>. * @param type <code>descriptor_index</code> * of <code>CONSTANT_NameAndType_info</code>. * @return the index of the added entry. */ public int addMethodrefInfo(int classInfo, String name, String type) { int nt = addNameAndTypeInfo(name, type); return addMethodrefInfo(classInfo, nt); } /** * Adds a new <code>CONSTANT_Methodref_info</code> structure. * * @param classInfo <code>class_index</code> * @param nameAndTypeInfo <code>name_and_type_index</code>. * @return the index of the added entry. */ public int addMethodrefInfo(int classInfo, int nameAndTypeInfo) { return addItem(new MethodrefInfo(classInfo, nameAndTypeInfo)); } /** * Adds a new <code>CONSTANT_InterfaceMethodref_info</code> * structure. * * <p>This also adds a new <code>CONSTANT_NameAndType_info</code> * structure. * * @param classInfo <code>class_index</code> * @param name <code>name_index</code> * of <code>CONSTANT_NameAndType_info</code>. * @param type <code>descriptor_index</code> * of <code>CONSTANT_NameAndType_info</code>. * @return the index of the added entry. */ public int addInterfaceMethodrefInfo(int classInfo, String name, String type) { int nt = addNameAndTypeInfo(name, type); return addInterfaceMethodrefInfo(classInfo, nt); } /** * Adds a new <code>CONSTANT_InterfaceMethodref_info</code> * structure. * * @param classInfo <code>class_index</code> * @param nameAndTypeInfo <code>name_and_type_index</code>. * @return the index of the added entry. */ public int addInterfaceMethodrefInfo(int classInfo, int nameAndTypeInfo) { return addItem(new InterfaceMethodrefInfo(classInfo, nameAndTypeInfo)); } /** * Adds a new <code>CONSTANT_String_info</code> * structure. * * <p>This also adds a new <code>CONSTANT_Utf8_info</code> * structure. * * @return the index of the added entry. */ public int addStringInfo(String str) { return addItem(new StringInfo(addUtf8Info(str))); } /** * Adds a new <code>CONSTANT_Integer_info</code> * structure. * * @return the index of the added entry. */ public int addIntegerInfo(int i) { return addItem(new IntegerInfo(i)); } /** * Adds a new <code>CONSTANT_Float_info</code> * structure. * * @return the index of the added entry. */ public int addFloatInfo(float f) { return addItem(new FloatInfo(f)); } /** * Adds a new <code>CONSTANT_Long_info</code> * structure. * * @return the index of the added entry. */ public int addLongInfo(long l) { int i = addItem(new LongInfo(l)); addItem(new ConstInfoPadding()); return i; } /** * Adds a new <code>CONSTANT_Double_info</code> * structure. * * @return the index of the added entry. */ public int addDoubleInfo(double d) { int i = addItem(new DoubleInfo(d)); addItem(new ConstInfoPadding()); return i; } /** * Adds a new <code>CONSTANT_Utf8_info</code> * structure. * * <p>If the given utf8 string has been already recorded in the * table, then this method does not add a new entry to avoid adding * a duplicated entry. * Instead, it returns the index of the entry already recorded. * * @return the index of the added entry. */ public int addUtf8Info(String utf8) { Utf8Info info = (Utf8Info)strings.get(utf8); if (info != null) return info.index; else { info = new Utf8Info(utf8, numOfItems); strings.put(utf8, info); return addItem(info); } } /** * Get all the class names. * * @return a set of class names */ public Set getClassNames() { HashSet result = new HashSet(); LongVector v = items; int size = numOfItems; for (int i = 1; i < size; ++i) { String className = ((ConstInfo) v.elementAt(i)).getClassName(this); if (className != null) result.add(className); } return result; } /** * Replaces all occurrences of a class name. * * @param oldName the replaced name (JVM-internal representation). * @param newName the substituted name (JVM-internal representation). */ public void renameClass(String oldName, String newName) { LongVector v = items; int size = numOfItems; classes = new HashMap(classes.size() * 2); for (int i = 1; i < size; ++i) { ConstInfo ci = (ConstInfo)v.elementAt(i); ci.renameClass(this, oldName, newName); ci.makeHashtable(this); } } /** * Replaces all occurrences of class names. * * @param classnames specifies pairs of replaced and substituted * name. */ public void renameClass(Map classnames) { LongVector v = items; int size = numOfItems; classes = new HashMap(classes.size() * 2); for (int i = 1; i < size; ++i) { ConstInfo ci = (ConstInfo)v.elementAt(i); ci.renameClass(this, classnames); ci.makeHashtable(this); } } private void read(DataInputStream in) throws IOException { int n = in.readUnsignedShort(); items = new LongVector(n); numOfItems = 0; addItem(null); // index 0 is reserved by the JVM. while (--n > 0) { // index 0 is reserved by JVM int tag = readOne(in); if ((tag == LongInfo.tag) || (tag == DoubleInfo.tag)) { addItem(new ConstInfoPadding()); --n; } } int i = 1; while (true) { ConstInfo info = (ConstInfo)items.elementAt(i++); if (info == null) break; else info.makeHashtable(this); } } private int readOne(DataInputStream in) throws IOException { ConstInfo info; int tag = in.readUnsignedByte(); switch (tag) { case Utf8Info.tag : // 1 info = new Utf8Info(in, numOfItems); strings.put(((Utf8Info)info).string, info); break; case IntegerInfo.tag : // 3 info = new IntegerInfo(in); break; case FloatInfo.tag : // 4 info = new FloatInfo(in); break; case LongInfo.tag : // 5 info = new LongInfo(in); break; case DoubleInfo.tag : // 6 info = new DoubleInfo(in); break; case ClassInfo.tag : // 7 info = new ClassInfo(in, numOfItems); // classes.put(<classname>, info); break; case StringInfo.tag : // 8 info = new StringInfo(in); break; case FieldrefInfo.tag : // 9 info = new FieldrefInfo(in); break; case MethodrefInfo.tag : // 10 info = new MethodrefInfo(in); break; case InterfaceMethodrefInfo.tag : // 11 info = new InterfaceMethodrefInfo(in); break; case NameAndTypeInfo.tag : // 12 info = new NameAndTypeInfo(in);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -