Annotation processors may be used to generate codes.
http://deors.wordpress.com/2011/09/26/annotation-types/
http://deors.wordpress.com/2011/10/08/annotation-processors/
http://deors.wordpress.com/2011/10/31/annotation-generators/
Packaging:
Add dependent jars also to factory path
(refers to Processor JavaDoc)
Each round:
MDE
Filer (javax.annotation.processing.Filer)
(bad) Sample: generating code
if (e.getKind() == ElementKind.CLASS) {
TypeElement classElement = (TypeElement) e;
PackageElement packageElement =
(PackageElement) classElement.getEnclosingElement();
JavaFileObject jfo = processingEnv.getFiler().createSourceFile(
classElement.getQualifiedName() + "BeanInfo");
BufferedWriter bw = new BufferedWriter(jfo.openWriter());
bw.append("package ");
bw.append(packageElement.getQualifiedName());
bw.append(";");
bw.newLine();
bw.newLine();
// rest of generated class contents
Using Apache Velocity
the approach:
(samples at http://deors.wordpress.com/2011/10/31/annotation-generators/)
Oracle JDK javac 1.7.0_51
Eclipse annotation processor feature is only suitable for running annotation processors, not developing and debugging.
To develop and debug,
to achieve (1), I created a shell script. Note DEBUGJAVAC is not used, which is to be put in Eclipse external tools environment. To create Eclipse external tools entry, "Run -> External Tools -> External Tools Configurations"
to achieve (2), "Project (context menu) -> debug -> debug configurations"
#!/bin/sh
# usage: debug.sh [-verbose]
export CURPATH=`pwd`
cd ..
# Project base
export PROJBASE="."
# Project MDE(Annotation) build target
export PROJANNOBIN="$PROJBASE/target/mdebin"
# Folder to place the generated source codes
export GENTARGET="$PROJBASE/target/generated-sources/mde"
# Common annotation processor project
export ANNOPROJ="/home/bing/git/EastIPAnnotationProcessor"
# A temp folder, to place the compiled classes
export TEMP="/home/bing/tmp/annotationProc"
# The generated Annotation Processor jar file
export JARFILE="$TEMP/AnnotationProcessor.jar"
# Other dependencies
export CLASSPATH_DEP="/home/bing/Software_n_Docs/Development/Velocity/velocity-1.7/velocity-1.7.jar:/home/bing/Software_n_Docs/Development/Velocity/velocity-1.7/velocity-1.7-dep.jar:/home/bing/.m2/repository/javax/javaee-api/6.0/javaee-api-6.0.jar"
# path to the source to be compiled (processed)
export SOURCEPATH="/home/bing/workspace/VaadinJeeIntestEjbBackend/ejbModule"
###################################################################################
export CLASSPATH="$JARFILE:$CLASSPATH_DEP"
echo "CLASSPATH=$CLASSPATH"
export WARNINGLEVEL=""
# export WARNINGLEVEL="-Xlint:-cast,-classfile,-deprecation,-dep-ann,-divzero,-empty,-fallthrough,-finally,-options,-overrides,-path,processing,-rawtypes,-serial,-static,-try,-unchecked,-varargs"
# DEBUGJAVAC can be put into Eclipse External Tool environment, for command line, just run
# export DEBUGJAVAC="-J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
rm -fr $TEMP/*
rm -fr $GENTARGET/*
if [ -f $JARFILE ];then
rm -f $JARFILE;
fi
jar -cf $JARFILE -C $ANNOPROJ/bin .
jar -uf $JARFILE -C $PROJANNOBIN .
for p in `cat MDEProcessorList`; do
echo "Processor: $p"
echo "--------------------------------------------------------------------------------------"
for i in `cat MDETargetList`;do
echo "Compiling $i"
javac $DEBUGJAVAC -XprintRounds -XprintProcessorInfo -proc:only -processor $p $WARNINGLEVEL -g $1 -d $TEMP -s $GENTARGET -sourcepath $SOURCEPATH $i
echo "Compiling done."
done
done
echo "**** Checking temp *****"
ls $TEMP
echo "**** Checking test/generated *****"
ls $GENTARGET
cd $CURPATH