You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-dev@jakarta.apache.org by tc...@apache.org on 2006/03/15 12:33:43 UTC

svn commit: r386056 [11/28] - in /jakarta/bcel/trunk: examples/ examples/Mini/ src/java/org/apache/bcel/ src/java/org/apache/bcel/classfile/ src/java/org/apache/bcel/generic/ src/java/org/apache/bcel/util/ src/java/org/apache/bcel/verifier/ src/java/or...

Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CPInstruction.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CPInstruction.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CPInstruction.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CPInstruction.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License. 
  *
- */ 
+ */
 package org.apache.bcel.generic;
 
-
 import java.io.DataOutputStream;
 import java.io.IOException;
 import org.apache.bcel.classfile.Constant;
@@ -35,97 +34,105 @@
  * @version $Id$
  * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
  */
-public abstract class CPInstruction extends Instruction
-  implements TypedInstruction, IndexedInstruction
-{
-  protected int index; // index to constant pool
-
-  /**
-   * Empty constructor needed for the Class.newInstance() statement in
-   * Instruction.readInstruction(). Not to be used otherwise.
-   */
-  CPInstruction() {}
-
-  /**
-   * @param index to constant pool
-   */
-  protected CPInstruction(short opcode, int index) {
-    super(opcode, (short)3);
-    setIndex(index);
-  }
-
-  /**
-   * Dump instruction as byte code to stream out.
-   * @param out Output stream
-   */
-  public void dump(DataOutputStream out) throws IOException {
-    out.writeByte(opcode);
-    out.writeShort(index);
-  }
-
-  /**
-   * Long output format:
-   *
-   * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" 
-   * "("&lt;length of instruction&gt;")" "&lt;"&lt; constant pool index&gt;"&gt;"
-   *
-   * @param verbose long/short format switch
-   * @return mnemonic for instruction
-   */
-  public String toString(boolean verbose) {
-    return super.toString(verbose) + " " + index;
-  }
-
-  /**
-   * @return mnemonic for instruction with symbolic references resolved
-   */
-  public String toString(ConstantPool cp) {
-    Constant c   = cp.getConstant(index);
-    String   str = cp.constantToString(c);
-
-    if(c instanceof ConstantClass)
-      str = str.replace('.', '/');
-
-    return org.apache.bcel.Constants.OPCODE_NAMES[opcode] + " " + str;
-  }
-
-  /**
-   * Read needed data (i.e., index) from file.
-   * @param bytes input stream
-   * @param wide wide prefix?
-   */
-  protected void initFromFile(ByteSequence bytes, boolean wide)
-       throws IOException
-  {
-    setIndex(bytes.readUnsignedShort());
-    length = 3;
-  }
-
-  /**
-   * @return index in constant pool referred by this instruction.
-   */
-  public final int getIndex() { return index; }
-
-  /**
-   * Set the index to constant pool.
-   * @param index in  constant pool.
-   */
-  public void setIndex(int index) { 
-    if(index < 0)
-      throw new ClassGenException("Negative index value: " + index);
-
-    this.index = index;
-  }
-
-  /** @return type related with this instruction.
-   */
-  public Type getType(ConstantPoolGen cpg) {
-    ConstantPool cp   = cpg.getConstantPool();
-    String       name = cp.getConstantString(index, org.apache.bcel.Constants.CONSTANT_Class);
+public abstract class CPInstruction extends Instruction implements TypedInstruction,
+        IndexedInstruction {
+
+    protected int index; // index to constant pool
 
-    if(!name.startsWith("["))
-      name = "L" + name + ";";
 
-    return Type.getType(name);
-  }
+    /**
+     * Empty constructor needed for the Class.newInstance() statement in
+     * Instruction.readInstruction(). Not to be used otherwise.
+     */
+    CPInstruction() {
+    }
+
+
+    /**
+     * @param index to constant pool
+     */
+    protected CPInstruction(short opcode, int index) {
+        super(opcode, (short) 3);
+        setIndex(index);
+    }
+
+
+    /**
+     * Dump instruction as byte code to stream out.
+     * @param out Output stream
+     */
+    public void dump( DataOutputStream out ) throws IOException {
+        out.writeByte(opcode);
+        out.writeShort(index);
+    }
+
+
+    /**
+     * Long output format:
+     *
+     * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]" 
+     * "("&lt;length of instruction&gt;")" "&lt;"&lt; constant pool index&gt;"&gt;"
+     *
+     * @param verbose long/short format switch
+     * @return mnemonic for instruction
+     */
+    public String toString( boolean verbose ) {
+        return super.toString(verbose) + " " + index;
+    }
+
+
+    /**
+     * @return mnemonic for instruction with symbolic references resolved
+     */
+    public String toString( ConstantPool cp ) {
+        Constant c = cp.getConstant(index);
+        String str = cp.constantToString(c);
+        if (c instanceof ConstantClass) {
+            str = str.replace('.', '/');
+        }
+        return org.apache.bcel.Constants.OPCODE_NAMES[opcode] + " " + str;
+    }
+
+
+    /**
+     * Read needed data (i.e., index) from file.
+     * @param bytes input stream
+     * @param wide wide prefix?
+     */
+    protected void initFromFile( ByteSequence bytes, boolean wide ) throws IOException {
+        setIndex(bytes.readUnsignedShort());
+        length = 3;
+    }
+
+
+    /**
+     * @return index in constant pool referred by this instruction.
+     */
+    public final int getIndex() {
+        return index;
+    }
+
+
+    /**
+     * Set the index to constant pool.
+     * @param index in  constant pool.
+     */
+    public void setIndex( int index ) {
+        if (index < 0) {
+            throw new ClassGenException("Negative index value: " + index);
+        }
+        this.index = index;
+    }
+
+
+    /** @return type related with this instruction.
+     */
+    public Type getType( ConstantPoolGen cpg ) {
+        ConstantPool cp = cpg.getConstantPool();
+        String name = cp.getConstantString(index, org.apache.bcel.Constants.CONSTANT_Class);
+        if (!name.startsWith("[")) {
+            name = "L" + name + ";";
+        }
+        return Type.getType(name);
+    }
 }

Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGen.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGen.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGen.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGen.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License. 
  *
- */ 
+ */
 package org.apache.bcel.generic;
 
-
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -39,500 +38,504 @@
  * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
  */
 public class ClassGen extends AccessFlags implements Cloneable {
-  /* Corresponds to the fields found in a JavaClass object.
-   */
-  private String class_name, super_class_name, file_name;
-  private int class_name_index = -1, superclass_name_index = -1;
-  private int major = Constants.MAJOR_1_1, minor = Constants.MINOR_1_1;
-
-  private ConstantPoolGen cp; // Template for building up constant pool
-
-  // ArrayLists instead of arrays to gather fields, methods, etc.
-  private List field_vec = new ArrayList();
-  private List method_vec = new ArrayList();
-  private List attribute_vec = new ArrayList();
-  private List interface_vec = new ArrayList();
-
-  private static BCELComparator _cmp = new BCELComparator() {
-    public boolean equals(Object o1, Object o2) {
-      ClassGen THIS = (ClassGen)o1;
-      ClassGen THAT = (ClassGen)o2;
-
-      return THIS.getClassName().equals(THAT.getClassName());
-    }
-
-    public int hashCode(Object o) {
-      ClassGen THIS = (ClassGen)o;
-      return THIS.getClassName().hashCode();
-    }
-  };
-
-  /** Convenience constructor to set up some important values initially.
-   *
-   * @param class_name fully qualified class name
-   * @param super_class_name fully qualified superclass name
-   * @param file_name source file name
-   * @param access_flags access qualifiers
-   * @param interfaces implemented interfaces
-   * @param cp constant pool to use
-   */
-  public ClassGen(
-    String class_name,
-    String super_class_name,
-    String file_name,
-    int access_flags,
-    String[] interfaces,
-    ConstantPoolGen cp) {
-    this.class_name = class_name;
-    this.super_class_name = super_class_name;
-    this.file_name = file_name;
-    this.access_flags = access_flags;
-    this.cp = cp;
-
-    // Put everything needed by default into the constant pool and the vectors
-    if (file_name != null)
-      addAttribute(
-        new SourceFile(
-          cp.addUtf8("SourceFile"),
-          2,
-          cp.addUtf8(file_name),
-          cp.getConstantPool()));
-
-    class_name_index = cp.addClass(class_name);
-    superclass_name_index = cp.addClass(super_class_name);
-
-    if (interfaces != null)
-      for (int i = 0; i < interfaces.length; i++)
-        addInterface(interfaces[i]);
-  }
-
-  /** Convenience constructor to set up some important values initially.
-   *
-   * @param class_name fully qualified class name
-   * @param super_class_name fully qualified superclass name
-   * @param file_name source file name
-   * @param access_flags access qualifiers
-   * @param interfaces implemented interfaces
-   */
-  public ClassGen(
-    String class_name,
-    String super_class_name,
-    String file_name,
-    int access_flags,
-    String[] interfaces) {
-    this(
-      class_name,
-      super_class_name,
-      file_name,
-      access_flags,
-      interfaces,
-      new ConstantPoolGen());
-  }
-
-  /**
-   * Initialize with existing class.
-   * @param clazz JavaClass object (e.g. read from file)
-   */
-  public ClassGen(JavaClass clazz) {
-    class_name_index = clazz.getClassNameIndex();
-    superclass_name_index = clazz.getSuperclassNameIndex();
-    class_name = clazz.getClassName();
-    super_class_name = clazz.getSuperclassName();
-    file_name = clazz.getSourceFileName();
-    access_flags = clazz.getAccessFlags();
-    cp = new ConstantPoolGen(clazz.getConstantPool());
-    major = clazz.getMajor();
-    minor = clazz.getMinor();
-
-    Attribute[] attributes = clazz.getAttributes();
-    Method[] methods = clazz.getMethods();
-    Field[] fields = clazz.getFields();
-    String[] interfaces = clazz.getInterfaceNames();
-
-    for (int i = 0; i < interfaces.length; i++)
-      addInterface(interfaces[i]);
-
-    for (int i = 0; i < attributes.length; i++)
-      addAttribute(attributes[i]);
-
-    for (int i = 0; i < methods.length; i++)
-      addMethod(methods[i]);
-
-    for (int i = 0; i < fields.length; i++)
-      addField(fields[i]);
-  }
-
-  /**
-   * @return the (finally) built up Java class object.
-   */
-  public JavaClass getJavaClass() {
-    int[] interfaces = getInterfaces();
-    Field[] fields = getFields();
-    Method[] methods = getMethods();
-    Attribute[] attributes = getAttributes();
-
-    // Must be last since the above calls may still add something to it
-    ConstantPool _cp = this.cp.getFinalConstantPool();
-
-    return new JavaClass(
-      class_name_index,
-      superclass_name_index,
-      file_name,
-      major,
-      minor,
-      access_flags,
-      _cp,
-      interfaces,
-      fields,
-      methods,
-      attributes);
-  }
-
-  /**
-   * Add an interface to this class, i.e., this class has to implement it.
-   * @param name interface to implement (fully qualified class name)
-   */
-  public void addInterface(String name) {
-    interface_vec.add(name);
-  }
-
-  /**
-   * Remove an interface from this class.
-   * @param name interface to remove (fully qualified name)
-   */
-  public void removeInterface(String name) {
-    interface_vec.remove(name);
-  }
-
-  /**
-   * @return major version number of class file
-   */
-  public int getMajor() {
-    return major;
-  }
-
-  /** Set major version number of class file, default value is 45 (JDK 1.1)
-   * @param major major version number
-   */
-  public void setMajor(int major) {
-    this.major = major;
-  }
-
-  /** Set minor version number of class file, default value is 3 (JDK 1.1)
-   * @param minor minor version number
-   */
-  public void setMinor(int minor) {
-    this.minor = minor;
-  }
-
-  /**
-   * @return minor version number of class file
-   */
-  public int getMinor() {
-    return minor;
-  }
-
-  /**
-   * Add an attribute to this class.
-   * @param a attribute to add
-   */
-  public void addAttribute(Attribute a) {
-    attribute_vec.add(a);
-  }
-
-  /**
-   * Add a method to this class.
-   * @param m method to add
-   */
-  public void addMethod(Method m) {
-    method_vec.add(m);
-  }
-
-  /**
-   * Convenience method.
-   *
-   * Add an empty constructor to this class that does nothing but calling super().
-   * @param access_flags rights for constructor
-   */
-  public void addEmptyConstructor(int access_flags) {
-    InstructionList il = new InstructionList();
-    il.append(InstructionConstants.THIS); // Push `this'
-    il.append(
-      new INVOKESPECIAL(cp.addMethodref(super_class_name, "<init>", "()V")));
-    il.append(InstructionConstants.RETURN);
-
-    MethodGen mg =
-      new MethodGen(
-        access_flags,
-        Type.VOID,
-        Type.NO_ARGS,
-        null,
-        "<init>",
-        class_name,
-        il,
-        cp);
-    mg.setMaxStack(1);
-    addMethod(mg.getMethod());
-  }
-
-  /**
-   * Add a field to this class.
-   * @param f field to add
-   */
-  public void addField(Field f) {
-    field_vec.add(f);
-  }
-
-  public boolean containsField(Field f) {
-    return field_vec.contains(f);
-  }
-
-  /** @return field object with given name, or null
-   */
-  public Field containsField(String name) {
-    for (Iterator e = field_vec.iterator(); e.hasNext();) {
-      Field f = (Field)e.next();
-      if (f.getName().equals(name))
-        return f;
-    }
-
-    return null;
-  }
-
-  /** @return method object with given name and signature, or null
-   */
-  public Method containsMethod(String name, String signature) {
-    for (Iterator e = method_vec.iterator(); e.hasNext();) {
-      Method m = (Method)e.next();
-      if (m.getName().equals(name) && m.getSignature().equals(signature))
-        return m;
-    }
-
-    return null;
-  }
-
-  /**
-   * Remove an attribute from this class.
-   * @param a attribute to remove
-   */
-  public void removeAttribute(Attribute a) {
-    attribute_vec.remove(a);
-  }
-
-  /**
-   * Remove a method from this class.
-   * @param m method to remove
-   */
-  public void removeMethod(Method m) {
-    method_vec.remove(m);
-  }
-
-  /** Replace given method with new one. If the old one does not exist
-   * add the new_ method to the class anyway.
-   */
-  public void replaceMethod(Method old, Method new_) {
-    if (new_ == null)
-      throw new ClassGenException("Replacement method must not be null");
-
-    int i = method_vec.indexOf(old);
-
-    if (i < 0)
-      method_vec.add(new_);
-    else
-      method_vec.set(i, new_);
-  }
-
-  /** Replace given field with new one. If the old one does not exist
-   * add the new_ field to the class anyway.
-   */
-  public void replaceField(Field old, Field new_) {
-    if (new_ == null)
-      throw new ClassGenException("Replacement method must not be null");
-
-    int i = field_vec.indexOf(old);
-
-    if (i < 0)
-      field_vec.add(new_);
-    else
-      field_vec.set(i, new_);
-  }
-
-  /**
-   * Remove a field to this class.
-   * @param f field to remove
-   */
-  public void removeField(Field f) {
-    field_vec.remove(f);
-  }
-
-  public String getClassName() {
-    return class_name;
-  }
-  public String getSuperclassName() {
-    return super_class_name;
-  }
-  public String getFileName() {
-    return file_name;
-  }
-
-  public void setClassName(String name) {
-    class_name = name.replace('/', '.');
-    class_name_index = cp.addClass(name);
-  }
-
-  public void setSuperclassName(String name) {
-    super_class_name = name.replace('/', '.');
-    superclass_name_index = cp.addClass(name);
-  }
-
-  public Method[] getMethods() {
-    return (Method[])method_vec.toArray(new Method[method_vec.size()]);
-  }
-
-  public void setMethods(Method[] methods) {
-    method_vec.clear();
-    for (int m = 0; m < methods.length; m++)
-      addMethod(methods[m]);
-  }
-
-  public void setMethodAt(Method method, int pos) {
-    method_vec.set(pos, method);
-  }
-
-  public Method getMethodAt(int pos) {
-    return (Method)method_vec.get(pos);
-  }
-
-  public String[] getInterfaceNames() {
-    int size = interface_vec.size();
-    String[] interfaces = new String[size];
-
-    interface_vec.toArray(interfaces);
-    return interfaces;
-  }
-
-  public int[] getInterfaces() {
-    int size = interface_vec.size();
-    int[] interfaces = new int[size];
-
-    for (int i = 0; i < size; i++)
-      interfaces[i] = cp.addClass((String)interface_vec.get(i));
-
-    return interfaces;
-  }
-
-  public Field[] getFields() {
-    return (Field[])field_vec.toArray(new Field[field_vec.size()]);
-  }
-
-  public Attribute[] getAttributes() {
-    return (Attribute[])attribute_vec.toArray(new Attribute[attribute_vec.size()]);
-  }
-
-  public ConstantPoolGen getConstantPool() {
-    return cp;
-  }
-  public void setConstantPool(ConstantPoolGen constant_pool) {
-    cp = constant_pool;
-  }
-
-  public void setClassNameIndex(int class_name_index) {
-    this.class_name_index = class_name_index;
-    class_name =
-      cp
-        .getConstantPool()
-        .getConstantString(class_name_index, Constants.CONSTANT_Class)
-        .replace('/', '.');
-  }
-
-  public void setSuperclassNameIndex(int superclass_name_index) {
-    this.superclass_name_index = superclass_name_index;
-    super_class_name =
-      cp
-        .getConstantPool()
-        .getConstantString(superclass_name_index, Constants.CONSTANT_Class)
-        .replace('/', '.');
-  }
-
-  public int getSuperclassNameIndex() {
-    return superclass_name_index;
-  }
-
-  public int getClassNameIndex() {
-    return class_name_index;
-  }
-
-  private ArrayList observers;
-
-  /** Add observer for this object.
-   */
-  public void addObserver(ClassObserver o) {
-    if (observers == null)
-      observers = new ArrayList();
-
-    observers.add(o);
-  }
-
-  /** Remove observer for this object.
-   */
-  public void removeObserver(ClassObserver o) {
-    if (observers != null)
-      observers.remove(o);
-  }
-
-  /** Call notify() method on all observers. This method is not called
-   * automatically whenever the state has changed, but has to be
-   * called by the user after he has finished editing the object.
-   */
-  public void update() {
-    if (observers != null) {
-      for (Iterator e = observers.iterator(); e.hasNext();) {
-        ((ClassObserver)e.next()).notify(this);
-      }
-    }
-  }
-
-  public Object clone() {
-    try {
-      return super.clone();
-    } catch (CloneNotSupportedException e) {
-      System.err.println(e);
-      return null;
-    }
-  }
-  
-	/**
-	 * @return Comparison strategy object
-	 */
-	public static BCELComparator getComparator() {
-		return _cmp;
-	}
-
-	/**
-	 * @param comparator Comparison strategy object
-	 */
-	public static void setComparator(BCELComparator comparator) {
-		_cmp = comparator;
-	}
-
-	/**
-	 * Return value as defined by given BCELComparator strategy.
-	 * By default two ClassGen objects are said to be equal when
-	 * their class names are equal.
-	 * 
-	 * @see java.lang.Object#equals(java.lang.Object)
-	 */
-	public boolean equals(Object obj) {
-		return _cmp.equals(this, obj);
-	}
-
-	/**
-	 * Return value as defined by given BCELComparator strategy.
-	 * By default return the hashcode of the class name.
-	 * 
-	 * @see java.lang.Object#hashCode()
-	 */
-	public int hashCode() {
-		return _cmp.hashCode(this);
-	}
+
+    /* Corresponds to the fields found in a JavaClass object.
+     */
+    private String class_name, super_class_name, file_name;
+    private int class_name_index = -1, superclass_name_index = -1;
+    private int major = Constants.MAJOR_1_1, minor = Constants.MINOR_1_1;
+    private ConstantPoolGen cp; // Template for building up constant pool
+    // ArrayLists instead of arrays to gather fields, methods, etc.
+    private List field_vec = new ArrayList();
+    private List method_vec = new ArrayList();
+    private List attribute_vec = new ArrayList();
+    private List interface_vec = new ArrayList();
+    private static BCELComparator _cmp = new BCELComparator() {
+
+        public boolean equals( Object o1, Object o2 ) {
+            ClassGen THIS = (ClassGen) o1;
+            ClassGen THAT = (ClassGen) o2;
+            return THIS.getClassName().equals(THAT.getClassName());
+        }
+
+
+        public int hashCode( Object o ) {
+            ClassGen THIS = (ClassGen) o;
+            return THIS.getClassName().hashCode();
+        }
+    };
+
+
+    /** Convenience constructor to set up some important values initially.
+     *
+     * @param class_name fully qualified class name
+     * @param super_class_name fully qualified superclass name
+     * @param file_name source file name
+     * @param access_flags access qualifiers
+     * @param interfaces implemented interfaces
+     * @param cp constant pool to use
+     */
+    public ClassGen(String class_name, String super_class_name, String file_name, int access_flags,
+            String[] interfaces, ConstantPoolGen cp) {
+        this.class_name = class_name;
+        this.super_class_name = super_class_name;
+        this.file_name = file_name;
+        this.access_flags = access_flags;
+        this.cp = cp;
+        // Put everything needed by default into the constant pool and the vectors
+        if (file_name != null) {
+            addAttribute(new SourceFile(cp.addUtf8("SourceFile"), 2, cp.addUtf8(file_name), cp
+                    .getConstantPool()));
+        }
+        class_name_index = cp.addClass(class_name);
+        superclass_name_index = cp.addClass(super_class_name);
+        if (interfaces != null) {
+            for (int i = 0; i < interfaces.length; i++) {
+                addInterface(interfaces[i]);
+            }
+        }
+    }
+
+
+    /** Convenience constructor to set up some important values initially.
+     *
+     * @param class_name fully qualified class name
+     * @param super_class_name fully qualified superclass name
+     * @param file_name source file name
+     * @param access_flags access qualifiers
+     * @param interfaces implemented interfaces
+     */
+    public ClassGen(String class_name, String super_class_name, String file_name, int access_flags,
+            String[] interfaces) {
+        this(class_name, super_class_name, file_name, access_flags, interfaces,
+                new ConstantPoolGen());
+    }
+
+
+    /**
+     * Initialize with existing class.
+     * @param clazz JavaClass object (e.g. read from file)
+     */
+    public ClassGen(JavaClass clazz) {
+        class_name_index = clazz.getClassNameIndex();
+        superclass_name_index = clazz.getSuperclassNameIndex();
+        class_name = clazz.getClassName();
+        super_class_name = clazz.getSuperclassName();
+        file_name = clazz.getSourceFileName();
+        access_flags = clazz.getAccessFlags();
+        cp = new ConstantPoolGen(clazz.getConstantPool());
+        major = clazz.getMajor();
+        minor = clazz.getMinor();
+        Attribute[] attributes = clazz.getAttributes();
+        Method[] methods = clazz.getMethods();
+        Field[] fields = clazz.getFields();
+        String[] interfaces = clazz.getInterfaceNames();
+        for (int i = 0; i < interfaces.length; i++) {
+            addInterface(interfaces[i]);
+        }
+        for (int i = 0; i < attributes.length; i++) {
+            addAttribute(attributes[i]);
+        }
+        for (int i = 0; i < methods.length; i++) {
+            addMethod(methods[i]);
+        }
+        for (int i = 0; i < fields.length; i++) {
+            addField(fields[i]);
+        }
+    }
+
+
+    /**
+     * @return the (finally) built up Java class object.
+     */
+    public JavaClass getJavaClass() {
+        int[] interfaces = getInterfaces();
+        Field[] fields = getFields();
+        Method[] methods = getMethods();
+        Attribute[] attributes = getAttributes();
+        // Must be last since the above calls may still add something to it
+        ConstantPool _cp = this.cp.getFinalConstantPool();
+        return new JavaClass(class_name_index, superclass_name_index, file_name, major, minor,
+                access_flags, _cp, interfaces, fields, methods, attributes);
+    }
+
+
+    /**
+     * Add an interface to this class, i.e., this class has to implement it.
+     * @param name interface to implement (fully qualified class name)
+     */
+    public void addInterface( String name ) {
+        interface_vec.add(name);
+    }
+
+
+    /**
+     * Remove an interface from this class.
+     * @param name interface to remove (fully qualified name)
+     */
+    public void removeInterface( String name ) {
+        interface_vec.remove(name);
+    }
+
+
+    /**
+     * @return major version number of class file
+     */
+    public int getMajor() {
+        return major;
+    }
+
+
+    /** Set major version number of class file, default value is 45 (JDK 1.1)
+     * @param major major version number
+     */
+    public void setMajor( int major ) {
+        this.major = major;
+    }
+
+
+    /** Set minor version number of class file, default value is 3 (JDK 1.1)
+     * @param minor minor version number
+     */
+    public void setMinor( int minor ) {
+        this.minor = minor;
+    }
+
+
+    /**
+     * @return minor version number of class file
+     */
+    public int getMinor() {
+        return minor;
+    }
+
+
+    /**
+     * Add an attribute to this class.
+     * @param a attribute to add
+     */
+    public void addAttribute( Attribute a ) {
+        attribute_vec.add(a);
+    }
+
+
+    /**
+     * Add a method to this class.
+     * @param m method to add
+     */
+    public void addMethod( Method m ) {
+        method_vec.add(m);
+    }
+
+
+    /**
+     * Convenience method.
+     *
+     * Add an empty constructor to this class that does nothing but calling super().
+     * @param access_flags rights for constructor
+     */
+    public void addEmptyConstructor( int access_flags ) {
+        InstructionList il = new InstructionList();
+        il.append(InstructionConstants.THIS); // Push `this'
+        il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name, "<init>", "()V")));
+        il.append(InstructionConstants.RETURN);
+        MethodGen mg = new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null, "<init>",
+                class_name, il, cp);
+        mg.setMaxStack(1);
+        addMethod(mg.getMethod());
+    }
+
+
+    /**
+     * Add a field to this class.
+     * @param f field to add
+     */
+    public void addField( Field f ) {
+        field_vec.add(f);
+    }
+
+
+    public boolean containsField( Field f ) {
+        return field_vec.contains(f);
+    }
+
+
+    /** @return field object with given name, or null
+     */
+    public Field containsField( String name ) {
+        for (Iterator e = field_vec.iterator(); e.hasNext();) {
+            Field f = (Field) e.next();
+            if (f.getName().equals(name)) {
+                return f;
+            }
+        }
+        return null;
+    }
+
+
+    /** @return method object with given name and signature, or null
+     */
+    public Method containsMethod( String name, String signature ) {
+        for (Iterator e = method_vec.iterator(); e.hasNext();) {
+            Method m = (Method) e.next();
+            if (m.getName().equals(name) && m.getSignature().equals(signature)) {
+                return m;
+            }
+        }
+        return null;
+    }
+
+
+    /**
+     * Remove an attribute from this class.
+     * @param a attribute to remove
+     */
+    public void removeAttribute( Attribute a ) {
+        attribute_vec.remove(a);
+    }
+
+
+    /**
+     * Remove a method from this class.
+     * @param m method to remove
+     */
+    public void removeMethod( Method m ) {
+        method_vec.remove(m);
+    }
+
+
+    /** Replace given method with new one. If the old one does not exist
+     * add the new_ method to the class anyway.
+     */
+    public void replaceMethod( Method old, Method new_ ) {
+        if (new_ == null) {
+            throw new ClassGenException("Replacement method must not be null");
+        }
+        int i = method_vec.indexOf(old);
+        if (i < 0) {
+            method_vec.add(new_);
+        } else {
+            method_vec.set(i, new_);
+        }
+    }
+
+
+    /** Replace given field with new one. If the old one does not exist
+     * add the new_ field to the class anyway.
+     */
+    public void replaceField( Field old, Field new_ ) {
+        if (new_ == null) {
+            throw new ClassGenException("Replacement method must not be null");
+        }
+        int i = field_vec.indexOf(old);
+        if (i < 0) {
+            field_vec.add(new_);
+        } else {
+            field_vec.set(i, new_);
+        }
+    }
+
+
+    /**
+     * Remove a field to this class.
+     * @param f field to remove
+     */
+    public void removeField( Field f ) {
+        field_vec.remove(f);
+    }
+
+
+    public String getClassName() {
+        return class_name;
+    }
+
+
+    public String getSuperclassName() {
+        return super_class_name;
+    }
+
+
+    public String getFileName() {
+        return file_name;
+    }
+
+
+    public void setClassName( String name ) {
+        class_name = name.replace('/', '.');
+        class_name_index = cp.addClass(name);
+    }
+
+
+    public void setSuperclassName( String name ) {
+        super_class_name = name.replace('/', '.');
+        superclass_name_index = cp.addClass(name);
+    }
+
+
+    public Method[] getMethods() {
+        return (Method[]) method_vec.toArray(new Method[method_vec.size()]);
+    }
+
+
+    public void setMethods( Method[] methods ) {
+        method_vec.clear();
+        for (int m = 0; m < methods.length; m++) {
+            addMethod(methods[m]);
+        }
+    }
+
+
+    public void setMethodAt( Method method, int pos ) {
+        method_vec.set(pos, method);
+    }
+
+
+    public Method getMethodAt( int pos ) {
+        return (Method) method_vec.get(pos);
+    }
+
+
+    public String[] getInterfaceNames() {
+        int size = interface_vec.size();
+        String[] interfaces = new String[size];
+        interface_vec.toArray(interfaces);
+        return interfaces;
+    }
+
+
+    public int[] getInterfaces() {
+        int size = interface_vec.size();
+        int[] interfaces = new int[size];
+        for (int i = 0; i < size; i++) {
+            interfaces[i] = cp.addClass((String) interface_vec.get(i));
+        }
+        return interfaces;
+    }
+
+
+    public Field[] getFields() {
+        return (Field[]) field_vec.toArray(new Field[field_vec.size()]);
+    }
+
+
+    public Attribute[] getAttributes() {
+        return (Attribute[]) attribute_vec.toArray(new Attribute[attribute_vec.size()]);
+    }
+
+
+    public ConstantPoolGen getConstantPool() {
+        return cp;
+    }
+
+
+    public void setConstantPool( ConstantPoolGen constant_pool ) {
+        cp = constant_pool;
+    }
+
+
+    public void setClassNameIndex( int class_name_index ) {
+        this.class_name_index = class_name_index;
+        class_name = cp.getConstantPool().getConstantString(class_name_index,
+                Constants.CONSTANT_Class).replace('/', '.');
+    }
+
+
+    public void setSuperclassNameIndex( int superclass_name_index ) {
+        this.superclass_name_index = superclass_name_index;
+        super_class_name = cp.getConstantPool().getConstantString(superclass_name_index,
+                Constants.CONSTANT_Class).replace('/', '.');
+    }
+
+
+    public int getSuperclassNameIndex() {
+        return superclass_name_index;
+    }
+
+
+    public int getClassNameIndex() {
+        return class_name_index;
+    }
+
+    private ArrayList observers;
+
+
+    /** Add observer for this object.
+     */
+    public void addObserver( ClassObserver o ) {
+        if (observers == null) {
+            observers = new ArrayList();
+        }
+        observers.add(o);
+    }
+
+
+    /** Remove observer for this object.
+     */
+    public void removeObserver( ClassObserver o ) {
+        if (observers != null) {
+            observers.remove(o);
+        }
+    }
+
+
+    /** Call notify() method on all observers. This method is not called
+     * automatically whenever the state has changed, but has to be
+     * called by the user after he has finished editing the object.
+     */
+    public void update() {
+        if (observers != null) {
+            for (Iterator e = observers.iterator(); e.hasNext();) {
+                ((ClassObserver) e.next()).notify(this);
+            }
+        }
+    }
+
+
+    public Object clone() {
+        try {
+            return super.clone();
+        } catch (CloneNotSupportedException e) {
+            System.err.println(e);
+            return null;
+        }
+    }
+
+
+    /**
+     * @return Comparison strategy object
+     */
+    public static BCELComparator getComparator() {
+        return _cmp;
+    }
+
+
+    /**
+     * @param comparator Comparison strategy object
+     */
+    public static void setComparator( BCELComparator comparator ) {
+        _cmp = comparator;
+    }
+
+
+    /**
+     * Return value as defined by given BCELComparator strategy.
+     * By default two ClassGen objects are said to be equal when
+     * their class names are equal.
+     * 
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    public boolean equals( Object obj ) {
+        return _cmp.equals(this, obj);
+    }
+
+
+    /**
+     * Return value as defined by given BCELComparator strategy.
+     * By default return the hashcode of the class name.
+     * 
+     * @see java.lang.Object#hashCode()
+     */
+    public int hashCode() {
+        return _cmp.hashCode(this);
+    }
 }

Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGenException.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGenException.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGenException.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassGenException.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License. 
  *
- */ 
+ */
 package org.apache.bcel.generic;
 
-
 /** 
  * Thrown on internal errors. Extends RuntimeException so it hasn't to be declared
  * in the throws clause every time.
@@ -25,7 +24,13 @@
  * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
  */
 public class ClassGenException extends RuntimeException {
-  public ClassGenException() { super(); }
-  public ClassGenException(String s) { super(s); }
-}
 
+    public ClassGenException() {
+        super();
+    }
+
+
+    public ClassGenException(String s) {
+        super(s);
+    }
+}

Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassObserver.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassObserver.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassObserver.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/generic/ClassObserver.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License. 
  *
- */ 
+ */
 package org.apache.bcel.generic;
 
-
 /**
  * Implement this interface if you're interested in changes to a ClassGen object
  * and register yourself with addObserver().
@@ -25,6 +24,6 @@
  * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
  */
 public interface ClassObserver {
-  public void notify(ClassGen clazz);
-}
 
+    public void notify( ClassGen clazz );
+}

Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CodeExceptionGen.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CodeExceptionGen.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CodeExceptionGen.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CodeExceptionGen.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License. 
  *
- */ 
+ */
 package org.apache.bcel.generic;
 
-
 import org.apache.bcel.classfile.CodeException;
 
 /** 
@@ -34,131 +33,152 @@
  * @see     CodeException
  * @see     InstructionHandle
  */
-public final class CodeExceptionGen
-  implements InstructionTargeter, Cloneable, java.io.Serializable {
-  private InstructionHandle start_pc;
-  private InstructionHandle end_pc;
-  private InstructionHandle handler_pc;
-  private ObjectType        catch_type;
-  
-  /**
-   * Add an exception handler, i.e., specify region where a handler is active and an
-   * instruction where the actual handling is done.
-   *
-   * @param start_pc Start of handled region (inclusive)
-   * @param end_pc End of handled region (inclusive)
-   * @param handler_pc Where handling is done
-   * @param catch_type which exception is handled, null for ANY
-   */
-  public CodeExceptionGen(InstructionHandle start_pc, InstructionHandle end_pc,
-			  InstructionHandle handler_pc, ObjectType catch_type) {
-    setStartPC(start_pc);
-    setEndPC(end_pc);
-    setHandlerPC(handler_pc);
-    this.catch_type = catch_type;
-  }
-
-  /**
-   * Get CodeException object.<BR>
-   *
-   * This relies on that the instruction list has already been dumped
-   * to byte code or or that the `setPositions' methods has been
-   * called for the instruction list.
-   *
-   * @param cp constant pool
-   */
-  public CodeException getCodeException(ConstantPoolGen cp) {
-    return new CodeException(start_pc.getPosition(),
-			     end_pc.getPosition() + end_pc.getInstruction().getLength(),
-			     handler_pc.getPosition(),
-			     (catch_type == null)? 0 : cp.addClass(catch_type));
-  }
-
-  /* Set start of handler 
-   * @param start_pc Start of handled region (inclusive)
-   */
-  public void setStartPC(InstructionHandle start_pc) {
-    BranchInstruction.notifyTarget(this.start_pc, start_pc, this);
-    this.start_pc = start_pc; 
-  }
-
-  /* Set end of handler 
-   * @param end_pc End of handled region (inclusive)
-   */
-  public void setEndPC(InstructionHandle end_pc) {
-    BranchInstruction.notifyTarget(this.end_pc, end_pc, this);
-    this.end_pc = end_pc;
-  }
-
-  /* Set handler code
-   * @param handler_pc Start of handler
-   */
-  public void setHandlerPC(InstructionHandle handler_pc) {
-    BranchInstruction.notifyTarget(this.handler_pc, handler_pc, this);
-    this.handler_pc = handler_pc;
-  }
-
-  /**
-   * @param old_ih old target, either start or end
-   * @param new_ih new target
-   */
-  public void updateTarget(InstructionHandle old_ih, InstructionHandle new_ih) {
-    boolean targeted = false;
-
-    if(start_pc == old_ih) {
-      targeted = true;
-      setStartPC(new_ih);
-    }
-
-    if(end_pc == old_ih) {
-      targeted = true;
-      setEndPC(new_ih);
-    }
-
-    if(handler_pc == old_ih) {
-      targeted = true;
-      setHandlerPC(new_ih);
-    }
-
-    if(!targeted)
-      throw new ClassGenException("Not targeting " + old_ih + ", but {" + start_pc + ", " +
-				  end_pc + ", " + handler_pc + "}");
-  }
-
-  /**
-   * @return true, if ih is target of this handler
-   */
-  public boolean containsTarget(InstructionHandle ih) {
-    return (start_pc == ih) || (end_pc == ih) || (handler_pc == ih);
-  }
-
-  /** Sets the type of the Exception to catch. Set 'null' for ANY. */
-  public void              setCatchType(ObjectType catch_type)        { this.catch_type = catch_type; }
-  /** Gets the type of the Exception to catch, 'null' for ANY. */
-  public ObjectType        getCatchType()                             { return catch_type; }
-
-  /** @return start of handled region (inclusive)
-   */
-  public InstructionHandle getStartPC()                               { return start_pc; }
-
-  /** @return end of handled region (inclusive)
-   */
-  public InstructionHandle getEndPC()                                 { return end_pc; }
-
-  /** @return start of handler
-   */
-  public InstructionHandle getHandlerPC()                             { return handler_pc; }
-
-  public String toString() {
-    return "CodeExceptionGen(" + start_pc + ", " + end_pc + ", " + handler_pc + ")";
-  }
-
-  public Object clone() {
-    try {
-      return super.clone();
-    } catch(CloneNotSupportedException e) {
-      System.err.println(e);
-      return null;
+public final class CodeExceptionGen implements InstructionTargeter, Cloneable, java.io.Serializable {
+
+    private InstructionHandle start_pc;
+    private InstructionHandle end_pc;
+    private InstructionHandle handler_pc;
+    private ObjectType catch_type;
+
+
+    /**
+     * Add an exception handler, i.e., specify region where a handler is active and an
+     * instruction where the actual handling is done.
+     *
+     * @param start_pc Start of handled region (inclusive)
+     * @param end_pc End of handled region (inclusive)
+     * @param handler_pc Where handling is done
+     * @param catch_type which exception is handled, null for ANY
+     */
+    public CodeExceptionGen(InstructionHandle start_pc, InstructionHandle end_pc,
+            InstructionHandle handler_pc, ObjectType catch_type) {
+        setStartPC(start_pc);
+        setEndPC(end_pc);
+        setHandlerPC(handler_pc);
+        this.catch_type = catch_type;
+    }
+
+
+    /**
+     * Get CodeException object.<BR>
+     *
+     * This relies on that the instruction list has already been dumped
+     * to byte code or or that the `setPositions' methods has been
+     * called for the instruction list.
+     *
+     * @param cp constant pool
+     */
+    public CodeException getCodeException( ConstantPoolGen cp ) {
+        return new CodeException(start_pc.getPosition(), end_pc.getPosition()
+                + end_pc.getInstruction().getLength(), handler_pc.getPosition(),
+                (catch_type == null) ? 0 : cp.addClass(catch_type));
+    }
+
+
+    /* Set start of handler 
+     * @param start_pc Start of handled region (inclusive)
+     */
+    public void setStartPC( InstructionHandle start_pc ) {
+        BranchInstruction.notifyTarget(this.start_pc, start_pc, this);
+        this.start_pc = start_pc;
+    }
+
+
+    /* Set end of handler 
+     * @param end_pc End of handled region (inclusive)
+     */
+    public void setEndPC( InstructionHandle end_pc ) {
+        BranchInstruction.notifyTarget(this.end_pc, end_pc, this);
+        this.end_pc = end_pc;
+    }
+
+
+    /* Set handler code
+     * @param handler_pc Start of handler
+     */
+    public void setHandlerPC( InstructionHandle handler_pc ) {
+        BranchInstruction.notifyTarget(this.handler_pc, handler_pc, this);
+        this.handler_pc = handler_pc;
+    }
+
+
+    /**
+     * @param old_ih old target, either start or end
+     * @param new_ih new target
+     */
+    public void updateTarget( InstructionHandle old_ih, InstructionHandle new_ih ) {
+        boolean targeted = false;
+        if (start_pc == old_ih) {
+            targeted = true;
+            setStartPC(new_ih);
+        }
+        if (end_pc == old_ih) {
+            targeted = true;
+            setEndPC(new_ih);
+        }
+        if (handler_pc == old_ih) {
+            targeted = true;
+            setHandlerPC(new_ih);
+        }
+        if (!targeted) {
+            throw new ClassGenException("Not targeting " + old_ih + ", but {" + start_pc + ", "
+                    + end_pc + ", " + handler_pc + "}");
+        }
+    }
+
+
+    /**
+     * @return true, if ih is target of this handler
+     */
+    public boolean containsTarget( InstructionHandle ih ) {
+        return (start_pc == ih) || (end_pc == ih) || (handler_pc == ih);
+    }
+
+
+    /** Sets the type of the Exception to catch. Set 'null' for ANY. */
+    public void setCatchType( ObjectType catch_type ) {
+        this.catch_type = catch_type;
+    }
+
+
+    /** Gets the type of the Exception to catch, 'null' for ANY. */
+    public ObjectType getCatchType() {
+        return catch_type;
+    }
+
+
+    /** @return start of handled region (inclusive)
+     */
+    public InstructionHandle getStartPC() {
+        return start_pc;
+    }
+
+
+    /** @return end of handled region (inclusive)
+     */
+    public InstructionHandle getEndPC() {
+        return end_pc;
+    }
+
+
+    /** @return start of handler
+     */
+    public InstructionHandle getHandlerPC() {
+        return handler_pc;
+    }
+
+
+    public String toString() {
+        return "CodeExceptionGen(" + start_pc + ", " + end_pc + ", " + handler_pc + ")";
+    }
+
+
+    public Object clone() {
+        try {
+            return super.clone();
+        } catch (CloneNotSupportedException e) {
+            System.err.println(e);
+            return null;
+        }
     }
-  }
 }

Modified: jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CompoundInstruction.java
URL: http://svn.apache.org/viewcvs/jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CompoundInstruction.java?rev=386056&r1=386055&r2=386056&view=diff
==============================================================================
--- jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CompoundInstruction.java (original)
+++ jakarta/bcel/trunk/src/java/org/apache/bcel/generic/CompoundInstruction.java Wed Mar 15 03:31:56 2006
@@ -13,10 +13,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License. 
  *
- */ 
+ */
 package org.apache.bcel.generic;
 
-
 /**
  * Wrapper class for `compound' operations, virtual instructions that
  * don't exist as byte code, but give a useful meaning. For example,
@@ -34,6 +33,6 @@
  * @see SWITCH
  */
 public interface CompoundInstruction {
-  public InstructionList getInstructionList();
-}
 
+    public InstructionList getInstructionList();
+}



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