org.datanucleus.enhancer.asm
Class ASMClassEnhancer

java.lang.Object
  extended by org.datanucleus.enhancer.AbstractClassEnhancer
      extended by org.datanucleus.enhancer.asm.ASMClassEnhancer
All Implemented Interfaces:
ClassEnhancer

public class ASMClassEnhancer
extends AbstractClassEnhancer

Class enhancer using ASM (http://asm.objectweb.org). Assumes that the version of ASM is 3.0 or above. ASM operates using a SAXParser-like "visitor-pattern". We utilise this as follows :-


Nested Class Summary
static class ASMClassEnhancer.MyClassVisitor
          Convenience class to look up the class name for a file.
 
Field Summary
protected  java.lang.String asmClassName
          ASM Class name for this class (replace .
protected  byte[] classBytes
          Bytes of the class (after enhancing).
protected  java.lang.String classDescriptor
          Class descriptor for this class.
protected  java.lang.Class cls
          Class that is being enhanced.
protected  byte[] inputBytes
          Bytes of the input class (only when enhancing generated classes with no class file).
protected  java.lang.String inputResourceName
          Resource name of the input class (only when the class exists in a class file).
 
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
ASMClassEnhancer(org.datanucleus.metadata.ClassMetaData cmd, org.datanucleus.ClassLoaderResolver clr)
          Constructor.
ASMClassEnhancer(org.datanucleus.metadata.ClassMetaData cmd, org.datanucleus.ClassLoaderResolver clr, byte[] classBytes)
          Constructor.
 
Method Summary
protected  boolean checkClassIsEnhanced(boolean logErrors)
          Convenience method to return if a class is enhanced.
 boolean enhance()
          Method to enhance a classes definition.
 java.lang.String getASMClassName()
          Accessor for the ASM class name
 byte[] getBytes()
          Accessor for the class bytes.
 java.lang.String getClassDescriptor()
          Accessor for the class descriptor for the class being enhanced
 java.lang.Class getClassEnhanced()
          Accessor for the class being enhanced.
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()
          Method to initialise the list of fields to add.
protected  void initialiseMethodsList()
          Method to initialise the list of methods to add.
 void save(java.lang.String directoryName)
          Method to save the class definition bytecode into a class file.
 boolean validate()
          Validate whether the class is enhanced.
 
Methods inherited from class org.datanucleus.enhancer.AbstractClassEnhancer
getClassLoaderResolver, getClassMetaData, getFieldsList, getMethodsList, initialise, isPersistenceCapable, requiresDetachable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inputResourceName

protected java.lang.String inputResourceName
Resource name of the input class (only when the class exists in a class file).


inputBytes

protected byte[] inputBytes
Bytes of the input class (only when enhancing generated classes with no class file).


cls

protected final java.lang.Class cls
Class that is being enhanced.


classBytes

protected byte[] classBytes
Bytes of the class (after enhancing).


asmClassName

protected java.lang.String asmClassName
ASM Class name for this class (replace . with /).


classDescriptor

protected java.lang.String classDescriptor
Class descriptor for this class.

Constructor Detail

ASMClassEnhancer

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

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

ASMClassEnhancer

public ASMClassEnhancer(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 to enhance
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

getClassEnhanced

public java.lang.Class getClassEnhanced()
Accessor for the class being enhanced.

Returns:
Class being enhanced

getASMClassName

public java.lang.String getASMClassName()
Accessor for the ASM class name

Returns:
ASM class name

getClassDescriptor

public java.lang.String getClassDescriptor()
Accessor for the class descriptor for the class being enhanced

Returns:
class descriptor

initialiseMethodsList

protected void initialiseMethodsList()
Method to initialise the list of methods to add.

Specified by:
initialiseMethodsList in class AbstractClassEnhancer

initialiseFieldsList

protected void initialiseFieldsList()
Method to initialise the list of fields to add.

Specified by:
initialiseFieldsList in class AbstractClassEnhancer

enhance

public boolean enhance()
Method to enhance a classes definition.

Returns:
Whether it was enhanced with no errors

getBytes

public byte[] getBytes()
Accessor for the class bytes. Only has relevance to be called after enhance().

Returns:
The class bytes

save

public void save(java.lang.String directoryName)
          throws java.io.IOException
Method to save the class definition bytecode into a class file. If directoryName is specified it will be written to $directoryName/className.class else will overwrite the existing class.

Specified by:
save in interface ClassEnhancer
Overrides:
save in class AbstractClassEnhancer
Parameters:
directoryName - Name of a directory (or null to overwrite the class)
Throws:
java.io.IOException - If an I/O error occurs in the write.

validate

public boolean validate()
Description copied from interface: ClassEnhancer
Validate whether the class is enhanced.

Returns:
Return true if already enhanced class.

checkClassIsEnhanced

protected boolean checkClassIsEnhanced(boolean logErrors)
Convenience method to return if a class is enhanced.

Parameters:
logErrors - Whether to log any errors (missing methods etc) as errors (otherwise info/debug)
Returns:
Whether the class is enhanced


Copyright © 2009. All Rights Reserved.