I am trying to copy methods from one class to another. This is the code I have been
woking on. I am getting errors with the resulting constant pool. Is this the correct
way to copy a method, or am i misunderstanding the API.
thanks,
james
public static void main(String[] args) {
JavaClass j1 = new ClassParser("A.class").parse();
JavaClass j2 = new ClassParser("B.class").parse();
copyCode(j1,j2);
}
public static void copyCode(JavaClass src, JavaClass dest) {
ClassGen cg1 = new ClassGen(dest);
ClassGen cg2 = new ClassGen(dest);
ConstantPoolGen cp1 = cg1.getConstantPool();
ConstantPoolGen cp2 = cg2.getConstantPool();
Method[] methods = src.getMethods();
for (int i=0; i< methods.length; i++) {
MethodGen m = new MethodGen(methods[i],
src.getClassName(),
cp1);
MethodGen m2 = m.copy(cg2.getClassName(), cp2);
printCode(new Method[] {m2.getMethod()}, true);
cg2.addMethod(m2.getMethod());
}
JavaClass c3 = cg2.getJavaClass();
c3.setConstantPool(cp2.getFinalConstantPool());
try {
c3.dump("test.class");
} catch (IOException e) {
System.out.println(e.getStackTrace());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]