|
DataNucleus relies on classes implementing
PersistenceCapable
, and
Detachable
.
Users could clearly do this manually but we provide the byte-code enhancement option. The DataNucleus
Enhancer is structured to firstly determine from the input which classes are required to be enhanced,
and secondly to enhance each class using the selected
ClassEnhancer
. DataNucleus has
the
BCELClassEnhancer
(using BCEL!) and the
ASMClassEnhancer
(using ASM!).
BCEL is one of the oldest Java bytecode tools and provides much flexibility in how to modify
the class bytecode, but it isnt the fastest nor the smallest.
If you want to see the BCEL code to generate a particular class you should use the "BCELifier"
utility that comes with it. BCEL doesn't have perfect support for Java5 or later.
ASM operates in a very similar way to BCEL, however is much more lightweight and operates using the same
pattern as a SAX Parser and much faster. It uses a Visitor pattern. First the class is visited, then
fields and methods, and finally an "end" point where you can add on any new fields/methods etc.
The
ASMClassEnhancer
uses the
JdoClassVisitor
to obtain information about a class
to be enhanced and adds on all required fields/methods.
A very useful utility when developing with ASM is its "Bytecode Outline" Eclipse plugin.
To install it simply add an "Eclipse Update site" to your Eclipse config
as "http://download.forge.objectweb.org/eclipse-update/" and the name "ObjectWeb".
You then install the "Bytecode Outline" plugin. Once you have it installed select
"Window" -> "Show View" -> "Other" -> "Java : Bytecode". This provides a window showing the Java bytecode
for the class being edited. If you click on the "ASM" button on this window it shows you the ASM
commands you would need to create the class, or a particular method/field!. This makes developing
new
ASMClassMethod
implementations a doddle - just create a class with the method you want
generating and then cut and paste the ASM code in.
If you ever need to check the byte-code enhanced class for correctness you can always decompile
it back to the Java file. This can be done with a bytecode decompiler such as
JODE. Unpack the JODE download so that you have
the following
-
jode.jar
-
lib/
containing jdo.jar, datanucleus-core.jar, log4j.jar
-
classes.jar - your classes to be decompiled
and invoke the following command
java -cp classes.jar:jode.jar:lib/datanucleus-core.jar:lib/jdo.jar:lib/log4j.jar jode.decompiler.Main --dest source classes.jar
and it creates your classes under
source/
|
|