📄 constantpoolgen.java
字号:
int ret; if((ret = lookupLong(n)) != -1) return ret; // Already in CP adjustSize(); ret = index; constants[index] = new ConstantLong(n); index += 2; // Wastes one entry according to spec return ret; } /** * Look for ConstantDouble in ConstantPool. * * @param n Double number to look for * @return index on success, -1 otherwise */ public int lookupDouble(double n) { for(int i=1; i < index; i++) { if(constants[i] instanceof ConstantDouble) { ConstantDouble c = (ConstantDouble)constants[i]; if(c.getBytes() == n) return i; } } return -1; } /** * Add a new double constant to the ConstantPool, if it is not already in there. * * @param n Double number to add * @return index of entry */ public int addDouble(double n) { int ret; if((ret = lookupDouble(n)) != -1) return ret; // Already in CP adjustSize(); ret = index; constants[index] = new ConstantDouble(n); index += 2; // Wastes one entry according to spec return ret; } private Hashtable n_a_t_table = new Hashtable(); /** * Look for ConstantNameAndType in ConstantPool. * * @param name of variable/method * @param signature of variable/method * @return index on success, -1 otherwise */ public int lookupNameAndType(String name, String signature) { Index index = (Index)n_a_t_table.get(name + NAT_DELIM + signature); return (index != null)? index.index : -1; } /** * Add a new NameAndType constant to the ConstantPool if it is not already * in there. * * @param n NameAndType string to add * @return index of entry */ public int addNameAndType(String name, String signature) { int ret; int name_index, signature_index; if((ret = lookupNameAndType(name, signature)) != -1) return ret; // Already in CP adjustSize(); name_index = addUtf8(name); signature_index = addUtf8(signature); ret = index; constants[index++] = new ConstantNameAndType(name_index, signature_index); n_a_t_table.put(name + NAT_DELIM + signature, new Index(ret)); return ret; } private Hashtable cp_table = new Hashtable(); /** * Look for ConstantMethodref in ConstantPool. * * @param class_name Where to find method * @param method_name Guess what * @param signature return and argument types * @return index on success, -1 otherwise */ public int lookupMethodref(String class_name, String method_name, String signature) { Index index = (Index)cp_table.get(class_name + METHODREF_DELIM + method_name + METHODREF_DELIM + signature); return (index != null)? index.index : -1; } public int lookupMethodref(MethodGen method) { return lookupMethodref(method.getClassName(), method.getName(), method.getSignature()); } /** * Add a new Methodref constant to the ConstantPool, if it is not already * in there. * * @param n Methodref string to add * @return index of entry */ public int addMethodref(String class_name, String method_name, String signature) { int ret, class_index, name_and_type_index; if((ret = lookupMethodref(class_name, method_name, signature)) != -1) return ret; // Already in CP adjustSize(); name_and_type_index = addNameAndType(method_name, signature); class_index = addClass(class_name); ret = index; constants[index++] = new ConstantMethodref(class_index, name_and_type_index); cp_table.put(class_name + METHODREF_DELIM + method_name + METHODREF_DELIM + signature, new Index(ret)); return ret; } public int addMethodref(MethodGen method) { return addMethodref(method.getClassName(), method.getName(), method.getSignature()); } /** * Look for ConstantInterfaceMethodref in ConstantPool. * * @param class_name Where to find method * @param method_name Guess what * @param signature return and argument types * @return index on success, -1 otherwise */ public int lookupInterfaceMethodref(String class_name, String method_name, String signature) { Index index = (Index)cp_table.get(class_name + IMETHODREF_DELIM + method_name + IMETHODREF_DELIM + signature); return (index != null)? index.index : -1; } public int lookupInterfaceMethodref(MethodGen method) { return lookupInterfaceMethodref(method.getClassName(), method.getName(), method.getSignature()); } /** * Add a new InterfaceMethodref constant to the ConstantPool, if it is not already * in there. * * @param n InterfaceMethodref string to add * @return index of entry */ public int addInterfaceMethodref(String class_name, String method_name, String signature) { int ret, class_index, name_and_type_index; if((ret = lookupInterfaceMethodref(class_name, method_name, signature)) != -1) return ret; // Already in CP adjustSize(); class_index = addClass(class_name); name_and_type_index = addNameAndType(method_name, signature); ret = index; constants[index++] = new ConstantInterfaceMethodref(class_index, name_and_type_index); cp_table.put(class_name + IMETHODREF_DELIM + method_name + IMETHODREF_DELIM + signature, new Index(ret)); return ret; } public int addInterfaceMethodref(MethodGen method) { return addInterfaceMethodref(method.getClassName(), method.getName(), method.getSignature()); } /** * Look for ConstantFieldref in ConstantPool. * * @param class_name Where to find method * @param field_name Guess what * @param signature return and argument types * @return index on success, -1 otherwise */ public int lookupFieldref(String class_name, String field_name, String signature) { Index index = (Index)cp_table.get(class_name + FIELDREF_DELIM + field_name + FIELDREF_DELIM + signature); return (index != null)? index.index : -1; } /** * Add a new Fieldref constant to the ConstantPool, if it is not already * in there. * * @param n Fieldref string to add * @return index of entry */ public int addFieldref(String class_name, String field_name, String signature) { int ret; int class_index, name_and_type_index; if((ret = lookupFieldref(class_name, field_name, signature)) != -1) return ret; // Already in CP adjustSize(); class_index = addClass(class_name); name_and_type_index = addNameAndType(field_name, signature); ret = index; constants[index++] = new ConstantFieldref(class_index, name_and_type_index); cp_table.put(class_name + FIELDREF_DELIM + field_name + FIELDREF_DELIM + signature, new Index(ret)); return ret; } /** * @param i index in constant pool * @return constant pool entry at index i */ public Constant getConstant(int i) { return constants[i]; } /** * Use with care! * * @param i index in constant pool * @param c new constant pool entry at index i */ public void setConstant(int i, Constant c) { constants[i] = c; } /** * @return intermediate constant pool */ public ConstantPool getConstantPool() { return new ConstantPool(constants); } /** * @return current size of constant pool */ public int getSize() { return index; } /** * @return constant pool with proper length */ public ConstantPool getFinalConstantPool() { Constant[] cs = new Constant[index]; System.arraycopy(constants, 0, cs, 0, index); return new ConstantPool(cs); } /** * @return String representation. */ public String toString() { StringBuffer buf = new StringBuffer(); for(int i=1; i < index; i++) buf.append(i + ")" + constants[i] + "\n"); return buf.toString(); } /** Import constant from another ConstantPool and return new index. */ public int addConstant(Constant c, ConstantPoolGen cp) { Constant[] constants = cp.getConstantPool().getConstantPool(); switch(c.getTag()) { case Constants.CONSTANT_String: { ConstantString s = (ConstantString)c; ConstantUtf8 u8 = (ConstantUtf8)constants[s.getStringIndex()]; return addString(u8.getBytes()); } case Constants.CONSTANT_Class: { ConstantClass s = (ConstantClass)c; ConstantUtf8 u8 = (ConstantUtf8)constants[s.getNameIndex()]; return addClass(u8.getBytes()); } case Constants.CONSTANT_NameAndType: { ConstantNameAndType n = (ConstantNameAndType)c; ConstantUtf8 u8 = (ConstantUtf8)constants[n.getNameIndex()]; ConstantUtf8 u8_2 = (ConstantUtf8)constants[n.getSignatureIndex()]; return addNameAndType(u8.getBytes(), u8_2.getBytes()); } case Constants.CONSTANT_Utf8: return addUtf8(((ConstantUtf8)c).getBytes()); case Constants.CONSTANT_Double: return addDouble(((ConstantDouble)c).getBytes()); case Constants.CONSTANT_Float: return addFloat(((ConstantFloat)c).getBytes()); case Constants.CONSTANT_Long: return addLong(((ConstantLong)c).getBytes()); case Constants.CONSTANT_Integer: return addInteger(((ConstantInteger)c).getBytes()); case Constants.CONSTANT_InterfaceMethodref: case Constants.CONSTANT_Methodref: case Constants.CONSTANT_Fieldref: { ConstantCP m = (ConstantCP)c; ConstantClass clazz = (ConstantClass)constants[m.getClassIndex()]; ConstantNameAndType n = (ConstantNameAndType)constants[m.getNameAndTypeIndex()]; ConstantUtf8 u8 = (ConstantUtf8)constants[clazz.getNameIndex()]; String class_name = u8.getBytes().replace('/', '.'); u8 = (ConstantUtf8)constants[n.getNameIndex()]; String name = u8.getBytes(); u8 = (ConstantUtf8)constants[n.getSignatureIndex()]; String signature = u8.getBytes(); switch(c.getTag()) { case Constants.CONSTANT_InterfaceMethodref: return addInterfaceMethodref(class_name, name, signature); case Constants.CONSTANT_Methodref: return addMethodref(class_name, name, signature); case Constants.CONSTANT_Fieldref: return addFieldref(class_name, name, signature); default: // Never reached throw new RuntimeException("Unknown constant type " + c); } } default: // Never reached throw new RuntimeException("Unknown constant type " + c); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -