You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2015/02/20 12:11:55 UTC

svn commit: r1661091 [2/2] - in /commons/proper/bcel/trunk/src/examples: HelloWorldBuilder.java JasminVisitor.java Package.java Peephole.java ProxyCreator.java TransitiveHull.java helloify.java id.java listclass.java maxstack.java patchclass.java

Modified: commons/proper/bcel/trunk/src/examples/listclass.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/examples/listclass.java?rev=1661091&r1=1661090&r2=1661091&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/examples/listclass.java (original)
+++ commons/proper/bcel/trunk/src/examples/listclass.java Fri Feb 20 11:11:54 2015
@@ -15,9 +15,12 @@
  *  limitations under the License.
  *
  */
+
 import java.io.IOException;
-import java.util.Hashtable;
-import java.util.Vector;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.apache.bcel.Constants;
 import org.apache.bcel.Repository;
@@ -42,11 +45,11 @@ import org.apache.bcel.classfile.Method;
  * <li>{@code -recurse}  Usually intended to be used along with
  * {@code -dependencies}  When this flag is set, listclass will also print information
  * about all classes which the target class depends on.</li>
- * 
+ *
  * <li>{@code -dependencies}  Setting this flag makes listclass print a list of all
  * classes which the target class depends on.  Generated from getting all
  * CONSTANT_Class constants from the constant pool.</li>
- * 
+ *
  * <li>{@code -exclude}  All non-flag arguments after this flag are added to an
  * 'exclusion list'.  Target classes are compared with the members of the
  * exclusion list.  Any target class whose fully qualified name begins with a
@@ -72,197 +75,205 @@ import org.apache.bcel.classfile.Method;
  * dependents.  Do not analyze classes beginning with "java.", "javax.", or "sun."
  * </p>
  *
+ * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>,
+ *         <a href="mailto:twheeler@objectspace.com">Thomas Wheeler</A>
  * @version $Id$
- * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>,
- * <a href="mailto:twheeler@objectspace.com">Thomas Wheeler</A>
  */
 public class listclass {
-  boolean   code, constants, verbose, classdep, nocontents, recurse;
-  Hashtable<String, String> listedClasses;
-  Vector<String>    exclude_name;
-
-  public static void main(String[] argv) {
-    Vector<String>  file_name    = new Vector<String>();
-    Vector<String>  exclude_name = new Vector<String>();
-    boolean code         = false, constants=false, verbose=true, classdep=false,
-            nocontents=false, recurse=false, exclude=false;
-    String  name         = null;
 
-    /* Parse command line arguments.
-     */
-    for(int i=0; i < argv.length; i++) {
-      if(argv[i].charAt(0) == '-') {  // command line switch
-        if(argv[i].equals("-constants")) {
-            constants=true;
-        } else if(argv[i].equals("-code")) {
-            code=true;
-        } else if(argv[i].equals("-brief")) {
-            verbose=false;
-        } else if(argv[i].equals("-dependencies")) {
-            classdep=true;
-        } else if(argv[i].equals("-nocontents")) {
-            nocontents=true;
-        } else if(argv[i].equals("-recurse")) {
-            recurse=true;
-        } else if(argv[i].equals("-exclude")) {
-            exclude=true;
-        } else if(argv[i].equals("-help")) {
-          System.out.println( "Usage: java listclass [-constants] [-code] [-brief] " +
-                  "[-dependencies] [-nocontents] [-recurse] class... " +
-                  "[-exclude <list>]\n" +
-                              "-constants       Print constants table (constant pool)\n" +
-                              "-code            Dump byte code of methods\n" +
-                              "-brief           Brief listing\n" +
-                              "-dependencies    Show class dependencies\n" +
-                              "-nocontents      Do not print field/method information\n" +
-                              "-recurse         Recurse into dependent classes\n" +
-                              "-exclude <list>  Do not list classes beginning with " +
-                  "strings in <list>" );
-          System.exit( 0 );
-    } else {
-            System.err.println("Unknown switch " + argv[i] + " ignored.");
-        }
-      } else { // add file name to list
-        if(exclude) {
-            exclude_name.addElement(argv[i]);
-        } else {
-            file_name.addElement(argv[i]);
+    boolean code;
+    boolean constants;
+    boolean verbose;
+    boolean classdep;
+    boolean nocontents;
+    boolean recurse;
+    Map<String, String> listedClasses;
+    List<String> exclude_name;
+
+    public static void main(String[] argv) {
+        List<String> file_name = new ArrayList<String>();
+        List<String> exclude_name = new ArrayList<String>();
+        boolean code = false;
+        boolean constants = false;
+        boolean verbose = true;
+        boolean classdep = false;
+        boolean nocontents = false;
+        boolean recurse = false;
+        boolean exclude = false;
+        String name;
+
+        // Parse command line arguments.
+        for (String arg : argv) {
+            if (arg.charAt(0) == '-') {  // command line switch
+                if (arg.equals("-constants")) {
+                    constants = true;
+                } else if (arg.equals("-code")) {
+                    code = true;
+                } else if (arg.equals("-brief")) {
+                    verbose = false;
+                } else if (arg.equals("-dependencies")) {
+                    classdep = true;
+                } else if (arg.equals("-nocontents")) {
+                    nocontents = true;
+                } else if (arg.equals("-recurse")) {
+                    recurse = true;
+                } else if (arg.equals("-exclude")) {
+                    exclude = true;
+                } else if (arg.equals("-help")) {
+                    System.out.println("Usage: java listclass [-constants] [-code] [-brief] " +
+                            "[-dependencies] [-nocontents] [-recurse] class... " +
+                            "[-exclude <list>]\n" +
+                            "-constants       Print constants table (constant pool)\n" +
+                            "-code            Dump byte code of methods\n" +
+                            "-brief           Brief listing\n" +
+                            "-dependencies    Show class dependencies\n" +
+                            "-nocontents      Do not print field/method information\n" +
+                            "-recurse         Recurse into dependent classes\n" +
+                            "-exclude <list>  Do not list classes beginning with " +
+                            "strings in <list>");
+                    System.exit(0);
+                } else {
+                    System.err.println("Unknown switch " + arg + " ignored.");
+                }
+            } else { // add file name to list
+                if (exclude) {
+                    exclude_name.add(arg);
+                } else {
+                    file_name.add(arg);
+                }
+            }
         }
-      }
-    }
-
-    if(file_name.size() == 0) {
-        System.err.println("list: No input files specified");
-    } else {
-      listclass listClass = new listclass(code, constants, verbose, classdep,
-                      nocontents, recurse, exclude_name);
-
-      for(int i=0; i < file_name.size(); i++) {
-        name = file_name.elementAt(i);
 
-        listClass.list(name);
-      }
-    }
-  }
-
-  public listclass(boolean code, boolean constants, boolean verbose, boolean classdep,
-           boolean nocontents, boolean recurse, Vector<String> exclude_name)
-  {
-    this.code = code;
-    this.constants = constants;
-    this.verbose = verbose;
-    this.classdep = classdep;
-    this.nocontents = nocontents;
-    this.recurse = recurse;
-    this.listedClasses = new Hashtable<String, String>();
-    this.exclude_name = exclude_name;
-  }
-
-  /** Print the given class on screen
-   */
-  public void list(String name) {
-    try {
-      JavaClass java_class;
+        if (file_name.size() == 0) {
+            System.err.println("list: No input files specified");
+        } else {
+            listclass listClass = new listclass(code, constants, verbose, classdep,
+                    nocontents, recurse, exclude_name);
 
-      if((listedClasses.get(name) != null) || name.startsWith("[")) {
-        return;
-    }
+            for (int i = 0; i < file_name.size(); i++) {
+                name = file_name.get(i);
 
-      for(int idx = 0; idx < exclude_name.size(); idx++) {
-        if(name.startsWith(exclude_name.elementAt(idx))) {
-            return;
+                listClass.list(name);
+            }
         }
     }
 
-      if(name.endsWith(".class")) {
-        java_class = new ClassParser(name).parse(); // May throw IOException
-      } else {
-    java_class = Repository.lookupClass(name);
-      }
-
-      if(nocontents) {
-        System.out.println(java_class.getClassName());
-    } else {
-        System.out.println(java_class);             // Dump the contents
-    }
-
-      if(constants) {
-        System.out.println(java_class.getConstantPool());
+    public listclass(boolean code, boolean constants, boolean verbose, boolean classdep,
+                     boolean nocontents, boolean recurse, List<String> exclude_name) {
+        this.code = code;
+        this.constants = constants;
+        this.verbose = verbose;
+        this.classdep = classdep;
+        this.nocontents = nocontents;
+        this.recurse = recurse;
+        this.listedClasses = new HashMap<String, String>();
+        this.exclude_name = exclude_name;
     }
 
-      if(code) {
-        printCode(java_class.getMethods(), verbose);
-    }
-
-      if(classdep) {
-        printClassDependencies(java_class.getConstantPool());
-    }
-
-      listedClasses.put(name, name);
-
-      if(recurse) {
-        String[] dependencies = getClassDependencies(java_class.getConstantPool());
-
-        for(int idx = 0; idx < dependencies.length; idx++) {
-            list(dependencies[idx]);
+    /**
+     * Print the given class on screen
+     */
+    public void list(String name) {
+        try {
+            JavaClass java_class;
+
+            if ((listedClasses.get(name) != null) || name.startsWith("[")) {
+                return;
+            }
+
+            for (int idx = 0; idx < exclude_name.size(); idx++) {
+                if (name.startsWith(exclude_name.get(idx))) {
+                    return;
+                }
+            }
+
+            if (name.endsWith(".class")) {
+                java_class = new ClassParser(name).parse(); // May throw IOException
+            } else {
+                java_class = Repository.lookupClass(name);
+            }
+
+            if (nocontents) {
+                System.out.println(java_class.getClassName());
+            } else {
+                System.out.println(java_class);             // Dump the contents
+            }
+
+            if (constants) {
+                System.out.println(java_class.getConstantPool());
+            }
+
+            if (code) {
+                printCode(java_class.getMethods(), verbose);
+            }
+
+            if (classdep) {
+                printClassDependencies(java_class.getConstantPool());
+            }
+
+            listedClasses.put(name, name);
+
+            if (recurse) {
+                String[] dependencies = getClassDependencies(java_class.getConstantPool());
+
+                for (String dependency : dependencies) {
+                    list(dependency);
+                }
+            }
+        } catch (IOException e) {
+            System.out.println("Error loading class " + name + " (" + e.getMessage() + ")");
+        } catch (Exception e) {
+            System.out.println("Error processing class " + name + " (" + e.getMessage() + ")");
         }
-      }
-    } catch(IOException e) {
-      System.out.println("Error loading class " + name + " (" + e.getMessage() + ")");
-    }
-    catch(Exception e) {
-      System.out.println("Error processing class " + name + " (" + e.getMessage() + ")");
     }
-  }
 
-  /**
-   * Dump the list of classes this class is dependent on
-   */
-  public static void printClassDependencies(ConstantPool pool) {
-    String[] names = getClassDependencies(pool);
-    System.out.println("Dependencies:");
-    for(int idx = 0; idx < names.length; idx++) {
-        System.out.println("\t" + names[idx]);
+    /**
+     * Dump the list of classes this class is dependent on
+     */
+    public static void printClassDependencies(ConstantPool pool) {
+        System.out.println("Dependencies:");
+        for (String name : getClassDependencies(pool)) {
+            System.out.println("\t" + name);
+        }
     }
-  }
 
-  public static String[] getClassDependencies(ConstantPool pool) {
-    String[] tempArray = new String[pool.getLength()];
-    int size = 0;
-    StringBuffer buf = new StringBuffer();
+    public static String[] getClassDependencies(ConstantPool pool) {
+        String[] tempArray = new String[pool.getLength()];
+        int size = 0;
+        StringBuilder buf = new StringBuilder();
+
+        for (int idx = 0; idx < pool.getLength(); idx++) {
+            Constant c = pool.getConstant(idx);
+            if (c != null && c.getTag() == Constants.CONSTANT_Class) {
+                ConstantUtf8 c1 = (ConstantUtf8) pool.getConstant(((ConstantClass) c).getNameIndex());
+                buf.setLength(0);
+                buf.append(c1.getBytes());
+                for (int n = 0; n < buf.length(); n++) {
+                    if (buf.charAt(n) == '/') {
+                        buf.setCharAt(n, '.');
+                    }
+                }
 
-    for(int idx = 0; idx < pool.getLength(); idx++) {
-      Constant c = pool.getConstant(idx);
-      if(c != null && c.getTag() == Constants.CONSTANT_Class) {
-        ConstantUtf8 c1 = (ConstantUtf8) pool.getConstant(((ConstantClass)c).getNameIndex());
-        buf.setLength(0);
-        buf.append(c1.getBytes());
-        for(int n = 0; n < buf.length(); n++) {
-          if(buf.charAt(n) == '/') {
-            buf.setCharAt(n, '.');
+                tempArray[size++] = buf.toString();
+            }
         }
-    }
 
-        tempArray[size++] = buf.toString();
-      }
+        String[] dependencies = new String[size];
+        System.arraycopy(tempArray, 0, dependencies, 0, size);
+        return dependencies;
     }
 
-    String[] dependencies = new String[size];
-    System.arraycopy(tempArray, 0, dependencies, 0, size);
-    return dependencies;
-  }
-
-  /**
-   * Dump the disassembled code of all methods in the class.
-   */
-  public static void printCode(Method[] methods, boolean verbose) {
-    for(int i=0; i < methods.length; i++) {
-      System.out.println(methods[i]);
-      
-      Code code = methods[i].getCode();
-      if(code != null) {
-        System.out.println(code.toString(verbose));
-    }
+    /**
+     * Dump the disassembled code of all methods in the class.
+     */
+    public static void printCode(Method[] methods, boolean verbose) {
+        for (Method method : methods) {
+            System.out.println(method);
+
+            Code code = method.getCode();
+            if (code != null) {
+                System.out.println(code.toString(verbose));
+            }
+        }
     }
-  }
 }

Modified: commons/proper/bcel/trunk/src/examples/maxstack.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/examples/maxstack.java?rev=1661091&r1=1661090&r2=1661091&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/examples/maxstack.java (original)
+++ commons/proper/bcel/trunk/src/examples/maxstack.java Fri Feb 20 11:11:54 2015
@@ -15,6 +15,7 @@
  *  limitations under the License.
  *
  */
+
 import org.apache.bcel.Repository;
 import org.apache.bcel.classfile.ClassParser;
 import org.apache.bcel.classfile.JavaClass;
@@ -26,58 +27,49 @@ import org.apache.bcel.generic.MethodGen
  * Read class file(s) and examine all of its methods, determining the
  * maximum stack depth used by analyzing control flow.
  *
- * @version $Id$
  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
+ * @version $Id$
  */
 public final class maxstack {
-  public static void main(String[] argv) { 
-    try {
-      for(int i = 0; i < argv.length; i++) {
-    String    class_name = argv[i];
-    JavaClass java_class = Repository.lookupClass(class_name);
 
-    if(java_class == null) {
-        java_class = new ClassParser(class_name).parse();
-    }
-
-    ConstantPoolGen cp      = new ConstantPoolGen(java_class.getConstantPool());
-       Method[]        methods = java_class.getMethods();
-    
-    for(int j = 0; j < methods.length; j++) {
-      Method    m  = methods[j];
-
-      if(!(m.isAbstract() || m.isNative())) {
-        MethodGen mg = new MethodGen(m, argv[i], cp);
-
-        int compiled_stack  = mg.getMaxStack();
-        int compiled_locals = mg.getMaxLocals();
-        mg.setMaxStack(); // Recompute value
-        mg.setMaxLocals();
-        int computed_stack  = mg.getMaxStack();
-        int computed_locals = mg.getMaxLocals();
-
-        mg.getInstructionList().dispose(); // Reuse instruction handles
-        
-        System.out.println(m);
-        
-        if(computed_stack == compiled_stack) {
-            System.out.println("Stack ok(" + computed_stack + ")");
-        } else {
-            System.out.println("\nCompiled stack size " + compiled_stack +
-                       " computed size " + computed_stack);
-        }
-        
-        if(computed_locals == compiled_locals) {
-            System.out.println("Locals ok(" + computed_locals + ")");
-        } else {
-            System.out.println("\nCompiled locals " + compiled_locals +
-                     " computed size " + computed_locals);
+    public static void main(String[] argv) throws Exception {
+        for (String class_name : argv) {
+            JavaClass java_class = Repository.lookupClass(class_name);
+
+            if (java_class == null) {
+                java_class = new ClassParser(class_name).parse();
+            }
+
+            ConstantPoolGen cp = new ConstantPoolGen(java_class.getConstantPool());
+
+            for (Method m : java_class.getMethods()) {
+                if (!(m.isAbstract() || m.isNative())) {
+                    MethodGen mg = new MethodGen(m, class_name, cp);
+
+                    int compiled_stack = mg.getMaxStack();
+                    int compiled_locals = mg.getMaxLocals();
+                    mg.setMaxStack(); // Recompute value
+                    mg.setMaxLocals();
+                    int computed_stack = mg.getMaxStack();
+                    int computed_locals = mg.getMaxLocals();
+
+                    mg.getInstructionList().dispose(); // Reuse instruction handles
+
+                    System.out.println(m);
+
+                    if (computed_stack == compiled_stack) {
+                        System.out.println("Stack ok(" + computed_stack + ")");
+                    } else {
+                        System.out.println("\nCompiled stack size " + compiled_stack + " computed size " + computed_stack);
+                    }
+
+                    if (computed_locals == compiled_locals) {
+                        System.out.println("Locals ok(" + computed_locals + ")");
+                    } else {
+                        System.out.println("\nCompiled locals " + compiled_locals + " computed size " + computed_locals);
+                    }
+                }
+            }
         }
-      }
-    }
-      }
-    } catch(Exception e) {
-      e.printStackTrace();
     }
-  }
 }

Modified: commons/proper/bcel/trunk/src/examples/patchclass.java
URL: http://svn.apache.org/viewvc/commons/proper/bcel/trunk/src/examples/patchclass.java?rev=1661091&r1=1661090&r2=1661091&view=diff
==============================================================================
--- commons/proper/bcel/trunk/src/examples/patchclass.java (original)
+++ commons/proper/bcel/trunk/src/examples/patchclass.java Fri Feb 20 11:11:54 2015
@@ -15,6 +15,7 @@
  *  limitations under the License.
  *
  */
+
 import org.apache.bcel.classfile.ClassParser;
 import org.apache.bcel.classfile.Constant;
 import org.apache.bcel.classfile.ConstantUtf8;
@@ -26,84 +27,76 @@ import org.apache.bcel.classfile.JavaCla
  *
  * Usage: patch <oldstring> <newstring> files
  *
- * @version $Id$
  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
+ * @version $Id$
  */
 public class patchclass {
-  public static void main(String[] argv) { 
-    String[]    file_name = new String[argv.length];
-    int         files     = 0;
-    ClassParser parser    = null;
-    JavaClass   java_class;
-
-    if(argv.length < 3) {
-      System.err.println("Usage: patch <oldstring> <newstring> file1.class ...");
-      System.exit(-1);
-    }
 
-    try {
-      for(int i=2; i < argv.length; i++) {
-        file_name[files++] = argv[i];
-    }
-    
-      for(int i=0; i < files; i++) {
-    parser     = new ClassParser(file_name[i]);
-    java_class = parser.parse();
-    
-    patchIt(argv[0], argv[1],
-        java_class.getConstantPool().getConstantPool());
-
-    // Dump the changed class to a new file
-    java_class.dump("_" + file_name[i]);
-    System.out.println("Results saved in: _" +  file_name[i]);
-      }      
-    } catch(Exception e) {
-      System.err.println(e);
-    }
-  }  
-  /*
-   * Replace all occurences of string "<em>old</em>" with 
-   * "<em>replacement</em>" in all Utf8 constants
-   */
-  private static void patchIt(String old, String replacement,
-                  Constant[] constant_pool)
-  {
-    ConstantUtf8 c;
-    String       str;
-    int          index, old_index;
-    StringBuffer buf;
+    public static void main(String[] argv) throws Exception {
+        String[] file_name = new String[argv.length];
+        int files = 0;
+
+        if (argv.length < 3) {
+            System.err.println("Usage: patch <oldstring> <newstring> file1.class ...");
+            System.exit(-1);
+        }
 
-    /* Loop through constant pool
-     */
-    for(short i=0; i < constant_pool.length; i++) {
-      if(constant_pool[i] instanceof ConstantUtf8) { // Utf8 string found
-    try {
-      c   = (ConstantUtf8)constant_pool[i]; // Get the string
-      str = c.getBytes();
-      
-      if((index = str.indexOf(old)) != -1) { // `old' found in str
-        buf       = new StringBuffer();      // target buffer
-        old_index = 0;                       // String start offset
-        
-        // While we have something to replace
-        while((index = str.indexOf(old, old_index)) != -1) {
-          buf.append(str.substring(old_index, index)); // append prefix
-          buf.append(replacement);               // append `replacement'
-          
-          old_index = index + old.length(); // Skip `old'.length chars
+        for (int i = 2; i < argv.length; i++) {
+            file_name[files++] = argv[i];
         }
 
-        buf.append(str.substring(old_index)); // append rest of string
-        str = buf.toString();
+        for (int i = 0; i < files; i++) {
+            ClassParser parser = new ClassParser(file_name[i]);
+            JavaClass java_class = parser.parse();
+
+            patchIt(argv[0], argv[1], java_class.getConstantPool().getConstantPool());
+
+            // Dump the changed class to a new file
+            java_class.dump("_" + file_name[i]);
+            System.out.println("Results saved in: _" + file_name[i]);
+        }
+    }
 
-        // Finally push the new string back to the constant pool
-        c = new ConstantUtf8(str);
-        constant_pool[i] = c;
-      }
-    } catch(StringIndexOutOfBoundsException e) { // Should not occur
-      System.err.println(e);
+    /*
+     * Replace all occurences of string "<em>old</em>" with 
+     * "<em>replacement</em>" in all Utf8 constants
+     */
+    private static void patchIt(String old, String replacement, Constant[] constant_pool) {
+        ConstantUtf8 c;
+        String str;
+        int index, old_index;
+        StringBuffer buf;
+
+        // Loop through constant pool
+        for (short i = 0; i < constant_pool.length; i++) {
+            if (constant_pool[i] instanceof ConstantUtf8) { // Utf8 string found
+                try {
+                    c = (ConstantUtf8) constant_pool[i]; // Get the string
+                    str = c.getBytes();
+
+                    if ((index = str.indexOf(old)) != -1) { // `old' found in str
+                        buf = new StringBuffer();           // target buffer
+                        old_index = 0;                      // String start offset
+
+                        // While we have something to replace
+                        while ((index = str.indexOf(old, old_index)) != -1) {
+                            buf.append(str.substring(old_index, index)); // append prefix
+                            buf.append(replacement);                     // append `replacement'
+
+                            old_index = index + old.length(); // Skip `old'.length chars
+                        }
+
+                        buf.append(str.substring(old_index)); // append rest of string
+                        str = buf.toString();
+
+                        // Finally push the new string back to the constant pool
+                        c = new ConstantUtf8(str);
+                        constant_pool[i] = c;
+                    }
+                } catch (StringIndexOutOfBoundsException e) { // Should not occur
+                    System.err.println(e);
+                }
+            }
+        }
     }
-      }
-    } 
-  }  
 }