org.apache.bcel.generic

Class ClassGen

Implemented Interfaces:
Cloneable, java.io.Serializable

public class ClassGen
extends AccessFlags
implements Cloneable

Template class for building up a java class. May be initialized with an existing java class (file).
Version:
$Id: ClassGen.java,v 1.3 2002/03/04 13:36:57 mdahm Exp $
Author:
M. Dahm
See Also:
JavaClass, Serialized Form

Field Summary

Fields inherited from class org.apache.bcel.classfile.AccessFlags

access_flags

Constructor Summary

ClassGen(String class_name, String super_class_name, String file_name, int access_flags, String[] interfaces)
Convenience constructor to set up some important values initially.
ClassGen(String class_name, String super_class_name, String file_name, int access_flags, String[] interfaces, ConstantPoolGen cp)
Convenience constructor to set up some important values initially.
ClassGen(JavaClass clazz)
Initialize with existing class.

Method Summary

void
addAttribute(Attribute a)
Add an attribute to this class.
void
addEmptyConstructor(int access_flags)
Convenience method.
void
addField(Field f)
Add a field to this class.
void
addInterface(String name)
Add an interface to this class, i.e., 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)
boolean
containsField(Field f)
Method
containsMethod(String name, String signature)
Attribute[]
getAttributes()
String
getClassName()
int
getClassNameIndex()
ConstantPoolGen
getConstantPool()
Field[]
getFields()
String
getFileName()
String[]
getInterfaceNames()
int[]
getInterfaces()
JavaClass
getJavaClass()
int
getMajor()
Method
getMethodAt(int pos)
Method[]
getMethods()
int
getMinor()
String
getSuperclassName()
int
getSuperclassNameIndex()
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 new_)
Replace given field with new one.
void
replaceMethod(Method old, Method new_)
Replace given method with new one.
void
setClassName(String name)
void
setClassNameIndex(int class_name_index)
void
setConstantPool(ConstantPoolGen constant_pool)
void
setMajor(int major)
Set major version number of class file, default value is 45 (JDK 1.1)
void
setMethodAt(Method method, int pos)
void
setMethods(Method[] methods)
void
setMinor(int minor)
Set minor version number of class file, default value is 3 (JDK 1.1)
void
setSuperclassName(String name)
void
setSuperclassNameIndex(int superclass_name_index)
void
update()
Call notify() method on all observers.

Methods inherited from class org.apache.bcel.classfile.AccessFlags

getAccessFlags, getModifiers, isAbstract, isAbstract, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isStrictfp, isStrictfp, isSynchronized, isSynchronized, isTransient, isTransient, isVolatile, isVolatile, setAccessFlags, setModifiers

Constructor Details

ClassGen

public ClassGen(String class_name,
                String super_class_name,
                String file_name,
                int access_flags,
                String[] interfaces)
Convenience constructor to set up some important values initially.
Parameters:
class_name - fully qualified class name
super_class_name - fully qualified superclass name
file_name - source file name
access_flags - access qualifiers
interfaces - implemented interfaces

ClassGen

public ClassGen(String class_name,
                String super_class_name,
                String file_name,
                int access_flags,
                String[] interfaces,
                ConstantPoolGen cp)
Convenience constructor to set up some important values initially.
Parameters:
class_name - fully qualified class name
super_class_name - fully qualified superclass name
file_name - source file name
access_flags - access qualifiers
interfaces - implemented interfaces
cp - constant pool to use

ClassGen

public ClassGen(JavaClass clazz)
Initialize with existing class.
Parameters:
clazz - JavaClass object (e.g. read from file)

Method Details

addAttribute

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

addEmptyConstructor

public void addEmptyConstructor(int access_flags)
Convenience method. Add an empty constructor to this class that does nothing but calling super().
Parameters:

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()

containsField

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

containsField

public boolean containsField(Field f)

containsMethod

public Method containsMethod(String name,
                             String signature)
Returns:
method object with given name and signature, or null

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()

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 new_)
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 new_)
Replace given method with new one. If the old one does not exist add the new_ method to the class anyway.

setClassName

public void setClassName(String name)

setClassNameIndex

public void setClassNameIndex(int class_name_index)

setConstantPool

public void setConstantPool(ConstantPoolGen constant_pool)

setMajor

public void setMajor(int major)
Set 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)
Set 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 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 he has finished editing the object.