Class ClassGen

All Implemented Interfaces:
Cloneable

public class ClassGen extends AccessFlags implements Cloneable
Template class for building up a java class. May be initialized with an existing Java class (file).
See Also:
  • Constructor Details

    • ClassGen

      public ClassGen(JavaClass clazz)
      Constructs a new instance from an existing class.
      Parameters:
      clazz - JavaClass object (for example read from file).
    • ClassGen

      public ClassGen(String className, String superClassName, String fileName, int accessFlags, String[] interfaces)
      Convenience constructor to set up some important values initially.
      Parameters:
      className - fully qualified class name.
      superClassName - fully qualified superclass name.
      fileName - source file name.
      accessFlags - access qualifiers.
      interfaces - implemented interfaces.
    • ClassGen

      public ClassGen(String className, String superClassName, String fileName, int accessFlags, String[] interfaces, ConstantPoolGen cp)
      Convenience constructor to set up some important values initially.
      Parameters:
      className - fully qualified class name.
      superClassName - fully qualified superclass name.
      fileName - source file name.
      accessFlags - access qualifiers.
      interfaces - implemented interfaces.
      cp - constant pool to use.
  • Method Details

    • getComparator

      Gets the comparison strategy object.
      Returns:
      Comparison strategy object.
    • setComparator

      public static void setComparator(BCELComparator<ClassGen> comparator)
      Sets the comparison strategy object.
      Parameters:
      comparator - Comparison strategy object.
    • addAnnotationEntry

      Adds an annotation entry to this class.
      Parameters:
      a - the annotation entry to add.
    • addAttribute

      public void addAttribute(Attribute a)
      Add an attribute to this class.
      Parameters:
      a - attribute to add.
    • addEmptyConstructor

      public void addEmptyConstructor(int accessFlags)
      Convenience method. Add an empty constructor to this class that does nothing but calling super().
      Parameters:
      accessFlags - rights for constructor.
    • addField

      public void addField(Field f)
      Add a field to this class.
      Parameters:
      f - field to add.
    • addInterface

      public void addInterface(String name)
      Add an interface to this class, that is, this class has to implement it.
      Parameters:
      name - interface to implement (fully qualified class name).
    • addMethod

      public void addMethod(Method m)
      Add a method to this class.
      Parameters:
      m - method to add.
    • addObserver

      public void addObserver(ClassObserver o)
      Add observer for this object.
      Parameters:
      o - the observer.
    • clone

      public Object clone()
      Overrides:
      clone in class Object
    • containsField

      public boolean containsField(Field f)
      Checks if this class contains the given field.
      Parameters:
      f - the field to check.
      Returns:
      true if this class contains the field.
    • containsField

      public Field containsField(String name)
      Gets the field object with given name, or null.
      Parameters:
      name - the field name.
      Returns:
      field object with given name, or null.
    • containsMethod

      public Method containsMethod(String name, String signature)
      Gets the method object with given name and signature, or null.
      Parameters:
      name - the method name.
      signature - the method signature.
      Returns:
      method object with given name and signature, or null.
    • equals

      public boolean equals(Object obj)
      Return value as defined by given BCELComparator strategy. By default two ClassGen objects are said to be equal when their class names are equal.
      Overrides:
      equals in class Object
      See Also:
    • getAnnotationEntries

      Gets the annotation entries.
      Returns:
      the annotation entries.
    • getAttributes

      Gets the attributes.
      Returns:
      the attributes.
    • getClassName

      public String getClassName()
      Gets the class name.
      Returns:
      the class name.
    • getClassNameIndex

      public int getClassNameIndex()
      Gets the class name index.
      Returns:
      the class name index.
    • getConstantPool

      Gets the constant pool.
      Returns:
      the constant pool.
    • getFields

      public Field[] getFields()
      Gets the fields.
      Returns:
      the fields.
    • getFileName

      public String getFileName()
      Gets the file name.
      Returns:
      the file name.
    • getInterfaceNames

      Gets the interface names.
      Returns:
      the interface names.
    • getInterfaces

      public int[] getInterfaces()
      Gets the interfaces.
      Returns:
      the interfaces.
    • getJavaClass

      Gets the (finally) built up Java class object.
      Returns:
      the (finally) built up Java class object.
    • getMajor

      public int getMajor()
      Gets major version number of class file.
      Returns:
      major version number of class file.
    • getMethodAt

      public Method getMethodAt(int pos)
      Gets the method at the given position.
      Parameters:
      pos - the position.
      Returns:
      the method at the given position.
    • getMethods

      public Method[] getMethods()
      Gets the methods.
      Returns:
      the methods.
    • getMinor

      public int getMinor()
      Gets minor version number of class file.
      Returns:
      minor version number of class file.
    • getSuperclassName

      Gets the superclass name.
      Returns:
      the superclass name.
    • getSuperclassNameIndex

      public int getSuperclassNameIndex()
      Gets the superclass name index.
      Returns:
      the superclass name index.
    • hashCode

      public int hashCode()
      Return value as defined by given BCELComparator strategy. By default return the hash code of the class name.
      Overrides:
      hashCode in class Object
      See Also:
    • removeAttribute

      public void removeAttribute(Attribute a)
      Remove an attribute from this class.
      Parameters:
      a - attribute to remove.
    • removeField

      public void removeField(Field f)
      Remove a field to this class.
      Parameters:
      f - field to remove.
    • removeInterface

      public void removeInterface(String name)
      Remove an interface from this class.
      Parameters:
      name - interface to remove (fully qualified name).
    • removeMethod

      public void removeMethod(Method m)
      Remove a method from this class.
      Parameters:
      m - method to remove.
    • removeObserver

      public void removeObserver(ClassObserver o)
      Remove observer for this object.
      Parameters:
      o - the observer to remove.
    • replaceField

      public void replaceField(Field old, Field newField)
      Replace given field with new one. If the old one does not exist add the new_ field to the class anyway.
      Parameters:
      old - the old field.
      newField - the new field.
    • replaceMethod

      public void replaceMethod(Method old, Method newMethod)
      Replace given method with new one. If the old one does not exist add the newMethod method to the class anyway.
      Parameters:
      old - the old method.
      newMethod - the new method.
    • setClassName

      public void setClassName(String name)
      Sets the class name.
      Parameters:
      name - the class name.
    • setClassNameIndex

      public void setClassNameIndex(int classNameIndex)
      Sets the class name index.
      Parameters:
      classNameIndex - the class name index.
    • setConstantPool

      public void setConstantPool(ConstantPoolGen constantPool)
      Sets the constant pool.
      Parameters:
      constantPool - the constant pool.
    • setMajor

      public void setMajor(int major)
      Sets major version number of class file, default value is 45 (JDK 1.1).
      Parameters:
      major - major version number.
    • setMethodAt

      public void setMethodAt(Method method, int pos)
      Sets the method at the given position.
      Parameters:
      method - the method.
      pos - the position.
    • setMethods

      public void setMethods(Method[] methods)
      Sets the methods.
      Parameters:
      methods - the methods.
    • setMinor

      public void setMinor(int minor)
      Sets minor version number of class file, default value is 3 (JDK 1.1).
      Parameters:
      minor - minor version number.
    • setSuperclassName

      public void setSuperclassName(String name)
      Sets the superclass name.
      Parameters:
      name - the superclass name.
    • setSuperclassNameIndex

      public void setSuperclassNameIndex(int superclassNameIndex)
      Sets the superclass name index.
      Parameters:
      superclassNameIndex - the superclass name index.
    • update

      public void update()
      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 they have finished editing the object.