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:
  • Field Details

    • bcelComparator

      private static BCELComparator<ClassGen> bcelComparator
    • className

      private String className
    • superClassName

      private String superClassName
    • fileName

      private final String fileName
    • classNameIndex

      private int classNameIndex
    • superclassNameIndex

      private int superclassNameIndex
    • major

      private int major
    • minor

      private int minor
    • cp

      private ConstantPoolGen cp
    • fieldList

      private final List<Field> fieldList
    • methodList

      private final List<Method> methodList
    • attributeList

      private final List<Attribute> attributeList
    • interfaceList

      private final List<String> interfaceList
    • annotationList

      private final List<AnnotationEntryGen> annotationList
    • observers

      private List<ClassObserver> observers
  • Constructor Details

    • ClassGen

      public ClassGen(JavaClass clazz)
      Initialize with existing class.
      Parameters:
      clazz - JavaClass object (e.g. 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

      public static BCELComparator<ClassGen> getComparator()
      Returns:
      Comparison strategy object
    • setComparator

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

      public void addAnnotationEntry(AnnotationEntryGen a)
    • 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, i.e., 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.
    • clone

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

      public boolean containsField(Field f)
    • containsField

      public Field containsField(String name)
      Returns:
      field object with given name, or null
    • containsMethod

      public Method containsMethod(String name, String 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

      public AnnotationEntryGen[] getAnnotationEntries()
    • getAttributes

      public Attribute[] getAttributes()
    • getClassName

      public String getClassName()
    • getClassNameIndex

      public int getClassNameIndex()
    • getConstantPool

      public ConstantPoolGen getConstantPool()
    • getFields

      public Field[] getFields()
    • getFileName

      public String getFileName()
    • getInterfaceNames

      public String[] getInterfaceNames()
    • getInterfaces

      public int[] getInterfaces()
    • getJavaClass

      public JavaClass getJavaClass()
      Returns:
      the (finally) built up Java class object.
    • getMajor

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

      public Method getMethodAt(int pos)
    • getMethods

      public Method[] getMethods()
    • getMinor

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

      public String getSuperclassName()
    • getSuperclassNameIndex

      public int getSuperclassNameIndex()
    • 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.
    • 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.
    • 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.
    • setClassName

      public void setClassName(String name)
    • setClassNameIndex

      public void setClassNameIndex(int classNameIndex)
    • setConstantPool

      public void setConstantPool(ConstantPoolGen constantPool)
    • 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)
    • setMethods

      public void setMethods(Method[] 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)
    • setSuperclassNameIndex

      public void setSuperclassNameIndex(int superclassNameIndex)
    • unpackAnnotations

      private AnnotationEntryGen[] unpackAnnotations(Attribute[] attrs)
      Look for attributes representing annotations and unpack them.
    • 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.