TA Enhancement At Build Time

This topic applies to Java version only. 

In the previous topic we discussed how TA can be enabled on classes while they are loaded. In this topic we will look at even more convenient and performant way of enhancing classes to support TA: during application build time.

For our example we will take the same classes as in the previous example, with the exception of TAInstrumentationRunner class, which won't be needed for build-time enhancement. Basically, we will move all the enhancing functionality of TAInstrumentationRunner into the build script. For this example we will create an ant script, which should be run after the classes (or jar) is built.

For simplistic example our build script should:

All these can be done with the following script:

Build.Xml
01<?xml version="1.0"?> 02 03<!-- 04 TA build time enhancement sample. 05--> 06 07<project name="taenhance" default="buildall"> 08 09<!-- 10 Set up the required class path for the enhancement task. 11 In a production environment, this will be composed of jars, of course. 12--> 13<path id="db4o.enhance.path"> 14 <pathelement path="${basedir}" /> 15 <fileset dir="lib"> 16 <include name="**/*.jar"/> 17 </fileset> 18</path> 19 20<!-- Define enhancement task. --> 21<taskdef 22 name="db4o-enhance" 23 classname="com.db4o.instrumentation.ant.Db4oFileEnhancerAntTask" 24 classpathref="db4o.enhance.path" 25 loaderref="db4o.enhance.loader" /> 26 27<typedef 28 name="transparent-activation" 29 classname="com.db4o.ta.instrumentation.ant.TAAntClassEditFactory" 30 classpathref="db4o.enhance.path" 31 loaderref="db4o.enhance.loader" /> 32 33 34 35<target name="buildall"> 36 37 <!-- Create enhanced output directory--> 38 <mkdir dir="${basedir}/enhanced-bin" /> 39 <delete dir="${basedir}/enhanced-bin" quiet = "true"> 40 <include name="**/*"/> 41 </delete> 42 43 <db4o-enhance classTargetDir="${basedir}/enhanced-bin" jarTargetDir="${basedir}/enhanced-lib"> 44 45 <classpath refid="db4o.enhance.path" /> 46 <!-- Use compiled classes as an input --> 47 <sources dir="${basedir}/bin" /> 48 49 <!-- Call transparent activation enhancement --> 50 <transparent-activation /> 51 52 </db4o-enhance> 53 54</target> 55 56 57 58</project>

In order to test this script:

Successfully executed build script will produce an instrumented copy of the project classes in enhanced-bin folder. You can check the results by running the following batch file from bin and enhanced-bin folders:

set CLASSPATH=.;{$PROJECT_ROOT}\lib\db4o-x.x-java5.jar

java com.db4odoc.taexamples.enhancer.TAInstrumentationExample

(In enhanced version the warning about classes that do not support TA should disappear).

Of course, the presented example is very simple and limited in functionality. In fact you can do a lot more things using the build script:

o       Add NQ optimization in the same enhancer task

o       Use ClassFilter to select classes for enhancement

o       Use regex to select classes for enhancement

o       Use several source folders

o       Use jar as the source for enhancement

An example of the above features can be found in our Project Spaces.