You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Al <no...@eircom.net> on 2003/02/09 00:33:41 UTC

Copying a JavaClass with XXXGen and visitor

Hi,

Im having trouble understanding the use of the various XXXGen classes. For
my application I want to begin by copying a JavaClass. I will later wish to
have some control over this process so I was trying to use a ClassGen and a
visitor to add stuff to it. My naive attempt is below. Of course it doesnt
quite work. I imagine Ill have to make use of other XXGen classes but Im not
sure and Im not sure how. I hoped someone here would talk me through what
EmptyVisitor methods I should override and what I should be doing in them.

Any help greatly appreciated, A.

/**
 * My attempt to copy a JavaClass using ClassGen and a visitor
 */
public class EVisitor extends de.fub.bytecode.classfile.EmptyVisitor
{ private String className;
  private de.fub.bytecode.generic.ClassGen cg;
  private de.fub.bytecode.generic.ConstantPoolGen cpg;

  public static void main(String[] args)
  { try
    { de.fub.bytecode.classfile.JavaClass c = new
de.fub.bytecode.classfile.ClassParser(args[0]).parse();

      de.fub.bytecode.generic.ClassGen cg = new
de.fub.bytecode.generic.ClassGen(c.getClassName(), c.getSuperclassName(),
c.getFileName(), c.getAccessFlags(), c.getInterfaceNames());

      new de.fub.bytecode.classfile.DescendingVisitor(c, new
EmbedderVisitor(cg)).visit();

      c = cg.getJavaClass();

      c.dump(args[1]);

      System.out.println(c);
    }
    catch(Exception e)
    { e.printStackTrace();
    }
  }

  public EVisitor(de.fub.bytecode.generic.ClassGen cg)
  { this.cg = cg;
    this.cpg = cg.getConstantPool();
    this.className = cg.getClassName();
  }

  public void visitMethod(de.fub.bytecode.classfile.Method m)
  { de.fub.bytecode.generic.MethodGen mg = new
de.fub.bytecode.generic.MethodGen(m, this.className, this.cpg);
    this.cg.addMethod(mg.getMethod());
  }

  public void visitField(de.fub.bytecode.classfile.Field f)
  { de.fub.bytecode.generic.FieldGen fg = new
de.fub.bytecode.generic.FieldGen(f, this.cpg);
    this.cg.addField(fg.getField());
  }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org