Package org.apache.bcel.generic
Class ClassGen
- java.lang.Object
-
- org.apache.bcel.classfile.AccessFlags
-
- org.apache.bcel.generic.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 Summary
Fields Modifier and Type Field Description private java.util.List<AnnotationEntryGen>annotationListprivate java.util.List<Attribute>attributeListprivate static BCELComparator<ClassGen>bcelComparatorprivate java.lang.StringclassNameprivate intclassNameIndexprivate ConstantPoolGencpprivate java.util.List<Field>fieldListprivate java.lang.StringfileNameprivate java.util.List<java.lang.String>interfaceListprivate intmajorprivate java.util.List<Method>methodListprivate intminorprivate java.util.List<ClassObserver>observersprivate java.lang.StringsuperClassNameprivate intsuperclassNameIndex-
Fields inherited from class org.apache.bcel.classfile.AccessFlags
access_flags
-
-
Constructor Summary
Constructors Constructor Description 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.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.ClassGen(JavaClass clazz)Constructs a new instance from an existing class.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAnnotationEntry(AnnotationEntryGen a)Adds an annotation entry to this class.voidaddAttribute(Attribute a)Add an attribute to this class.voidaddEmptyConstructor(int accessFlags)Convenience method.voidaddField(Field f)Add a field to this class.voidaddInterface(java.lang.String name)Add an interface to this class, that is, this class has to implement it.voidaddMethod(Method m)Add a method to this class.voidaddObserver(ClassObserver o)Add observer for this object.java.lang.Objectclone()FieldcontainsField(java.lang.String name)Gets the field object with given name, or null.booleancontainsField(Field f)Checks if this class contains the given field.MethodcontainsMethod(java.lang.String name, java.lang.String signature)Gets the method object with given name and signature, or null.booleanequals(java.lang.Object obj)Return value as defined by given BCELComparator strategy.AnnotationEntryGen[]getAnnotationEntries()Gets the annotation entries.Attribute[]getAttributes()Gets the attributes.java.lang.StringgetClassName()Gets the class name.intgetClassNameIndex()Gets the class name index.static BCELComparator<ClassGen>getComparator()Gets the comparison strategy object.ConstantPoolGengetConstantPool()Gets the constant pool.Field[]getFields()Gets the fields.java.lang.StringgetFileName()Gets the file name.java.lang.String[]getInterfaceNames()Gets the interface names.int[]getInterfaces()Gets the interfaces.JavaClassgetJavaClass()Gets the (finally) built up Java class object.intgetMajor()Gets major version number of class file.MethodgetMethodAt(int pos)Gets the method at the given position.Method[]getMethods()Gets the methods.intgetMinor()Gets minor version number of class file.java.lang.StringgetSuperclassName()Gets the superclass name.intgetSuperclassNameIndex()Gets the superclass name index.inthashCode()Return value as defined by given BCELComparator strategy.voidremoveAttribute(Attribute a)Remove an attribute from this class.voidremoveField(Field f)Remove a field to this class.voidremoveInterface(java.lang.String name)Remove an interface from this class.voidremoveMethod(Method m)Remove a method from this class.voidremoveObserver(ClassObserver o)Remove observer for this object.voidreplaceField(Field old, Field newField)Replace given field with new one.voidreplaceMethod(Method old, Method newMethod)Replace given method with new one.voidsetClassName(java.lang.String name)Sets the class name.voidsetClassNameIndex(int classNameIndex)Sets the class name index.static voidsetComparator(BCELComparator<ClassGen> comparator)Sets the comparison strategy object.voidsetConstantPool(ConstantPoolGen constantPool)Sets the constant pool.voidsetMajor(int major)Sets major version number of class file, default value is 45 (JDK 1.1).voidsetMethodAt(Method method, int pos)Sets the method at the given position.voidsetMethods(Method[] methods)Sets the methods.voidsetMinor(int minor)Sets minor version number of class file, default value is 3 (JDK 1.1).voidsetSuperclassName(java.lang.String name)Sets the superclass name.voidsetSuperclassNameIndex(int superclassNameIndex)Sets the superclass name index.private AnnotationEntryGen[]unpackAnnotations(Attribute[] attributes)Unpacks attributes representing annotations.voidupdate()Call notify() method on all observers.-
Methods inherited from class org.apache.bcel.classfile.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
-
-
-
-
Field Detail
-
bcelComparator
private static BCELComparator<ClassGen> bcelComparator
-
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
-
cp
private ConstantPoolGen cp
-
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
-
annotationList
private final java.util.List<AnnotationEntryGen> annotationList
-
observers
private java.util.List<ClassObserver> observers
-
-
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:
clonein classjava.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:
equalsin classjava.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:
hashCodein classjava.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.
-
-