jaxp/src/com/sun/org/apache/bcel/internal/util/BCELifier.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/bcel/internal/util/BCELifier.java@7f561c08de6b
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
package com.sun.org.apache.bcel.internal.util;
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
/* ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * The Apache Software License, Version 1.1
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * Redistribution and use in source and binary forms, with or without
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * modification, are permitted provided that the following conditions
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * are met:
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * 1. Redistributions of source code must retain the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 *    notice, this list of conditions and the following disclaimer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 * 2. Redistributions in binary form must reproduce the above copyright
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 *    notice, this list of conditions and the following disclaimer in
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 *    the documentation and/or other materials provided with the
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 *    distribution.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
 * 3. The end-user documentation included with the redistribution,
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
 *    if any, must include the following acknowledgment:
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
 *       "This product includes software developed by the
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 *        Apache Software Foundation (http://www.apache.org/)."
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 *    Alternately, this acknowledgment may appear in the software itself,
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 *    if and wherever such third-party acknowledgments normally appear.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * 4. The names "Apache" and "Apache Software Foundation" and
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 *    "Apache BCEL" must not be used to endorse or promote products
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *    derived from this software without prior written permission. For
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 *    written permission, please contact apache@apache.org.
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * 5. Products derived from this software may not be called "Apache",
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *    "Apache BCEL", nor may "Apache" appear in their name, without
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *    prior written permission of the Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * SUCH DAMAGE.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * ====================================================================
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * This software consists of voluntary contributions made by many
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * individuals on behalf of the Apache Software Foundation.  For more
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * information on the Apache Software Foundation, please see
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * <http://www.apache.org/>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
import com.sun.org.apache.bcel.internal.classfile.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
import com.sun.org.apache.bcel.internal.generic.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
import com.sun.org.apache.bcel.internal.Repository;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
import com.sun.org.apache.bcel.internal.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
 * This class takes a given JavaClass object and converts it to a
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
 * Java program that creates that very class using BCEL. This
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
 * gives new users of BCEL a useful example showing how things
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
 * are done with BCEL. It does not cover all features of BCEL,
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
 * but tries to mimic hand-written code as close as possible.
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
public class BCELifier extends com.sun.org.apache.bcel.internal.classfile.EmptyVisitor {
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
  private JavaClass         _clazz;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  private PrintWriter       _out;
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  private ConstantPoolGen   _cp;
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  /** @param clazz Java class to "decompile"
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * @param out where to output Java program
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  public BCELifier(JavaClass clazz, OutputStream out) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    _clazz = clazz;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    _out = new PrintWriter(out);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    _cp = new ConstantPoolGen(_clazz.getConstantPool());
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
  /** Start Java code generation
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  public void start() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    visitJavaClass(_clazz);
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    _out.flush();
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  public void visitJavaClass(JavaClass clazz) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    String class_name   = clazz.getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    String super_name   = clazz.getSuperclassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    String package_name = clazz.getPackageName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    String inter        = Utility.printArray(clazz.getInterfaceNames(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
                                             false, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    if(!"".equals(package_name)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
      class_name = class_name.substring(package_name.length() + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
      _out.println("package " + package_name + ";\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    _out.println("import com.sun.org.apache.bcel.internal.generic.*;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
    _out.println("import com.sun.org.apache.bcel.internal.classfile.*;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    _out.println("import com.sun.org.apache.bcel.internal.*;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    _out.println("import java.io.*;\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    _out.println("public class " + class_name + "Creator implements Constants {");
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    _out.println("  private InstructionFactory _factory;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    _out.println("  private ConstantPoolGen    _cp;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    _out.println("  private ClassGen           _cg;\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    _out.println("  public " + class_name  + "Creator() {");
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    _out.println("    _cg = new ClassGen(\"" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
                 (("".equals(package_name))? class_name :
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
                  package_name + "." + class_name) +
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
                 "\", \"" + super_name + "\", " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
                 "\"" + clazz.getSourceFileName() + "\", " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
                 printFlags(clazz.getAccessFlags(), true) + ", " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
                 "new String[] { " + inter + " });\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    _out.println("    _cp = _cg.getConstantPool();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    _out.println("    _factory = new InstructionFactory(_cg, _cp);");
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    _out.println("  }\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    printCreate();
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    Field[] fields = clazz.getFields();
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
    if(fields.length > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
      _out.println("  private void createFields() {");
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
      _out.println("    FieldGen field;");
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
      for(int i=0; i < fields.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        fields[i].accept(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
      _out.println("  }\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    Method[] methods = clazz.getMethods();
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    for(int i=0; i < methods.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
      _out.println("  private void createMethod_" + i + "() {");
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      methods[i].accept(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
      _out.println("  }\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    printMain();
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    _out.println("}");
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
  private void printCreate() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    _out.println("  public void create(OutputStream out) throws IOException {");
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    Field[] fields = _clazz.getFields();
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    if(fields.length > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
      _out.println("    createFields();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    Method[] methods = _clazz.getMethods();
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    for(int i=0; i < methods.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
      _out.println("    createMethod_" + i + "();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    _out.println("    _cg.getJavaClass().dump(out);");
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    _out.println("  }\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
  private void printMain() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    String   class_name   = _clazz.getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    _out.println("  public static void _main(String[] args) throws Exception {");
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    _out.println("    " + class_name + "Creator creator = new " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
                 class_name + "Creator();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    _out.println("    creator.create(new FileOutputStream(\"" + class_name +
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
                 ".class\"));");
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    _out.println("  }");
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
  public void visitField(Field field) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    _out.println("\n    field = new FieldGen(" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
                 printFlags(field.getAccessFlags()) +
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
                 ", " + printType(field.getSignature()) + ", \"" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
                 field.getName() + "\", _cp);");
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    ConstantValue cv = field.getConstantValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    if(cv != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
      String value = cv.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
      _out.println("    field.setInitValue(" + value + ")");
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    _out.println("    _cg.addField(field.getField());");
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
  public void visitMethod(Method method) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    Type   result_type = mg.getReturnType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
    Type[] arg_types   = mg.getArgumentTypes();
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    _out.println("    InstructionList il = new InstructionList();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    _out.println("    MethodGen method = new MethodGen(" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
                 printFlags(method.getAccessFlags()) +
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
                 ", " + printType(result_type) +
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
                 ", " + printArgumentTypes(arg_types) + ", " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                 "new String[] { " +
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
                 Utility.printArray(mg.getArgumentNames(), false, true) +
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
                 " }, \"" + method.getName() + "\", \"" +
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
                 _clazz.getClassName() + "\", il, _cp);\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
    BCELFactory factory = new BCELFactory(mg, _out);
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
    factory.start();
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
    _out.println("    method.setMaxStack();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
    _out.println("    method.setMaxLocals();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    _out.println("    _cg.addMethod(method.getMethod());");
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    _out.println("    il.dispose();");
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
  static String printFlags(int flags) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    return printFlags(flags, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
  static String printFlags(int flags, boolean for_class) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
    if(flags == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
      return "0";
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    StringBuffer buf = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    for(int i=0, pow=1; i <= Constants.MAX_ACC_FLAG; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
      if((flags & pow) != 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
        if((pow == Constants.ACC_SYNCHRONIZED) && for_class)
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
          buf.append("ACC_SUPER | ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
          buf.append("ACC_" + Constants.ACCESS_NAMES[i].toUpperCase() + " | ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
      pow <<= 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
    String str = buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    return str.substring(0, str.length() - 3);
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
  static String printArgumentTypes(Type[] arg_types) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
    if(arg_types.length == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
      return "Type.NO_ARGS";
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
    StringBuffer args = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
    for(int i=0; i < arg_types.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
      args.append(printType(arg_types[i]));
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
      if(i < arg_types.length - 1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
        args.append(", ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    return "new Type[] { " + args.toString() + " }";
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
  static String printType(Type type) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
    return printType(type.getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
  static String printType(String signature) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    Type type = Type.getType(signature);
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    byte t    = type.getType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
    if(t <= Constants.T_VOID) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
      return "Type." + Constants.TYPE_NAMES[t].toUpperCase();
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
    } else if(type.toString().equals("java.lang.String")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
      return "Type.STRING";
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    } else if(type.toString().equals("java.lang.Object")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
      return "Type.OBJECT";
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
    } else if(type.toString().equals("java.lang.StringBuffer")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
      return "Type.STRINGBUFFER";
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
    } else if(type instanceof ArrayType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
      ArrayType at = (ArrayType)type;
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
      return "new ArrayType(" + printType(at.getBasicType()) +
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
        ", " + at.getDimensions() + ")";
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
      return "new ObjectType(\"" + Utility.signatureToString(signature, false) +
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        "\")";
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
  /** Default _main method
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
  public static void _main(String[] argv) throws Exception {
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
    JavaClass java_class;
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
    String    name = argv[0];
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
    if((java_class = Repository.lookupClass(name)) == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
      java_class = new ClassParser(name).parse(); // May throw IOException
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
    BCELifier bcelifier = new BCELifier(java_class, System.out);
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
    bcelifier.start();
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
}