org.datanucleus.enhancer.bcel
Class BCELClassEnhancer

java.lang.Object
  extended by org.datanucleus.enhancer.AbstractClassEnhancer
      extended by org.datanucleus.enhancer.bcel.BCELClassEnhancer
All Implemented Interfaces:
ClassEnhancer

public class BCELClassEnhancer
extends AbstractClassEnhancer

Class enhancer using Apache BCEL (http://jakarta.apache.org/bcel).


Nested Class Summary
 class BCELClassEnhancer.SerialVersionUID
          Compute the serialVersionUID
 
Field Summary
protected  long addSerialVersionUID
          serialVersionUID value
 org.apache.bcel.generic.ObjectType classType
          class type of enhancing class
 org.apache.bcel.generic.ConstantPoolGen constantPoolGen
          constant pool of enhancing class
 org.apache.bcel.generic.ClassGen newClass
          enhancing class
 org.apache.bcel.classfile.JavaClass oldClass
          Original class
static org.apache.bcel.generic.ObjectType OT_BitSet
          Object type of java.util.BitSet
static org.apache.bcel.generic.ObjectType OT_ByteIdentity
           
static org.apache.bcel.generic.ObjectType OT_CharIdentity
           
static org.apache.bcel.generic.ObjectType OT_CLASS
          Object type of java.lang.Class
static org.apache.bcel.generic.ObjectType OT_Detachable
          Object type of javax.jdo.spi.Detachable
static org.apache.bcel.generic.Type OT_Flag
          Field type of jdoFlag
static org.apache.bcel.generic.ObjectType OT_IntIdentity
           
static org.apache.bcel.generic.ObjectType OT_JDOImplHelper
          Object type of javax.jdo.spi.JDOImplHelper
static org.apache.bcel.generic.ObjectType OT_LongIdentity
          Object type of SingleFieldIdentity classes
static org.apache.bcel.generic.Type OT_ObjectArray
          Type of Object[]
static org.apache.bcel.generic.ObjectType OT_ObjectIdentity
           
static org.apache.bcel.generic.ObjectType OT_ObjectIdFieldConsumer
          Object type of javax.jdo.spi.PersistenceCapable.ObjectIdFieldConsumer
static org.apache.bcel.generic.ObjectType OT_ObjectIdFieldSupplier
          Object type of javax.jdo.spi.PersistenceCapable.ObjectIdFieldSupplier
static org.apache.bcel.generic.ObjectType OT_PersistenceCapable
          Object type of javax.jdo.spi.PersistenceCapable
static org.apache.bcel.generic.ObjectType OT_PersistenceManager
          Object type of javax.spi.PersistenceManager
static org.apache.bcel.generic.ObjectType OT_ShortIdentity
           
static org.apache.bcel.generic.ObjectType OT_StateManager
          Object type of javax.jdo.spi.StateManager
static org.apache.bcel.generic.ObjectType OT_StringIdentity
           
 
Fields inherited from class org.datanucleus.enhancer.AbstractClassEnhancer
className, clr, cmd, fieldsToAdd, initialised, LOCALISER, methodsToAdd, update
 
Fields inherited from interface org.datanucleus.enhancer.ClassEnhancer
CN_BitSet, CN_ByteIdentity, CN_CharIdentity, CN_Class, CN_ClassCastException, CN_Detachable, CN_IllegalArgumentException, CN_IllegalStateException, CN_IntIdentity, CN_JDODetachedFieldAccessException, CN_JDOFatalInternalException, CN_JDOHelper, CN_JDOImplHelper, CN_LongIdentity, CN_ObjectIdentity, CN_ObjectIdFieldConsumer, CN_ObjectIdFieldSupplier, CN_PersistenceCapable, CN_PersistenceManager, CN_ShortIdentity, CN_StateManager, CN_StringIdentity, FN_FieldFlags, FN_FieldNames, FN_FieldTypes, FN_Flag, FN_JdoDetachedState, FN_JdoInheritedFieldCount, FN_PersistenceCapableSuperclass, FN_serialVersionUID, FN_StateManager, MN_FieldFlagsInitMethod, MN_FieldNamesInitMethod, MN_FieldTypesInitMethod, MN_JdoCopyField, MN_JdoCopyFields, MN_JdoCopyKeyFieldsFromObjectId, MN_JdoCopyKeyFieldsToObjectId, MN_JdoGetInheritedFieldCount, MN_JdoGetManagedFieldCount, MN_JdoGetObjectId, MN_JdoGetPersistenceManager, MN_JdoGetTransactionalObjectId, MN_JdoGetVersion, MN_JdoIsDeleted, MN_JdoIsDetached, MN_JdoIsDetachedInternal, MN_JdoIsDirty, MN_JdoIsNew, MN_JdoIsPersistent, MN_JdoIsTransactional, MN_jdoLoadClass, MN_JdoMakeDirty, MN_JdoMakeDirtyDetached, MN_JdoNewInstance, MN_JdoNewObjectIdInstance, MN_JdoPersistenceCapableSuperclassInit, MN_JdoPreSerialize, MN_JdoProvideField, MN_JdoProvideFields, MN_JdoReplaceDetachedState, MN_JdoReplaceField, MN_JdoReplaceFields, MN_JdoReplaceFlags, MN_JdoReplaceStateManager, MN_JdoSuperClone
 
Constructor Summary
BCELClassEnhancer(org.datanucleus.metadata.ClassMetaData cmd, org.datanucleus.ClassLoaderResolver clr)
          Constructor.
BCELClassEnhancer(org.datanucleus.metadata.ClassMetaData cmd, org.datanucleus.ClassLoaderResolver clr, byte[] classBytes)
          Constructor.
 
Method Summary
protected  java.lang.String checkHasDefaultConstructor()
          Check original class has default(no arg) constructor.
protected  void class_addInterface(java.lang.String interfaceName)
          Method to add the "implements {interface}" to the class description.
 boolean enhance()
          Method to enhance the classes
protected  void enhanceClass()
          Method to enhance the class as a whole, providing the required interfaces and adding any setters/getters for its fields
protected  void enhanceFields()
          Method to enhance the fields
protected  void enhanceGetter(BCELFieldPropertyMetaData fieldConfig)
          This method adds to the generated class the jdoGet methods
protected  void enhanceMethods()
          Method to enhance the methods of the class.
protected  void enhanceOriginalMethod(org.apache.bcel.classfile.Method m)
           
protected  void enhanceOriginalMethods()
           
protected  void enhancePropertyAccessor(org.apache.bcel.classfile.Method m)
          Takes the original getXXX, setXXX method and takes the code and creates a jdoGetXXX, jdoSetXXX with the exact same code.
protected  void enhancePropertyGetter(org.apache.bcel.classfile.Method m)
          Method to take the method and create a valid getXXX when the method is a getter for a persistent property.
protected  void enhancePropertySetter(org.apache.bcel.classfile.Method m)
          Method to take the method and create a valid setXXX when the method is a setter for a persistent property.
protected  void enhanceSetter(BCELFieldPropertyMetaData fieldConfig)
          This method adds to the generated class the jdoSet methods
protected  void enhanceStaticInitializers()
           
 byte[] getBytes()
          Access the class in byte array format
static java.lang.String getClassNameForFileName(java.lang.String filename)
          Convenience accessor for the class name that is stored in a particular class.
protected  void initialiseFieldsList()
          Initialise the fields that we need to add to this class
protected  void initialiseMethodsList()
          Initialise the methods that we need to add to this class
protected  void staticInitializerAppend(org.apache.bcel.generic.InstructionFactory factory, org.apache.bcel.generic.InstructionList il)
           
 boolean validate()
          Check original class is already enhanced.
 
Methods inherited from class org.datanucleus.enhancer.AbstractClassEnhancer
getClassLoaderResolver, getClassMetaData, getFieldsList, getMethodsList, initialise, isPersistenceCapable, requiresDetachable, save
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

oldClass

public final org.apache.bcel.classfile.JavaClass oldClass
Original class


newClass

public final org.apache.bcel.generic.ClassGen newClass
enhancing class


constantPoolGen

public final org.apache.bcel.generic.ConstantPoolGen constantPoolGen
constant pool of enhancing class


classType

public final org.apache.bcel.generic.ObjectType classType
class type of enhancing class


addSerialVersionUID

protected long addSerialVersionUID
serialVersionUID value


OT_Flag

public static final org.apache.bcel.generic.Type OT_Flag
Field type of jdoFlag


OT_ObjectArray

public static final org.apache.bcel.generic.Type OT_ObjectArray
Type of Object[]


OT_LongIdentity

public static final org.apache.bcel.generic.ObjectType OT_LongIdentity
Object type of SingleFieldIdentity classes


OT_StringIdentity

public static final org.apache.bcel.generic.ObjectType OT_StringIdentity

OT_ShortIdentity

public static final org.apache.bcel.generic.ObjectType OT_ShortIdentity

OT_IntIdentity

public static final org.apache.bcel.generic.ObjectType OT_IntIdentity

OT_CharIdentity

public static final org.apache.bcel.generic.ObjectType OT_CharIdentity

OT_ByteIdentity

public static final org.apache.bcel.generic.ObjectType OT_ByteIdentity

OT_ObjectIdentity

public static final org.apache.bcel.generic.ObjectType OT_ObjectIdentity

OT_PersistenceManager

public static final org.apache.bcel.generic.ObjectType OT_PersistenceManager
Object type of javax.spi.PersistenceManager


OT_ObjectIdFieldConsumer

public static final org.apache.bcel.generic.ObjectType OT_ObjectIdFieldConsumer
Object type of javax.jdo.spi.PersistenceCapable.ObjectIdFieldConsumer


OT_ObjectIdFieldSupplier

public static final org.apache.bcel.generic.ObjectType OT_ObjectIdFieldSupplier
Object type of javax.jdo.spi.PersistenceCapable.ObjectIdFieldSupplier


OT_BitSet

public static final org.apache.bcel.generic.ObjectType OT_BitSet
Object type of java.util.BitSet


OT_StateManager

public static final org.apache.bcel.generic.ObjectType OT_StateManager
Object type of javax.jdo.spi.StateManager


OT_PersistenceCapable

public static final org.apache.bcel.generic.ObjectType OT_PersistenceCapable
Object type of javax.jdo.spi.PersistenceCapable


OT_Detachable

public static final org.apache.bcel.generic.ObjectType OT_Detachable
Object type of javax.jdo.spi.Detachable


OT_JDOImplHelper

public static final org.apache.bcel.generic.ObjectType OT_JDOImplHelper
Object type of javax.jdo.spi.JDOImplHelper


OT_CLASS

public static final org.apache.bcel.generic.ObjectType OT_CLASS
Object type of java.lang.Class

Constructor Detail

BCELClassEnhancer

public BCELClassEnhancer(org.datanucleus.metadata.ClassMetaData cmd,
                         org.datanucleus.ClassLoaderResolver clr,
                         byte[] classBytes)
Constructor.

Parameters:
cmd - MetaData for the class to be enhanced
clr - ClassLoader resolver
classBytes - Bytes of the class (unenhanced)

BCELClassEnhancer

public BCELClassEnhancer(org.datanucleus.metadata.ClassMetaData cmd,
                         org.datanucleus.ClassLoaderResolver clr)
Constructor.

Parameters:
cmd - MetaData for the class to be enhanced
clr - ClassLoader resolver
Method Detail

getClassNameForFileName

public static java.lang.String getClassNameForFileName(java.lang.String filename)
Convenience accessor for the class name that is stored in a particular class.

Parameters:
filename - Name of the file
Returns:
The class name

initialiseMethodsList

protected void initialiseMethodsList()
Initialise the methods that we need to add to this class

Specified by:
initialiseMethodsList in class AbstractClassEnhancer

initialiseFieldsList

protected void initialiseFieldsList()
Initialise the fields that we need to add to this class

Specified by:
initialiseFieldsList in class AbstractClassEnhancer

validate

public boolean validate()
Check original class is already enhanced.

Returns:
Return true if already enhanced class.

checkHasDefaultConstructor

protected java.lang.String checkHasDefaultConstructor()
Check original class has default(no arg) constructor.
Original class must has default(no arg) constructor.

Returns:
Return null if this class has default constructor.

enhance

public boolean enhance()
Method to enhance the classes

Returns:
Whether the class was enhanced successfully

enhanceClass

protected void enhanceClass()
Method to enhance the class as a whole, providing the required interfaces and adding any setters/getters for its fields


class_addInterface

protected void class_addInterface(java.lang.String interfaceName)
Method to add the "implements {interface}" to the class description.

Parameters:
interfaceName - Name of the interface to add.

enhanceFields

protected void enhanceFields()
Method to enhance the fields


enhanceMethods

protected void enhanceMethods()
Method to enhance the methods of the class. Processes all methods in methodsToAdd, adding them to the class.


enhanceStaticInitializers

protected void enhanceStaticInitializers()

staticInitializerAppend

protected void staticInitializerAppend(org.apache.bcel.generic.InstructionFactory factory,
                                       org.apache.bcel.generic.InstructionList il)

enhanceOriginalMethod

protected void enhanceOriginalMethod(org.apache.bcel.classfile.Method m)

enhancePropertyAccessor

protected void enhancePropertyAccessor(org.apache.bcel.classfile.Method m)
Takes the original getXXX, setXXX method and takes the code and creates a jdoGetXXX, jdoSetXXX with the exact same code. The jdoGetXXX, jdoSetXXX are not static (unlike for persistent fields). e.g. public String getName() { return name; } The generated method is: public String jdoGetName() { return name; }

Parameters:
m - The method

enhanceOriginalMethods

protected void enhanceOriginalMethods()

enhancePropertySetter

protected void enhancePropertySetter(org.apache.bcel.classfile.Method m)
Method to take the method and create a valid setXXX when the method is a setter for a persistent property.

Parameters:
m - The method

enhancePropertyGetter

protected void enhancePropertyGetter(org.apache.bcel.classfile.Method m)
Method to take the method and create a valid getXXX when the method is a getter for a persistent property.

Parameters:
m - The method

enhanceSetter

protected void enhanceSetter(BCELFieldPropertyMetaData fieldConfig)
This method adds to the generated class the jdoSet methods

Parameters:
fieldConfig -

enhanceGetter

protected void enhanceGetter(BCELFieldPropertyMetaData fieldConfig)
This method adds to the generated class the jdoGet methods

Parameters:
fieldConfig -

getBytes

public byte[] getBytes()
Access the class in byte array format

Returns:
the class in byte array format


Copyright © 2009. All Rights Reserved.