# Class ClassGen

java.lang.Object
org.apache.bcel.classfile.AccessFlags
org.apache.bcel.generic.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:
  - `JavaClass`

- ## Field Summary

  ### Fields inherited from class AccessFlags

  `access_flags`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ClassGen(String className, String superClassName, String fileName, int accessFlags, String[] interfaces)`
  Convenience constructor to set up some important values initially.
  `ClassGen(String className, String superClassName, String fileName, int accessFlags, String[] interfaces, ConstantPoolGen cp)`
  Convenience constructor to set up some important values initially.
  `ClassGen(JavaClass clazz)`
  Constructs a new instance from an existing class.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addAnnotationEntry(AnnotationEntryGen a)`
  Adds an annotation entry to this class.
  `void`
  `addAttribute(Attribute a)`
  Add an attribute to this class.
  `void`
  `addEmptyConstructor(int accessFlags)`
  Convenience method.
  `void`
  `addField(Field f)`
  Add a field to this class.
  `void`
  `addInterface(String name)`
  Add an interface to this class, that is, this class has to implement it.
  `void`
  `addMethod(Method m)`
  Add a method to this class.
  `void`
  `addObserver(ClassObserver o)`
  Add observer for this object.
  `Object`
  `clone()`
  `Field`
  `containsField(String name)`
  Gets the field object with given name, or null.
  `boolean`
  `containsField(Field f)`
  Checks if this class contains the given field.
  `Method`
  `containsMethod(String name, String signature)`
  Gets the method object with given name and signature, or null.
  `boolean`
  `equals(Object obj)`
  Return value as defined by given BCELComparator strategy.
  `AnnotationEntryGen[]`
  `getAnnotationEntries()`
  Gets the annotation entries.
  `Attribute[]`
  `getAttributes()`
  Gets the attributes.
  `String`
  `getClassName()`
  Gets the class name.
  `int`
  `getClassNameIndex()`
  Gets the class name index.
  `static BCELComparator<ClassGen>`
  `getComparator()`
  Gets the comparison strategy object.
  `ConstantPoolGen`
  `getConstantPool()`
  Gets the constant pool.
  `Field[]`
  `getFields()`
  Gets the fields.
  `String`
  `getFileName()`
  Gets the file name.
  `String[]`
  `getInterfaceNames()`
  Gets the interface names.
  `int[]`
  `getInterfaces()`
  Gets the interfaces.
  `JavaClass`
  `getJavaClass()`
  Gets the (finally) built up Java class object.
  `int`
  `getMajor()`
  Gets major version number of class file.
  `Method`
  `getMethodAt(int pos)`
  Gets the method at the given position.
  `Method[]`
  `getMethods()`
  Gets the methods.
  `int`
  `getMinor()`
  Gets minor version number of class file.
  `String`
  `getSuperclassName()`
  Gets the superclass name.
  `int`
  `getSuperclassNameIndex()`
  Gets the superclass name index.
  `int`
  `hashCode()`
  Return value as defined by given BCELComparator strategy.
  `void`
  `removeAttribute(Attribute a)`
  Remove an attribute from this class.
  `void`
  `removeField(Field f)`
  Remove a field to this class.
  `void`
  `removeInterface(String name)`
  Remove an interface from this class.
  `void`
  `removeMethod(Method m)`
  Remove a method from this class.
  `void`
  `removeObserver(ClassObserver o)`
  Remove observer for this object.
  `void`
  `replaceField(Field old, Field newField)`
  Replace given field with new one.
  `void`
  `replaceMethod(Method old, Method newMethod)`
  Replace given method with new one.
  `void`
  `setClassName(String name)`
  Sets the class name.
  `void`
  `setClassNameIndex(int classNameIndex)`
  Sets the class name index.
  `static void`
  `setComparator(BCELComparator<ClassGen> comparator)`
  Sets the comparison strategy object.
  `void`
  `setConstantPool(ConstantPoolGen constantPool)`
  Sets the constant pool.
  `void`
  `setMajor(int major)`
  Sets major version number of class file, default value is 45 (JDK 1.1).
  `void`
  `setMethodAt(Method method, int pos)`
  Sets the method at the given position.
  `void`
  `setMethods(Method[] methods)`
  Sets the methods.
  `void`
  `setMinor(int minor)`
  Sets minor version number of class file, default value is 3 (JDK 1.1).
  `void`
  `setSuperclassName(String name)`
  Sets the superclass name.
  `void`
  `setSuperclassNameIndex(int superclassNameIndex)`
  Sets the superclass name index.
  `void`
  `update()`
  Call notify() method on all observers.

  ### Methods inherited from class AccessFlags

  `getAccessFlags, getModifiers, isAbstract, isAbstract, isAnnotation, isAnnotation, isEnum, isEnum, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isStrictfp, isStrictfp, isSynchronized, isSynchronized, isSynthetic, isSynthetic, isTransient, isTransient, isVarArgs, isVarArgs, isVolatile, isVolatile, setAccessFlags, setModifiers`

  ### Methods inherited from class Object

  `finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

- ## 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

    public static BCELComparator<ClassGen> 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

    public void addAnnotationEntry(AnnotationEntryGen a)
    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:
      - `Object.equals(Object)`

  - ### getAnnotationEntries

    public AnnotationEntryGen[] getAnnotationEntries()
    Gets the annotation entries.
    Returns:
      the annotation entries.

  - ### getAttributes

    public Attribute[] 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

    public ConstantPoolGen 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

    public String[] getInterfaceNames()
    Gets the interface names.
    Returns:
      the interface names.

  - ### getInterfaces

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

  - ### getJavaClass

    public JavaClass 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

    public String 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:
      - `Object.hashCode()`

  - ### 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.