Package org.apache.openjpa.enhance
Class PCEnhancer
java.lang.Object
org.apache.openjpa.enhance.PCEnhancer
Bytecode enhancer used to enhance persistent classes from metadata. The
enhancer must be invoked on all persistence-capable and persistence aware
classes.
- Author:
- Abe White, Mark Struberg
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Plugin interface for additional enhancement.static class
Run flags. -
Field Summary
-
Constructor Summary
ConstructorDescriptionPCEnhancer
(OpenJPAConfiguration conf, Class<?> type) Constructor.PCEnhancer
(OpenJPAConfiguration conf, ClassMetaData meta) Constructor.PCEnhancer
(OpenJPAConfiguration conf, ClassNodeTracker type, MetaDataRepository repos) Deprecated.PCEnhancer
(OpenJPAConfiguration conf, ClassNodeTracker type, MetaDataRepository repos, ClassLoader loader) Constructor.PCEnhancer
(MetaDataRepository repos, ClassNodeTracker type, ClassMetaData meta) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
checkEnhancementLevel
(Class<?> cls, Log log) This static public worker method detects and logs any Entities that may have been enhanced at build time by a version of the enhancer that is older than the current version.boolean
A boolean indicating whether the enhancer should add a no-args constructor if one is not already present in the class.Gets the auxiliary enhancers registered asservices
.Return the currentBytecodeWriter
to write to or null if none.boolean
Whether the enhancer should make its arguments persistence-capable, or generate a persistence-capable subclass.The base build directory to generate code to.boolean
Whether to fail if the persistent type uses property access and bytecode analysis shows that it may be violating OpenJPA's property access restrictions.Return the bytecode representation of the managed class being manipulated.Return the metadata for the class being manipulated, or null if not a persistent type.Return the bytecode representation of the persistence-capable class being manipulated.boolean
Whether the enhancer should mutate its arguments, or just run validation and optional subclassing logic on them.getType
(ClassMetaData meta) Return the concrete type for the given class, i.e. impl for managed interfacesboolean
Whether the type that this instance is enhancing has already been redefined.boolean
Whether the type that this instance is enhancing has already been subclassed in this instance's environment classloader.static boolean
isPCSubclassName
(String className) WhetherclassName
is the name for a dynamically-created persistence-capable subclass.static void
Usage: java org.apache.openjpa.enhance.PCEnhancer [option]* <class name | .java file | .class file | .jdo file>+ Where the following options are recognized.void
record()
Write the generated bytecode.int
run()
Perform bytecode enhancements.static boolean
Run the tool.static boolean
run
(OpenJPAConfiguration conf, String[] args, PCEnhancer.Flags flags, MetaDataRepository repos, BytecodeWriter writer, ClassLoader loader) Enhance the given classes.static boolean
run
(OpenJPAConfiguration conf, String[] args, Options opts) Run the tool.void
setAddDefaultConstructor
(boolean addDefaultConstructor) A boolean indicating whether the enhancer should add a no-args constructor if one is not already present in the class.void
setBytecodeWriter
(BytecodeWriter writer) Set theBytecodeWriter
to write the bytecode to or null if none.void
setCreateSubclass
(boolean subclass) Whether the enhancer should make its arguments persistence-capable, or generate a persistence-capable subclass.void
setDirectory
(File dir) The base build directory to generate code to.void
setEnforcePropertyRestrictions
(boolean fail) Whether to fail if the persistent type uses property access and bytecode analysis shows that it may be violating OpenJPA's property access restrictions.void
setRedefine
(boolean redefine) Whether the enhancer should mutate its arguments, or just run validation and optional subclassing logic on them.static String
toManagedTypeName
(String className) IfclassName
is a dynamically-created persistence-capable subclass name, returns the name of the class that it subclasses.
-
Field Details
-
ENHANCER_VERSION
public static final int ENHANCER_VERSION -
ENHANCE_NONE
public static final int ENHANCE_NONE- See Also:
-
ENHANCE_AWARE
public static final int ENHANCE_AWARE- See Also:
-
ENHANCE_INTERFACE
public static final int ENHANCE_INTERFACE- See Also:
-
ENHANCE_PC
public static final int ENHANCE_PC- See Also:
-
PRE
- See Also:
-
ISDETACHEDSTATEDEFINITIVE
- See Also:
-
-
Constructor Details
-
PCEnhancer
Constructor. Supply configuration and type to enhance. This will look up the metadata fortype
fromconf
's repository. -
PCEnhancer
Constructor. Supply configuration and type to enhance. This will look up the metadata formeta
by converting back to a class and then loading fromconf
's repository. -
PCEnhancer
@Deprecated public PCEnhancer(OpenJPAConfiguration conf, ClassNodeTracker type, MetaDataRepository repos) Deprecated.Constructor. Supply configuration.- Parameters:
type
- the bytecode representation fo the type to enhance; this can be created from any stream or filerepos
- a metadata repository to use for metadata access, or null to create a new reporitory; the repository from the given configuration isn't used by default because the configuration might be an implementation-specific subclass whose metadata required more than just base metadata files
-
PCEnhancer
public PCEnhancer(OpenJPAConfiguration conf, ClassNodeTracker type, MetaDataRepository repos, ClassLoader loader) Constructor. Supply configuration.- Parameters:
type
- the bytecode representation fo the type to enhance; this can be created from any stream or filerepos
- a metadata repository to use for metadata access, or null to create a new reporitory; the repository from the given configuration isn't used by default because the configuration might be an implementation-specific subclass whose metadata required more than just base metadata filesloader
- the environment classloader to use for loading classes and resources.
-
PCEnhancer
Constructor. Supply repository. The repository's configuration will be used, and the metadata passed in will be used as-is without doing any additional lookups. This is useful when running the enhancer during metadata load.- Parameters:
repos
- a metadata repository to use for metadata access, or null to create a new reporitory; the repository from the given configuration isn't used by default because the configuration might be an implementation-specific subclass whose metadata required more than just base metadata filestype
- the bytecode representation fo the type to enhance; this can be created from any stream or filemeta
- the metadata to use for processing this type.- Since:
- 1.1.0
-
-
Method Details
-
isPCSubclassName
WhetherclassName
is the name for a dynamically-created persistence-capable subclass.- Since:
- 1.1.0
-
toManagedTypeName
IfclassName
is a dynamically-created persistence-capable subclass name, returns the name of the class that it subclasses. Otherwise, returnsclassName
.- Since:
- 1.1.0
-
getPCBytecode
Return the bytecode representation of the persistence-capable class being manipulated. -
getManagedTypeBytecode
Return the bytecode representation of the managed class being manipulated. This is usually the same asgetPCBytecode()
, except when running the enhancer to redefine and subclass existing persistent types. -
getMetaData
Return the metadata for the class being manipulated, or null if not a persistent type. -
getAddDefaultConstructor
public boolean getAddDefaultConstructor()A boolean indicating whether the enhancer should add a no-args constructor if one is not already present in the class. OpenJPA requires that a no-arg constructor (whether created by the compiler or by the user) be present in a PC. -
setAddDefaultConstructor
public void setAddDefaultConstructor(boolean addDefaultConstructor) A boolean indicating whether the enhancer should add a no-args constructor if one is not already present in the class. OpenJPA requires that a no-arg constructor (whether created by the compiler or by the user) be present in a PC. -
getRedefine
public boolean getRedefine()Whether the enhancer should mutate its arguments, or just run validation and optional subclassing logic on them. Usually used in conjunction withsetCreateSubclass(true)
.- Since:
- 1.0.0
-
setRedefine
public void setRedefine(boolean redefine) Whether the enhancer should mutate its arguments, or just run validation and optional subclassing logic on them. Usually used in conjunction withsetCreateSubclass(true)
.- Since:
- 1.0.0
-
isAlreadyRedefined
public boolean isAlreadyRedefined()Whether the type that this instance is enhancing has already been redefined.- Since:
- 1.0.0
-
isAlreadySubclassed
public boolean isAlreadySubclassed()Whether the type that this instance is enhancing has already been subclassed in this instance's environment classloader.- Since:
- 1.0.0
-
getCreateSubclass
public boolean getCreateSubclass()Whether the enhancer should make its arguments persistence-capable, or generate a persistence-capable subclass.- Since:
- 1.0.0
-
setCreateSubclass
public void setCreateSubclass(boolean subclass) Whether the enhancer should make its arguments persistence-capable, or generate a persistence-capable subclass.- Since:
- 1.0.0
-
getEnforcePropertyRestrictions
public boolean getEnforcePropertyRestrictions()Whether to fail if the persistent type uses property access and bytecode analysis shows that it may be violating OpenJPA's property access restrictions. -
setEnforcePropertyRestrictions
public void setEnforcePropertyRestrictions(boolean fail) Whether to fail if the persistent type uses property access and bytecode analysis shows that it may be violating OpenJPA's property access restrictions. -
getDirectory
The base build directory to generate code to. The proper package structure will be created beneath this directory. Defaults to overwriting the existing class file if null. -
setDirectory
The base build directory to generate code to. The proper package structure will be creaed beneath this directory. Defaults to overwriting the existing class file if null. -
getBytecodeWriter
Return the currentBytecodeWriter
to write to or null if none. -
setBytecodeWriter
Set theBytecodeWriter
to write the bytecode to or null if none. -
run
public int run()Perform bytecode enhancements.- Returns:
ENHANCE_*
constant
-
record
Write the generated bytecode.- Throws:
IOException
-
getAuxiliaryEnhancers
Gets the auxiliary enhancers registered asservices
. -
getType
Return the concrete type for the given class, i.e. impl for managed interfaces -
main
Usage: java org.apache.openjpa.enhance.PCEnhancer [option]* <class name | .java file | .class file | .jdo file>+ Where the following options are recognized.- -properties/-p <properties file>: The path to a OpenJPA
properties file containing information as outlined in
Configuration
; optional. - -<property name> <property value>: All bean
properties of the standard OpenJPA
OpenJPAConfiguration
can be set by using their names and supplying a value; for example: - -directory/-d <build directory>: The path to the base directory where enhanced classes are stored. By default, the enhancer overwrites the original .class file with the enhanced version. Use this option to store the generated .class file in another directory. The package structure will be created beneath the given directory.
- -addDefaultConstructor/-adc [true/t | false/f]: Whether to add a default constructor to persistent classes missing one, as opposed to throwing an exception. Defaults to true.
- -tmpClassLoader/-tcl [true/t | false/f]: Whether to load the pre-enhanced classes using a temporary class loader. Defaults to true. Set this to false when attempting to debug class loading errors.
- -enforcePropertyRestrictions/-epr [true/t | false/f]: Whether to throw an exception if a PROPERTY access entity appears to be violating standard property restrictions. Defaults to false.
- -properties/-p <properties file>: The path to a OpenJPA
properties file containing information as outlined in
-
run
Run the tool. Returns false if invalid options given. Runs against all the persistence units defined in the resource to parse. -
run
public static boolean run(OpenJPAConfiguration conf, String[] args, Options opts) throws IOException Run the tool. Returns false if invalid options given.- Throws:
IOException
-
run
public static boolean run(OpenJPAConfiguration conf, String[] args, PCEnhancer.Flags flags, MetaDataRepository repos, BytecodeWriter writer, ClassLoader loader) throws IOException Enhance the given classes.- Throws:
IOException
-
checkEnhancementLevel
This static public worker method detects and logs any Entities that may have been enhanced at build time by a version of the enhancer that is older than the current version.- Parameters:
cls
- - A non-null Class implementing org.apache.openjpa.enhance.PersistenceCapable.log
- - A non-null org.apache.openjpa.lib.log.Log.- Returns:
- true if the provided Class is down level from the current PCEnhancer.ENHANCER_VERSION. False otherwise.
-
PCEnhancer(OpenJPAConfiguration, ClassNodeTracker, MetaDataRepository, ClassLoader)
instead.