jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Sasitorn <ja...@onegoodcookie.com>
Subject copy method to different class
Date Mon, 19 Jul 2004 17:18:46 GMT
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: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Mime
View raw message