public int lookupMethodref(MethodGen method) { return lookupMethodref(method.getClassName(), method.getName(), method.getSignature()); }
public int lookupMethodref( final 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 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; }
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); }