Class ClassEnhancer
- All Implemented Interfaces:
ClassFileTransformer
Entity or Persistent.
ClassEnhancer objects are thread-safe. Multiple threads may
safely call the methods of a shared ClassEnhancer object.
As described in the invalid input: '{@link <a href="../package-summary.html#bytecode">package' summary}, bytecode enhancement may be used either at runtime or offline (at build time).
To use enhancement offline, this class may be used as a main
program.
For enhancement at runtime, this class provides the low level support needed to transform class bytes during class loading. To configure runtime enhancement you may use one of the following approaches:
- The BDB
je-<version>.jarordb.jarfile may be used as an instrumentation agent as follows:java -javaagent:<BDB-JAR-FILE>=enhance:packageNames ...
packageNamesis a comma separated list of packages containing persistent classes. Sub-packages of these packages are also searched. IfpackageNamesis omitted then all packages known to the current classloader are searched.The "-v" option may be included in the comma separated list to print the name of each class that is enhanced.
- The
enhance(java.lang.String, byte[])method may be called to implement a class loader that performs enhancement. Using this approach, it is the developer's responsibility to implement and configure the class loader.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a class enhancer that searches all packages.ClassEnhancer(Set<String> packageNames) Creates a class enhancer that searches a given set of packages. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]Enhances the given class bytes if the class is annotated withEntityorPersistent.booleanGets verbose mode.static voidEnhances classes in the directories specified.static voidpremain(String args, Instrumentation inst) Enhances classes as specified by a JVM -javaagent argument.voidsetVerbose(boolean verbose) Sets verbose mode.byte[]transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.instrument.ClassFileTransformer
transform
-
Constructor Details
-
ClassEnhancer
public ClassEnhancer()Creates a class enhancer that searches all packages. -
ClassEnhancer
Creates a class enhancer that searches a given set of packages.- Parameters:
packageNames- a set of packages to search for persistent classes. Sub-packages of these packages are also searched. If empty or null, all packages known to the current classloader are searched.
-
-
Method Details
-
main
Enhances classes in the directories specified. The class files are replaced when they are enhanced, without changing the file modification date. For example:java -cp je-<version>.jar com.sleepycat.persist.model.ClassEnhancer ./classes
The "-v" argument may be specified to print the name of each class file that is enhanced. The total number of class files enhanced will always be printed.
- Parameters:
args- one or more directories containing classes to be enhanced. Subdirectories of these directories will also be searched. Optionally, -v may be included to print the name of every class file enhanced.- Throws:
Exception
-
premain
Enhances classes as specified by a JVM -javaagent argument.- See Also:
-
setVerbose
public void setVerbose(boolean verbose) Sets verbose mode.True may be specified to print the name of each class file that is enhanced. This property is false by default.
-
getVerbose
public boolean getVerbose()Gets verbose mode.- See Also:
-
transform
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) - Specified by:
transformin interfaceClassFileTransformer
-
enhance
Enhances the given class bytes if the class is annotated withEntityorPersistent.- Parameters:
className- the class name in binary format; for example, "my.package.MyClass$Name", or null if no filtering by class name should be performed.classBytes- are the class file bytes to be enhanced.- Returns:
- the enhanced bytes, or null if no enhancement was performed.
-