Class ClassEnhancerImpl
java.lang.Object
org.datanucleus.enhancer.ClassEnhancerImpl
- All Implemented Interfaces:
ClassEnhancer
Class enhancer using ASM (see http://asm.objectweb.org but included in DataNucleus core repackaged).
ASM operates using a SAXParser-like "visitor-pattern". We utilise this as follows :-
- enhance : start with a ClassReader for the class to be enhanced, and create a EnhancerClassAdapter (attached to a ClassWriter) that will perform the modifications, and use that as a visitor for the reader so that the reader sends its events to the adapter. Within the EnhancerClassAdapter we also make use of a EnhancerMethodAdapter to update individual methods
- check : take a ClassReader, and create a EnhancerClassChecker that performs the checks. We then set the checker as a visitor for the reader so that the reader sends its events to the checker.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classConvenience class to look up the class name for a file. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringASM Class name for this class (replace .protected byte[]Bytes of the class (after enhancing).protected StringClass descriptor for this class.final StringClass name of the class being enhancedprotected final ClassLoaderResolverClass Loader Resolver to use for any loading issues.protected final ClassClass that is being enhanced.protected final ClassMetaDataMetaData for the class being enhanced.protected List<ClassField> List of fields to be added to the class.protected booleanFlag for whether we are initialised.protected byte[]Bytes of the input class (only when enhancing generated classes with no class file).protected StringResource name of the input class (only when the class exists in a class file).protected final MetaDataManagerMetaData Manager to use.protected List<ClassMethod> List of methods to be added to the class.protected EnhancementNamerprotected Collection<String> Options for enhancement.protected byte[]Bytes for any auto-generated PK class (if generated during enhancement).protected booleanFlag specifying if the class needs updating.Fields inherited from interface ClassEnhancer
ASM_API_VERSION, OPTION_GENERATE_DEFAULT_CONSTRUCTOR, OPTION_GENERATE_DETACH_LISTENER, OPTION_GENERATE_PK -
Constructor Summary
ConstructorsConstructorDescriptionClassEnhancerImpl(ClassMetaData cmd, ClassLoaderResolver clr, MetaDataManager mmgr, EnhancementNamer namer) Constructor for an enhancer for the class.ClassEnhancerImpl(ClassMetaData cmd, ClassLoaderResolver clr, MetaDataManager mmgr, EnhancementNamer namer, byte[] classBytes) Constructor for an enhancer to enhance a class defined by the provided bytes. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckClassIsEnhanced(boolean logErrors) Convenience method to return if a class is enhanced.booleanenhance()Method to enhance a classes definition.Accessor for the ASM class nameAccessor for the class being enhanced.byte[]Accessor for the class bytes.Accessor for the class descriptor for the class being enhancedAccessor for the ClassLoaderResolverAccessor for the ClassMetaData for the class.Accessor for the name of the class being enhanced.static StringgetClassNameForFileName(String filename) Convenience accessor for the class name that is stored in a particular class.Accessor for the fields required.Accessor for the MetaData manager in use.Accessor for the methods required.getNamer()byte[]Accessor for the primary-key class bytes (if generating a PK).booleanAccessor for whether a particular option is enabled.protected voidInitialisation of the information for enhancing this class.protected voidMethod to initialise the list of fields to add.protected voidMethod to initialise the list of methods to add.booleanisPersistable(String className) Check if the class is Persistable or is going to be enhanced based on the metadataprotected booleanConvenience method for whether this class needs to implement DetachablevoidMethod to save the class definition bytecode into a class file.voidsetNamer(EnhancementNamer namer) voidsetOptions(Collection<String> options) Method to set the options controlling the enhancement.booleanvalidate()Validate whether the class is enhanced.
-
Field Details
-
clr
Class Loader Resolver to use for any loading issues. -
metaDataMgr
MetaData Manager to use. -
cmd
MetaData for the class being enhanced. -
className
Class name of the class being enhanced -
update
protected boolean updateFlag specifying if the class needs updating. -
fieldsToAdd
List of fields to be added to the class. -
methodsToAdd
List of methods to be added to the class. -
initialised
protected boolean initialisedFlag for whether we are initialised. -
options
Options for enhancement. -
inputResourceName
Resource name of the input class (only when the class exists in a class file). -
inputBytes
protected byte[] inputBytesBytes of the input class (only when enhancing generated classes with no class file). -
cls
Class that is being enhanced. -
classBytes
protected byte[] classBytesBytes of the class (after enhancing). -
pkClassBytes
protected byte[] pkClassBytesBytes for any auto-generated PK class (if generated during enhancement). -
asmClassName
ASM Class name for this class (replace . with /). -
classDescriptor
Class descriptor for this class. -
namer
-
-
Constructor Details
-
ClassEnhancerImpl
public ClassEnhancerImpl(ClassMetaData cmd, ClassLoaderResolver clr, MetaDataManager mmgr, EnhancementNamer namer) Constructor for an enhancer for the class. The class is assumed to be in the CLASSPATH.- Parameters:
cmd- MetaData for the class to be enhancedclr- ClassLoader resolvermmgr- MetaData managernamer- The namer
-
ClassEnhancerImpl
public ClassEnhancerImpl(ClassMetaData cmd, ClassLoaderResolver clr, MetaDataManager mmgr, EnhancementNamer namer, byte[] classBytes) Constructor for an enhancer to enhance a class defined by the provided bytes.- Parameters:
cmd- MetaData for the class to be enhancedclr- ClassLoader resolvermmgr- MetaData managernamer- The namerclassBytes- Bytes of the class to enhance
-
-
Method Details
-
initialise
protected void initialise()Initialisation of the information for enhancing this class. -
getClassName
Description copied from interface:ClassEnhancerAccessor for the name of the class being enhanced.- Specified by:
getClassNamein interfaceClassEnhancer- Returns:
- Class name
-
getMethodsList
Accessor for the methods required.- Specified by:
getMethodsListin interfaceClassEnhancer- Returns:
- List of methods required for enhancement
-
getFieldsList
Accessor for the fields required.- Specified by:
getFieldsListin interfaceClassEnhancer- Returns:
- List of fields required for enhancement
-
getClassLoaderResolver
Accessor for the ClassLoaderResolver- Specified by:
getClassLoaderResolverin interfaceClassEnhancer- Returns:
- ClassLoader resolver
-
getMetaDataManager
Description copied from interface:ClassEnhancerAccessor for the MetaData manager in use.- Specified by:
getMetaDataManagerin interfaceClassEnhancer- Returns:
- MetaData manager
-
getClassMetaData
Description copied from interface:ClassEnhancerAccessor for the ClassMetaData for the class.- Specified by:
getClassMetaDatain interfaceClassEnhancer- Returns:
- MetaData for the class
-
requiresDetachable
protected boolean requiresDetachable()Convenience method for whether this class needs to implement Detachable- Returns:
- Whether we need to implement the Detachable interface
-
isPersistable
Check if the class is Persistable or is going to be enhanced based on the metadata- Specified by:
isPersistablein interfaceClassEnhancer- Parameters:
className- the class name- Returns:
- true if Persistable
-
setOptions
Description copied from interface:ClassEnhancerMethod to set the options controlling the enhancement.- Specified by:
setOptionsin interfaceClassEnhancer- Parameters:
options- The options
-
hasOption
Description copied from interface:ClassEnhancerAccessor for whether a particular option is enabled.- Specified by:
hasOptionin interfaceClassEnhancer- Parameters:
name- Name of the option- Returns:
- Whether it has this option
-
save
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:
savein interfaceClassEnhancer- Parameters:
directoryName- Name of a directory (or null to overwrite the class)- Throws:
IOException- If an I/O error occurs in the write.
-
setNamer
- Specified by:
setNamerin interfaceClassEnhancer
-
getClassNameForFileName
-
getClassBeingEnhanced
Accessor for the class being enhanced.- Specified by:
getClassBeingEnhancedin interfaceClassEnhancer- Returns:
- Class being enhanced
-
getASMClassName
Accessor for the ASM class name- Specified by:
getASMClassNamein interfaceClassEnhancer- Returns:
- ASM class name
-
getClassDescriptor
Accessor for the class descriptor for the class being enhanced- Specified by:
getClassDescriptorin interfaceClassEnhancer- Returns:
- class descriptor
-
initialiseMethodsList
protected void initialiseMethodsList()Method to initialise the list of methods to add. -
initialiseFieldsList
protected void initialiseFieldsList()Method to initialise the list of fields to add. -
enhance
public boolean enhance()Method to enhance a classes definition.- Specified by:
enhancein interfaceClassEnhancer- Returns:
- Whether it was enhanced with no errors
-
getClassBytes
public byte[] getClassBytes()Accessor for the class bytes. Only has relevance to be called after enhance().- Specified by:
getClassBytesin interfaceClassEnhancer- Returns:
- The class bytes
-
getPrimaryKeyClassBytes
public byte[] getPrimaryKeyClassBytes()Accessor for the primary-key class bytes (if generating a PK). Only has relevance to be called after enhance().- Specified by:
getPrimaryKeyClassBytesin interfaceClassEnhancer- Returns:
- The primary-key class bytes
-
validate
public boolean validate()Description copied from interface:ClassEnhancerValidate whether the class is enhanced.- Specified by:
validatein interfaceClassEnhancer- 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
-
getNamer
- Specified by:
getNamerin interfaceClassEnhancer
-