/** * Add a new InterfaceMethodref constant to the ConstantPool, if it is not already * in there. * * @param class_name class name string to add * @param method_name method name string to add * @param signature signature string to add * @return index of entry */ public int addInterfaceMethodref( final String class_name, final String method_name, final String signature ) { int ret; int class_index; int 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); final String key = class_name + IMETHODREF_DELIM + method_name + IMETHODREF_DELIM + signature; if (!cp_table.containsKey(key)) { cp_table.put(key, new Index(ret)); } return ret; }
/** * Add a new Fieldref constant to the ConstantPool, if it is not already * in there. * * @param class_name class name string to add * @param field_name field name string to add * @param signature signature string to add * @return index of entry */ public int addFieldref( final String class_name, final String field_name, final String signature ) { int ret; int class_index; int 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); final String key = class_name + FIELDREF_DELIM + field_name + FIELDREF_DELIM + signature; if (!cp_table.containsKey(key)) { cp_table.put(key, new Index(ret)); } return ret; }
/** * Add a new Methodref constant to the ConstantPool, if it is not already * in there. * * @param class_name class name string to add * @param method_name method name string to add * @param signature method signature string to add * @return index of entry */ public int addMethodref( final String class_name, final String method_name, final String signature ) { int ret; int class_index; int 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); final String key = class_name + METHODREF_DELIM + method_name + METHODREF_DELIM + signature; if (!cp_table.containsKey(key)) { cp_table.put(key, new Index(ret)); } return ret; }
/** * 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; }
/** * 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; }
/** * 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; }
JavaClass target; try { target = Repository.lookupClass("Target"); } catch (final ClassNotFoundException ex) { throw new RuntimeException("unable to resolve Target", ex); } final ClassGen targetGen = new ClassGen(target); final ConstantPoolGen pool = targetGen.getConstantPool(); final ConstantMethodref ref = (ConstantMethodref) pool.getConstant( pool.lookupMethodref("Name", "getName", "()Ljava/lang/String;")); ref.setClassIndex(pool.lookupClass("Target")); ref.setNameAndTypeIndex(pool.addNameAndType("$Name$getName", "()Ljava/lang/String;")); final InstructionList code = new InstructionList(); final InstructionFactory factory = new InstructionFactory(targetGen, pool); code.append(factory.createConstant("overriden-name")); code.append(factory.createReturn(Type.STRING)); code.setPositions(); final MethodGen methodGen = new MethodGen( Constants.ACC_PRIVATE | Constants.ACC_SYNTHETIC | Constants.ACC_STATIC, Type.STRING, new Type[0], new String[0], "$Name$getName", "Target", code, pool); methodGen.setMaxLocals(0); methodGen.setMaxStack(1); targetGen.addMethod(methodGen.getMethod()); try { targetGen.getJavaClass().dump("Target.class"); } catch (final IOException ex) { throw new RuntimeException("unable to save Target", ex); }
final ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()]; final ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()]; return addNameAndType(u8.getBytes(), u8_2.getBytes());
ConstantUtf8 u8_2 = (ConstantUtf8)constants[n.getSignatureIndex()]; return addNameAndType(u8.getBytes(), u8_2.getBytes());