FileDocCategorySizeDatePackage
ClassGen.javaAPI DocJava SE 6 API14675Tue Jun 10 00:22:18 BST 2008com.sun.org.apache.bcel.internal.generic

ClassGen

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
JavaClass
version
$Id: ClassGen.java,v 1.1.2.1 2005/07/31 23:45:37 jeffsuttor Exp $
author
M. Dahm

Fields Summary
private String
class_name
private String
super_class_name
private String
file_name
private int
class_name_index
private int
superclass_name_index
private int
major
private int
minor
private ConstantPoolGen
cp
private ArrayList
field_vec
private ArrayList
method_vec
private ArrayList
attribute_vec
private ArrayList
interface_vec
private ArrayList
observers
Constructors Summary
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.

param
class_name fully qualified class name
param
super_class_name fully qualified superclass name
param
file_name source file name
param
access_flags access qualifiers
param
interfaces implemented interfaces
param
cp constant pool to use


                                             
        
		        
    this.class_name       = class_name;
    this.super_class_name = super_class_name;
    this.file_name        = file_name;
    this.access_flags     = access_flags;
    this.cp               = cp;

    // Put everything needed by default into the constant pool and the vectors
    if(file_name != null)
      addAttribute(new SourceFile(cp.addUtf8("SourceFile"), 2,
				  cp.addUtf8(file_name), cp.getConstantPool()));

    class_name_index      = cp.addClass(class_name);
    superclass_name_index = cp.addClass(super_class_name);

    if(interfaces != null)
      for(int i=0; i < interfaces.length; i++)
	addInterface(interfaces[i]);
  
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.

param
class_name fully qualified class name
param
super_class_name fully qualified superclass name
param
file_name source file name
param
access_flags access qualifiers
param
interfaces implemented interfaces

    this(class_name, super_class_name, file_name, access_flags, interfaces,
	 new ConstantPoolGen());
  
public ClassGen(JavaClass clazz)
Initialize with existing class.

param
clazz JavaClass object (e.g. read from file)

    class_name_index      = clazz.getClassNameIndex();
    superclass_name_index = clazz.getSuperclassNameIndex();
    class_name            = clazz.getClassName();
    super_class_name      = clazz.getSuperclassName();
    file_name             = clazz.getSourceFileName();
    access_flags          = clazz.getAccessFlags();
    cp                    = new ConstantPoolGen(clazz.getConstantPool());
    major                 = clazz.getMajor();
    minor                 = clazz.getMinor();

    Attribute[] attributes = clazz.getAttributes();
    Method[]    methods    = clazz.getMethods();
    Field[]     fields     = clazz.getFields();
    String[]    interfaces = clazz.getInterfaceNames();
    
    for(int i=0; i < interfaces.length; i++)
      addInterface(interfaces[i]);

    for(int i=0; i < attributes.length; i++)
      addAttribute(attributes[i]);

    for(int i=0; i < methods.length; i++)
      addMethod(methods[i]);

    for(int i=0; i < fields.length; i++)
      addField(fields[i]);
  
Methods Summary
public voidaddAttribute(com.sun.org.apache.bcel.internal.classfile.Attribute a)
Add an attribute to this class.

param
a attribute to add

 attribute_vec.add(a); 
public voidaddEmptyConstructor(int access_flags)
Convenience method. Add an empty constructor to this class that does nothing but calling super().

param
access rights for constructor

    InstructionList il = new InstructionList();
    il.append(InstructionConstants.THIS); // Push `this'
    il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name,
						"<init>", "()V")));
    il.append(InstructionConstants.RETURN);

    MethodGen mg = new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null,
		       "<init>", class_name, il, cp);
    mg.setMaxStack(1);
    addMethod(mg.getMethod());
  
public voidaddField(com.sun.org.apache.bcel.internal.classfile.Field f)
Add a field to this class.

param
f field to add

 field_vec.add(f); 
public voidaddInterface(java.lang.String name)
Add an interface to this class, i.e., this class has to implement it.

param
name interface to implement (fully qualified class name)

    interface_vec.add(name);
  
public voidaddMethod(com.sun.org.apache.bcel.internal.classfile.Method m)
Add a method to this class.

param
m method to add

 method_vec.add(m); 
public voidaddObserver(com.sun.org.apache.bcel.internal.generic.ClassObserver o)
Add observer for this object.

    if(observers == null)
      observers = new ArrayList();

    observers.add(o);
  
public java.lang.Objectclone()

    try {
      return super.clone();
    } catch(CloneNotSupportedException e) {
      System.err.println(e);
      return null;
    }
  
public booleancontainsField(com.sun.org.apache.bcel.internal.classfile.Field f)

 return field_vec.contains(f); 
public com.sun.org.apache.bcel.internal.classfile.FieldcontainsField(java.lang.String name)

return
field object with given name, or null

    for(Iterator e=field_vec.iterator(); e.hasNext(); ) {
      Field f = (Field)e.next();
      if(f.getName().equals(name))
	return f;
    }

    return null;
  
public com.sun.org.apache.bcel.internal.classfile.MethodcontainsMethod(java.lang.String name, java.lang.String signature)

return
method object with given name and signature, or null

    for(Iterator e=method_vec.iterator(); e.hasNext();) {
      Method m = (Method)e.next();
      if(m.getName().equals(name) && m.getSignature().equals(signature))
	return m;
    }

    return null;
  
public com.sun.org.apache.bcel.internal.classfile.Attribute[]getAttributes()

    Attribute[] attributes = new Attribute[attribute_vec.size()];
    attribute_vec.toArray(attributes);
    return attributes;
  
public java.lang.StringgetClassName()

 return class_name; 
public intgetClassNameIndex()

 return class_name_index; 
public com.sun.org.apache.bcel.internal.generic.ConstantPoolGengetConstantPool()

 return cp; 
public com.sun.org.apache.bcel.internal.classfile.Field[]getFields()

    Field[] fields = new Field[field_vec.size()];
    field_vec.toArray(fields);
    return fields;
  
public java.lang.StringgetFileName()

 return file_name; 
public java.lang.String[]getInterfaceNames()

    int      size = interface_vec.size();
    String[] interfaces = new String[size];

    interface_vec.toArray(interfaces);
    return interfaces;
  
public int[]getInterfaces()

    int   size = interface_vec.size();
    int[] interfaces = new int[size];

    for(int i=0; i < size; i++)
      interfaces[i] = cp.addClass((String)interface_vec.get(i));

    return interfaces;
  
public com.sun.org.apache.bcel.internal.classfile.JavaClassgetJavaClass()

return
the (finally) built up Java class object.

    int[]        interfaces = getInterfaces();
    Field[]      fields     = getFields();
    Method[]     methods    = getMethods();
    Attribute[]  attributes = getAttributes();

    // Must be last since the above calls may still add something to it
    ConstantPool cp         = this.cp.getFinalConstantPool();
    
    return new JavaClass(class_name_index, superclass_name_index,
			 file_name, major, minor, access_flags,
			 cp, interfaces, fields, methods, attributes);
  
public intgetMajor()

return
major version number of class file

 return major; 
public com.sun.org.apache.bcel.internal.classfile.MethodgetMethodAt(int pos)

    return (Method)method_vec.get(pos);
  
public com.sun.org.apache.bcel.internal.classfile.Method[]getMethods()

    Method[] methods = new Method[method_vec.size()];
    method_vec.toArray(methods);
    return methods;
  
public intgetMinor()

return
minor version number of class file

 return minor; 
public java.lang.StringgetSuperclassName()

 return super_class_name; 
public intgetSuperclassNameIndex()

 return superclass_name_index; 
public voidremoveAttribute(com.sun.org.apache.bcel.internal.classfile.Attribute a)
Remove an attribute from this class.

param
a attribute to remove

 attribute_vec.remove(a); 
public voidremoveField(com.sun.org.apache.bcel.internal.classfile.Field f)
Remove a field to this class.

param
f field to remove

 field_vec.remove(f); 
public voidremoveInterface(java.lang.String name)
Remove an interface from this class.

param
name interface to remove (fully qualified name)

    interface_vec.remove(name);
  
public voidremoveMethod(com.sun.org.apache.bcel.internal.classfile.Method m)
Remove a method from this class.

param
m method to remove

 method_vec.remove(m); 
public voidremoveObserver(com.sun.org.apache.bcel.internal.generic.ClassObserver o)
Remove observer for this object.

    if(observers != null)
      observers.remove(o);
  
public voidreplaceField(com.sun.org.apache.bcel.internal.classfile.Field old, com.sun.org.apache.bcel.internal.classfile.Field new_)
Replace given field with new one. If the old one does not exist add the new_ field to the class anyway.

    if(new_ == null)
      throw new ClassGenException("Replacement method must not be null");

    int i = field_vec.indexOf(old);

    if(i < 0)
      field_vec.add(new_);
    else
      field_vec.set(i, new_);
  
public voidreplaceMethod(com.sun.org.apache.bcel.internal.classfile.Method old, com.sun.org.apache.bcel.internal.classfile.Method new_)
Replace given method with new one. If the old one does not exist add the new_ method to the class anyway.

    if(new_ == null)
      throw new ClassGenException("Replacement method must not be null");

    int i = method_vec.indexOf(old);

    if(i < 0)
      method_vec.add(new_);
    else
      method_vec.set(i, new_);
  
public voidsetClassName(java.lang.String name)

    class_name = name.replace('/", '.");
    class_name_index = cp.addClass(name);
  
public voidsetClassNameIndex(int class_name_index)

    this.class_name_index = class_name_index;
    class_name = cp.getConstantPool().
      getConstantString(class_name_index, Constants.CONSTANT_Class).replace('/", '.");
  
public voidsetConstantPool(com.sun.org.apache.bcel.internal.generic.ConstantPoolGen constant_pool)

    cp = constant_pool;
  
public voidsetMajor(int major)
Set major version number of class file, default value is 45 (JDK 1.1)

param
major major version number

    this.major = major;
  
public voidsetMethodAt(com.sun.org.apache.bcel.internal.classfile.Method method, int pos)

    method_vec.set(pos, method);
  
public voidsetMethods(com.sun.org.apache.bcel.internal.classfile.Method[] methods)

    method_vec.clear();
    for(int m=0; m<methods.length; m++)
      addMethod(methods[m]);
  
public voidsetMinor(int minor)
Set minor version number of class file, default value is 3 (JDK 1.1)

param
minor minor version number

    this.minor = minor;
  
public voidsetSuperclassName(java.lang.String name)

    super_class_name = name.replace('/", '.");
    superclass_name_index = cp.addClass(name);
  
public voidsetSuperclassNameIndex(int superclass_name_index)

    this.superclass_name_index = superclass_name_index;
    super_class_name = cp.getConstantPool().
      getConstantString(superclass_name_index, Constants.CONSTANT_Class).replace('/", '.");
  
public voidupdate()
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.

    if(observers != null)
      for(Iterator e = observers.iterator(); e.hasNext(); )
	((ClassObserver)e.next()).notify(this);