org.apache.avalon.fortress.impl.factory
Class BCELCodeGenerator

java.lang.Object
  extended by org.apache.avalon.fortress.impl.factory.BCELCodeGenerator

public final class BCELCodeGenerator
extends java.lang.Object

BCELCodeGenerator creates implementations for the Methods and Fields needed in creating a WrapperClass.

Author:
Avalon Development Team

Nested Class Summary
private static class BCELCodeGenerator.MethodDesc
           
 
Field Summary
private static java.lang.String ACCESSOR_METHOD_NAME
          The name of the field accessor used to ask the generated wrapper for the wrapped class instance.
private  org.apache.bcel.generic.ClassGen m_classGenerator
          The ClassGen instance to use for code generation.
private  org.apache.bcel.classfile.JavaClass m_classToWrap
          Class object holding the type of the object we want to create a wrapper for.
private  org.apache.bcel.generic.Type m_classToWrapType
          The Type of the class we want to create a wrapper for.
private  org.apache.bcel.generic.ConstantPoolGen m_constPoolGenerator
          The ConstantPoolGen instance to use for code generation.
private  org.apache.bcel.generic.InstructionFactory m_instructionFactory
          The InstructionFactory to use during code gereration.
private  org.apache.bcel.generic.InstructionList m_instructionList
          The InstructionList instance to use during code generation.
private  boolean m_isInitialized
          Flag indicating whether this instance is already initialized or not.
private  java.lang.String m_wrapperClassName
          The name of the wrapper class to be created.
private  java.lang.String m_wrapperSuperclassName
          The name of the superclass of the wrapper class to be generated.
private static java.lang.String WRAPPED_CLASS_FN
          The name of the field holding the wrapped class in the generated wrapper, e.g.
 
Constructor Summary
BCELCodeGenerator()
          Default constructor.
 
Method Summary
 org.apache.bcel.classfile.Method createDefaultConstructor()
          Create the wrapper class' default constructor:
 org.apache.bcel.classfile.Method[] createImplementation(org.apache.bcel.classfile.JavaClass[] interfacesToImplement)
          Creates an implementation for the supplied JavaClass instance representing an interface.
 org.apache.bcel.classfile.Method createMethodWrapper(BCELCodeGenerator.MethodDesc meth)
          Create a method declaration/definition of the form
 org.apache.bcel.classfile.Method createMethodWrapper(org.apache.bcel.classfile.Method methodToWrap)
          Create a method declaration/definition of the form
 org.apache.bcel.classfile.Method createWrappedClassAccessor()
          Create a field accessor for the wrapped class instance of the form
 org.apache.bcel.classfile.Field createWrappedClassField()
          Create a field declaration of the form
private  org.apache.bcel.classfile.Method extractMethod(org.apache.bcel.generic.MethodGen mg)
          Extracts the Method out of the supplied MethodGen instance, clears the InstructionList and returns the extracted Method.
(package private) static BCELCodeGenerator.MethodDesc[] extractMethods(org.apache.bcel.classfile.JavaClass[] interfacesToImplement)
          Extracts the collection of Methods declared in the supplied JavaClass instance.
private static void extractMethods(org.apache.bcel.classfile.JavaClass interfaceToImplement, java.util.Set methods)
           
private  void findImplementation(BCELCodeGenerator.MethodDesc meth)
           
 void init(java.lang.String wrapperClassName, java.lang.String wrapperSuperclassName, org.apache.bcel.classfile.JavaClass classToWrap, org.apache.bcel.generic.ClassGen classGenerator)
           
private  boolean isInitialized()
          Has this instance already been initialized?
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WRAPPED_CLASS_FN

private static final java.lang.String WRAPPED_CLASS_FN
The name of the field holding the wrapped class in the generated wrapper, e.g.
  
      private final  WRAPPED_CLASS_FN;
  
 

See Also:
Constant Field Values

ACCESSOR_METHOD_NAME

private static final java.lang.String ACCESSOR_METHOD_NAME
The name of the field accessor used to ask the generated wrapper for the wrapped class instance.

See Also:
Constant Field Values

m_wrapperClassName

private java.lang.String m_wrapperClassName
The name of the wrapper class to be created.


m_wrapperSuperclassName

private java.lang.String m_wrapperSuperclassName
The name of the superclass of the wrapper class to be generated.


m_classToWrap

private org.apache.bcel.classfile.JavaClass m_classToWrap
Class object holding the type of the object we want to create a wrapper for.


m_classToWrapType

private org.apache.bcel.generic.Type m_classToWrapType
The Type of the class we want to create a wrapper for.


m_classGenerator

private org.apache.bcel.generic.ClassGen m_classGenerator
The ClassGen instance to use for code generation.


m_constPoolGenerator

private org.apache.bcel.generic.ConstantPoolGen m_constPoolGenerator
The ConstantPoolGen instance to use for code generation.


m_instructionList

private final org.apache.bcel.generic.InstructionList m_instructionList
The InstructionList instance to use during code generation.


m_instructionFactory

private org.apache.bcel.generic.InstructionFactory m_instructionFactory
The InstructionFactory to use during code gereration.


m_isInitialized

private boolean m_isInitialized
Flag indicating whether this instance is already initialized or not.

Constructor Detail

BCELCodeGenerator

public BCELCodeGenerator()
Default constructor.

Method Detail

init

public void init(java.lang.String wrapperClassName,
                 java.lang.String wrapperSuperclassName,
                 org.apache.bcel.classfile.JavaClass classToWrap,
                 org.apache.bcel.generic.ClassGen classGenerator)
          throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

createWrappedClassField

public org.apache.bcel.classfile.Field createWrappedClassField()
                                                        throws java.lang.IllegalStateException
Create a field declaration of the form
  
      private  WRAPPED_CLASS_FN;
  
 

Returns:
Field
Throws:
java.lang.IllegalStateException - If this instance is not initialized.

createDefaultConstructor

public org.apache.bcel.classfile.Method createDefaultConstructor()
                                                          throws java.lang.IllegalStateException
Create the wrapper class' default constructor:
  
      public ( classToWrap)
      {
          this. = classToWrap;
      }
  
 

Returns:
The created default constructor
Throws:
java.lang.IllegalStateException - If this instance is not initialized.

createWrappedClassAccessor

public org.apache.bcel.classfile.Method createWrappedClassAccessor()
                                                            throws java.lang.IllegalStateException
Create a field accessor for the wrapped class instance of the form
  
      public Object ()
      {
          return this.;
      }
  
 

Returns:
Method
Throws:
java.lang.IllegalStateException

createMethodWrapper

public org.apache.bcel.classfile.Method createMethodWrapper(BCELCodeGenerator.MethodDesc meth)
                                                     throws java.lang.IllegalArgumentException,
                                                            java.lang.IllegalStateException
Create a method declaration/definition of the form
  
      public  ()
          throws 
      {
          return this..();
      }
  
 

Parameters:
meth - The method descriptor
Returns:
Method The Method object representing the created method
Throws:
java.lang.IllegalArgumentException - If any of the parameters passed in is null.
java.lang.IllegalStateException - If this instance is not initialized.

findImplementation

private void findImplementation(BCELCodeGenerator.MethodDesc meth)

createMethodWrapper

public org.apache.bcel.classfile.Method createMethodWrapper(org.apache.bcel.classfile.Method methodToWrap)
                                                     throws java.lang.IllegalArgumentException,
                                                            java.lang.IllegalStateException
Create a method declaration/definition of the form
  
      public  ()
          throws 
      {
          return this..();
      }
  
 

Parameters:
methodToWrap - The Method to create a wrapper for.
Returns:
Method The wrapper method.
Throws:
java.lang.IllegalArgumentException - If methodToWrao is null.
java.lang.IllegalStateException - If this instance is not initialized.

createImplementation

public org.apache.bcel.classfile.Method[] createImplementation(org.apache.bcel.classfile.JavaClass[] interfacesToImplement)
                                                        throws java.lang.Exception
Creates an implementation for the supplied JavaClass instance representing an interface.

Parameters:
interfacesToImplement - The interfaces we want to create an implementation for
Returns:
Method[] An array of Method instances representing the interface implementation.
Throws:
java.lang.IllegalArgumentException - If interfaceToImplement is null or does not represent an interface
java.lang.IllegalStateException - If this instance has not been initialized
java.lang.Exception

extractMethod

private org.apache.bcel.classfile.Method extractMethod(org.apache.bcel.generic.MethodGen mg)
Extracts the Method out of the supplied MethodGen instance, clears the InstructionList and returns the extracted Method.

Parameters:
mg - The MethodGen instance holding the Method to extract
Returns:
The extracted Method

isInitialized

private boolean isInitialized()
Has this instance already been initialized?

Returns:
TRUE, if this instance has already been initialized, FALSE otherwise

extractMethods

static BCELCodeGenerator.MethodDesc[] extractMethods(org.apache.bcel.classfile.JavaClass[] interfacesToImplement)
                                              throws java.lang.Exception
Extracts the collection of Methods declared in the supplied JavaClass instance. This instance is supposed to represent an interface.

Parameters:
interfacesToImplement - The JavaClass instances representing the interfaces we are asking for its methods.
Returns:
MethodDesc[] The array of Methods declared by the interface
Throws:
java.lang.IllegalArgumentException - If interfaceToImplement does not represent an interface
java.lang.NullPointerException - if the interfaceToImplement is null
java.lang.Exception

extractMethods

private static final void extractMethods(org.apache.bcel.classfile.JavaClass interfaceToImplement,
                                         java.util.Set methods)