|
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
ASMClassEnhancer
(using ASM!) and the
BCELClassEnhancer
(using BCEL!).
The default is ASM, and the strategic direction is to drop support for BCEL at some point.
BCEL is not actively maintained and doesn't support more advanced Java language features fully.
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.
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.
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
-
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
jode.decompiler.Main --dest source classes.jar
and it creates your classes under
source/
|
|