BCELifier xref

View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.util;
    18
    19  import java.io.OutputStream;
    20  import java.io.PrintWriter;
    21  import java.util.Locale;
    22  import org.apache.bcel.Constants;
    23  import org.apache.bcel.Repository;
    24  import org.apache.bcel.classfile.ClassParser;
    25  import org.apache.bcel.classfile.ConstantValue;
    26  import org.apache.bcel.classfile.Field;
    27  import org.apache.bcel.classfile.JavaClass;
    28  import org.apache.bcel.classfile.Method;
    29  import org.apache.bcel.classfile.Utility;
    30  import org.apache.bcel.generic.ArrayType;
    31  import org.apache.bcel.generic.ConstantPoolGen;
    32  import org.apache.bcel.generic.MethodGen;
    33  import org.apache.bcel.generic.Type;
    34
    35  /***
    36   * This class takes a given JavaClass object and converts it to a
    37   * Java program that creates that very class using BCEL. This
    38   * gives new users of BCEL a useful example showing how things
    39   * are done with BCEL. It does not cover all features of BCEL,
    40   * but tries to mimic hand-written code as close as possible.
    41   *
    42   * @version $Id: BCELifier.java 394939 2006-04-18 13:23:49Z tcurdt $
    43   * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
    44   */
    45  public class BCELifier extends org.apache.bcel.classfile.EmptyVisitor {
    46
    47      private static final int FLAG_FOR_UNKNOWN = -1;
    48      private static final int FLAG_FOR_CLASS = 0;
    49      private static final int FLAG_FOR_METHOD = 1;
    50      private JavaClass _clazz;
    51      private PrintWriter _out;
    52      private ConstantPoolGen _cp;
    53
    54
    55      /*** @param clazz Java class to "decompile"
    56       * @param out where to output Java program
    57       */
    58      public BCELifier(JavaClass clazz, OutputStream out) {
    59          _clazz = clazz;
    60          _out = new PrintWriter(out);
    61          _cp = new ConstantPoolGen(_clazz.getConstantPool());
    62      }
    63
    64
    65      /*** Start Java code generation
    66       */
    67      public void start() {
    68          visitJavaClass(_clazz);
    69          _out.flush();
    70      }
    71
    72
    73      public void visitJavaClass( JavaClass clazz ) {
    74          String class_name = clazz.getClassName();
    75          String super_name = clazz.getSuperclassName();
    76          String package_name = clazz.getPackageName();
    77          String inter = Utility.printArray(clazz.getInterfaceNames(), false, true);
    78          if (!"".equals(package_name)) {
    79              class_name = class_name/substring(package_name/length() + 1)/package-summary.html">strong>_name = class_name.substring(package_name.length() + 1);
    80              _out.println("package " + package_name + ";");
    81              _out.println();
    82          }
    83          _out.println("import org.apache.bcel.generic.*;");
    84          _out.println("import org.apache.bcel.classfile.*;");
    85          _out.println("import org.apache.bcel.*;");
    86          _out.println("import java.io.*;");
    87          _out.println();
    88          _out.println("public class " + class_name + "Creator implements Constants {");
    89          _out.println("  private InstructionFactory _factory;");
    90          _out.println("  private ConstantPoolGen    _cp;");
    91          _out.println("  private ClassGen           _cg;");
    92          _out.println();
    93          _out.println("  public " + class_name + "Creator() {");
    94          _out.println("    _cg = new ClassGen(\""
    95                  + (("".equals(package_name)) ? class_name : package_name + "." + class_name)
    96                  + "\", \"" + super_name + "\", " + "\"" + clazz.getSourceFileName() + "\", "
    97                  + printFlags(clazz.getAccessFlags(), FLAG_FOR_CLASS) + ", " + "new String[] { "
    98                  + inter + " });");
    99          _out.println();
    100         _out.println("    _cp = _cg.getConstantPool();");
    101         _out.println("    _factory = new InstructionFactory(_cg, _cp);");
    102         _out.println("  }");
    103         _out.println();
    104         printCreate();
    105         Field[] fields = clazz.getFields();
    106         if (fields.length > 0) {
    107             _out.println("  private void createFields() {");
    108             _out.println("    FieldGen field;");
    109             for (int i = 0; i < fields.length; i++) {
    110                 fields[i].accept(this);
    111             }
    112             _out.println("  }");
    113             _out.println();
    114         }
    115         Method[] methods = clazz.getMethods();
    116         for (int i = 0; i < methods.length; i++) {
    117             _out.println("  private void createMethod_" + i + "() {");
    118             methods[i].accept(this);
    119             _out.println("  }");
    120             _out.println();
    121         }
    122         printMain();
    123         _out.println("}");
    124     }
    125
    126
    127     private void printCreate() {
    128         _out.println("  public void create(OutputStream out) throws IOException {");
    129         Field[] fields = _clazz.getFields();
    130         if (fields.length > 0) {
    131             _out.println("    createFields();");
    132         }
    133         Method[] methods = _clazz.getMethods();
    134         for (int i = 0; i < methods.length; i++) {
    135             _out.println("    createMethod_" + i + "();");
    136         }
    137         _out.println("    _cg.getJavaClass().dump(out);");
    138         _out.println("  }");
    139         _out.println();
    140     }
    141
    142
    143     private void printMain() {
    144         String class_name = _clazz.getClassName();
    145         _out.println("  public static void main(String[] args) throws Exception {");
    146         _out.println("    " + class_name + "Creator creator = new " + class_name + "Creator();");
    147         _out.println("    creator.create(new FileOutputStream(\"" + class_name + ".class\"));");
    148         _out.println("  }");
    149     }
    150
    151
    152     public void visitField( Field field ) {
    153         _out.println();
    154         _out.println("    field = new FieldGen(" + printFlags(field.getAccessFlags()) + ", "
    155                 + printType(field.getSignature()) + ", \"" + field.getName() + "\", _cp);");
    156         ConstantValue cv = field.getConstantValue();
    157         if (cv != null) {
    158             String value = cv.toString();
    159             _out.println("    field.setInitValue(" + value + ")");
    160         }
    161         _out.println("    _cg.addField(field.getField());");
    162     }
    163
    164
    165     public void visitMethod( Method method ) {
    166         MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp);
    167         Type result_type = mg.getReturnType();
    168         Type[] arg_types = mg.getArgumentTypes();
    169         _out.println("    InstructionList il = new InstructionList();");
    170         _out.println("    MethodGen method = new MethodGen("
    171                 + printFlags(method.getAccessFlags(), FLAG_FOR_METHOD) + ", "
    172                 + printType(result_type) + ", " + printArgumentTypes(arg_types) + ", "
    173                 + "new String[] { " + Utility.printArray(mg.getArgumentNames(), false, true)
    174                 + " }, \"" + method.getName() + "\", \"" + _clazz.getClassName() + "\", il, _cp);");
    175         _out.println();
    176         BCELFactory factory = new BCELFactory(mg, _out);
    177         factory.start();
    178         _out.println("    method.setMaxStack();");
    179         _out.println("    method.setMaxLocals();");
    180         _out.println("    _cg.addMethod(method.getMethod());");
    181         _out.println("    il.dispose();");
    182     }
    183
    184
    185     static String printFlags( int flags ) {
    186         return printFlags(flags, FLAG_FOR_UNKNOWN);
    187     }
    188
    189
    190     static String printFlags( int flags, int reason ) {
    191         if (flags == 0) {
    192             return "0";
    193         }
    194         StringBuffer buf = new StringBuffer();
    195         for (int i = 0, pow = 1; pow <= Constants.MAX_ACC_FLAG; i++) {
    196             if ((flags & pow) != 0) {
    197                 if ((pow == Constants.ACC_SYNCHRONIZED) && (reason == FLAG_FOR_CLASS)) {
    198                     buf.append("ACC_SUPER | ");
    199                 } else if ((pow == Constants.ACC_VOLATILE) && (reason == FLAG_FOR_METHOD)) {
    200                     buf.append("ACC_BRIDGE | ");
    201                 } else if ((pow == Constants.ACC_TRANSIENT) && (reason == FLAG_FOR_METHOD)) {
    202                     buf.append("ACC_VARARGS | ");
    203                 } else {
    204                     buf.append("ACC_")
    205                             .append(Constants.ACCESS_NAMES[i].toUpperCase(Locale.ENGLISH)).append(
    206                                     " | ");
    207                 }
    208             }
    209             pow <<= 1;
    210         }
    211         String str = buf.toString();
    212         return str.substring(0, str.length() - 3);
    213     }
    214
    215
    216     static String printArgumentTypes( Type[] arg_types ) {
    217         if (arg_types.length == 0) {
    218             return "Type.NO_ARGS";
    219         }
    220         StringBuffer args = new StringBuffer();
    221         for (int i = 0; i < arg_types.length; i++) {
    222             args.append(printType(arg_types[i]));
    223             if (i < arg_types.length - 1) {
    224                 args.append(", ");
    225             }
    226         }
    227         return "new Type[] { " + args.toString() + " }";
    228     }
    229
    230
    231     static String printType( Type type ) {
    232         return printType(type.getSignature());
    233     }
    234
    235
    236     static String printType( String signature ) {
    237         Type type = Type.getType(signature);
    238         byte t = type.getType();
    239         if (t <= Constants.T_VOID) {
    240             return "Type." + Constants.TYPE_NAMES[t].toUpperCase(Locale.ENGLISH);
    241         } else if (type.toString().equals("java.lang.String")) {
    242             return "Type.STRING";
    243         } else if (type.toString().equals("java.lang.Object")) {
    244             return "Type.OBJECT";
    245         } else if (type.toString().equals("java.lang.StringBuffer")) {
    246             return "Type.STRINGBUFFER";
    247         } else if (type instanceof ArrayType) {
    248             ArrayType at = (ArrayType) type;
    249             return "new ArrayType(" + printType(at.getBasicType()) + ", " + at.getDimensions()
    250                     + ")";
    251         } else {
    252             return "new ObjectType(\"" + Utility.signatureToString(signature, false) + "\")";
    253         }
    254     }
    255
    256
    257     /*** Default main method
    258      */
    259     public static void main( String[] argv ) throws Exception {
    260         JavaClass java_class;
    261         String name = argv[0];
    262         if ((java_class = Repository.lookupClass(name)) == null) {
    263             java_class = new ClassParser(name).parse(); // May throw IOException
    264         }
    265         BCELifier bcelifier = new BCELifier(java_class, System.out);
    266         bcelifier.start();
    267     }
    268 }