FileDocCategorySizeDatePackage
BCELifier.javaAPI DocJava SE 6 API10826Tue Jun 10 00:22:24 BST 2008com.sun.org.apache.bcel.internal.util

BCELifier

public class BCELifier extends EmptyVisitor
This class takes a given JavaClass object and converts it to a Java program that creates that very class using BCEL. This gives new users of BCEL a useful example showing how things are done with BCEL. It does not cover all features of BCEL, but tries to mimic hand-written code as close as possible.
version
$Id: BCELifier.java,v 1.1.2.1 2005/07/31 23:47:01 jeffsuttor Exp $
author
M. Dahm

Fields Summary
private JavaClass
_clazz
private PrintWriter
_out
private ConstantPoolGen
_cp
Constructors Summary
public BCELifier(JavaClass clazz, OutputStream out)

param
clazz Java class to "decompile"
param
out where to output Java program

    _clazz = clazz;
    _out = new PrintWriter(out);
    _cp = new ConstantPoolGen(_clazz.getConstantPool());
  
Methods Summary
public static void_main(java.lang.String[] argv)
Default _main method

    JavaClass java_class;
    String    name = argv[0];

    if((java_class = Repository.lookupClass(name)) == null)
      java_class = new ClassParser(name).parse(); // May throw IOException

    BCELifier bcelifier = new BCELifier(java_class, System.out);
    bcelifier.start();
  
static java.lang.StringprintArgumentTypes(com.sun.org.apache.bcel.internal.generic.Type[] arg_types)

    if(arg_types.length == 0)
      return "Type.NO_ARGS";

    StringBuffer args = new StringBuffer();

    for(int i=0; i < arg_types.length; i++) {
      args.append(printType(arg_types[i]));

      if(i < arg_types.length - 1)
	args.append(", ");
    }

    return "new Type[] { " + args.toString() + " }";
  
private voidprintCreate()

    _out.println("  public void create(OutputStream out) throws IOException {");

    Field[] fields = _clazz.getFields();
    if(fields.length > 0) {
      _out.println("    createFields();");   
    }

    Method[] methods = _clazz.getMethods();
    for(int i=0; i < methods.length; i++) {
      _out.println("    createMethod_" + i + "();");   
    }

    _out.println("    _cg.getJavaClass().dump(out);");   
  
    _out.println("  }\n");     
  
static java.lang.StringprintFlags(int flags)

    return printFlags(flags, false);
  
static java.lang.StringprintFlags(int flags, boolean for_class)

    if(flags == 0)
      return "0";

    StringBuffer buf = new StringBuffer();
    for(int i=0, pow=1; i <= Constants.MAX_ACC_FLAG; i++) {
      if((flags & pow) != 0) {
	if((pow == Constants.ACC_SYNCHRONIZED) && for_class)
	  buf.append("ACC_SUPER | ");
	else
	  buf.append("ACC_" + Constants.ACCESS_NAMES[i].toUpperCase() + " | ");
      }

      pow <<= 1;
    }

    String str = buf.toString();
    return str.substring(0, str.length() - 3);
  
private voidprintMain()

    String   class_name   = _clazz.getClassName();
    
    _out.println("  public static void _main(String[] args) throws Exception {");
    _out.println("    " + class_name + "Creator creator = new " +
		 class_name + "Creator();");
    _out.println("    creator.create(new FileOutputStream(\"" + class_name +
		 ".class\"));");
    _out.println("  }");     
  
static java.lang.StringprintType(com.sun.org.apache.bcel.internal.generic.Type type)

    return printType(type.getSignature());
  
static java.lang.StringprintType(java.lang.String signature)

    Type type = Type.getType(signature);
    byte t    = type.getType();

    if(t <= Constants.T_VOID) {
      return "Type." + Constants.TYPE_NAMES[t].toUpperCase();
    } else if(type.toString().equals("java.lang.String")) {
      return "Type.STRING";
    } else if(type.toString().equals("java.lang.Object")) {
      return "Type.OBJECT";
    } else if(type.toString().equals("java.lang.StringBuffer")) {
      return "Type.STRINGBUFFER";
    } else if(type instanceof ArrayType) {
      ArrayType at = (ArrayType)type;

      return "new ArrayType(" + printType(at.getBasicType()) +
	", " + at.getDimensions() + ")";
    } else {
      return "new ObjectType(\"" + Utility.signatureToString(signature, false) +
	"\")";
    }
  
public voidstart()
Start Java code generation

    visitJavaClass(_clazz);
    _out.flush();
  
public voidvisitField(com.sun.org.apache.bcel.internal.classfile.Field field)

    _out.println("\n    field = new FieldGen(" +
		 printFlags(field.getAccessFlags()) +
		 ", " + printType(field.getSignature()) + ", \"" +
		 field.getName() + "\", _cp);");

    ConstantValue cv = field.getConstantValue();

    if(cv != null) {
      String value = cv.toString();
      _out.println("    field.setInitValue(" + value + ")");
    }

    _out.println("    _cg.addField(field.getField());");
  
public voidvisitJavaClass(com.sun.org.apache.bcel.internal.classfile.JavaClass clazz)

    String class_name   = clazz.getClassName();
    String super_name   = clazz.getSuperclassName();
    String package_name = clazz.getPackageName();
    String inter        = Utility.printArray(clazz.getInterfaceNames(),
					     false, true);
    if(!"".equals(package_name)) {
      class_name = class_name.substring(package_name.length() + 1);
      _out.println("package " + package_name + ";\n");
     }

    _out.println("import com.sun.org.apache.bcel.internal.generic.*;");
    _out.println("import com.sun.org.apache.bcel.internal.classfile.*;");
    _out.println("import com.sun.org.apache.bcel.internal.*;");
    _out.println("import java.io.*;\n");

    _out.println("public class " + class_name + "Creator implements Constants {");
    _out.println("  private InstructionFactory _factory;");
    _out.println("  private ConstantPoolGen    _cp;");
    _out.println("  private ClassGen           _cg;\n");

    _out.println("  public " + class_name  + "Creator() {");
    _out.println("    _cg = new ClassGen(\"" +
		 (("".equals(package_name))? class_name :
		  package_name + "." + class_name) +
		 "\", \"" + super_name + "\", " +
		 "\"" + clazz.getSourceFileName() + "\", " +
		 printFlags(clazz.getAccessFlags(), true) + ", " +
		 "new String[] { " + inter + " });\n");

    _out.println("    _cp = _cg.getConstantPool();");
    _out.println("    _factory = new InstructionFactory(_cg, _cp);");
    _out.println("  }\n");

    printCreate();

    Field[] fields = clazz.getFields();

    if(fields.length > 0) {
      _out.println("  private void createFields() {");   
      _out.println("    FieldGen field;");

      for(int i=0; i < fields.length; i++) {
	fields[i].accept(this);
      }

      _out.println("  }\n");   
    }

    Method[] methods = clazz.getMethods();

    for(int i=0; i < methods.length; i++) {
      _out.println("  private void createMethod_" + i + "() {");   

      methods[i].accept(this);
      _out.println("  }\n");   
    }

    printMain();
    _out.println("}");
  
public voidvisitMethod(com.sun.org.apache.bcel.internal.classfile.Method method)

    MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp);

    Type   result_type = mg.getReturnType();
    Type[] arg_types   = mg.getArgumentTypes();

    _out.println("    InstructionList il = new InstructionList();");
    _out.println("    MethodGen method = new MethodGen(" +
		 printFlags(method.getAccessFlags()) +
		 ", " + printType(result_type) +
		 ", " + printArgumentTypes(arg_types) + ", " +
		 "new String[] { " +
		 Utility.printArray(mg.getArgumentNames(), false, true) +
		 " }, \"" + method.getName() + "\", \"" +
		 _clazz.getClassName() + "\", il, _cp);\n");
    
    BCELFactory factory = new BCELFactory(mg, _out);
    factory.start();

    _out.println("    method.setMaxStack();");
    _out.println("    method.setMaxLocals();");
    _out.println("    _cg.addMethod(method.getMethod());");
    _out.println("    il.dispose();");