Class ClassGen

  • All Implemented Interfaces:
    java.lang.Cloneable

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

      • className

        private java.lang.String className
      • superClassName

        private java.lang.String superClassName
      • fileName

        private final java.lang.String fileName
      • classNameIndex

        private int classNameIndex
      • superclassNameIndex

        private int superclassNameIndex
      • major

        private int major
      • minor

        private int minor
      • fieldList

        private final java.util.List<Field> fieldList
      • methodList

        private final java.util.List<Method> methodList
      • attributeList

        private final java.util.List<Attribute> attributeList
      • interfaceList

        private final java.util.List<java.lang.String> interfaceList
    • Constructor Detail

      • 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​(java.lang.String className,
                        java.lang.String superClassName,
                        java.lang.String fileName,
                        int accessFlags,
                        java.lang.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​(java.lang.String className,
                        java.lang.String superClassName,
                        java.lang.String fileName,
                        int accessFlags,
                        java.lang.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 Detail

      • 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​(java.lang.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 java.lang.Object clone()
        Overrides:
        clone in class java.lang.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​(java.lang.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​(java.lang.String name,
                                     java.lang.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​(java.lang.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 java.lang.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 java.lang.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 java.lang.String getFileName()
        Gets the file name.
        Returns:
        the file name.
      • getInterfaceNames

        public java.lang.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 java.lang.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 java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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.
      • unpackAnnotations

        private AnnotationEntryGen[] unpackAnnotations​(Attribute[] attributes)
        Unpacks attributes representing annotations.
      • 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.