# HG changeset patch # User Jaroslav Tulach # Date 1275386927 -7200 # Node ID d1e7424dc98870e309fca2780fcee80ef57c73e4 # Parent f77d863ac962714c46a1e9c660416098e19d9a9f Example of Annotation Processor that generates a db access class diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/build.xml Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project livedb. + + + diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/nbproject/build-impl.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/nbproject/build-impl.xml Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,883 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/nbproject/genfiles.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/nbproject/genfiles.properties Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,8 @@ +build.xml.data.CRC32=acb473e7 +build.xml.script.CRC32=61ba5a6a +build.xml.stylesheet.CRC32=958a1d3e@1.38.0.45 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=acb473e7 +nbproject/build-impl.xml.script.CRC32=c82572ef +nbproject/build-impl.xml.stylesheet.CRC32=c75ce636@1.38.0.45 diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/nbproject/project.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/nbproject/project.properties Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=true +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=livedb +application.vendor=jarda +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/livedb.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.junit-4.4.jar=../libs/dist/junit-4.4.jar +file.reference.org-openide-util-lookup.jar=../libs/dist/org-openide-util-lookup.jar +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.org-openide-util-lookup.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${file.reference.junit-4.4.jar} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/nbproject/project.xml Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + livedb + + + + + + + + + diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/src/org/apidesign/livedb/LiveDB.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/src/org/apidesign/livedb/LiveDB.java Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,17 @@ +package org.apidesign.livedb; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author Jaroslav Tulach + */ +@Target(ElementType.PACKAGE) +@Retention(RetentionPolicy.SOURCE) +public @interface LiveDB { + String classname(); + String field(); +} diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/src/org/apidesign/livedb/impl/LiveDBProcessor.java Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,53 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.apidesign.livedb.impl; + +import java.io.IOException; +import java.io.Writer; +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.tools.JavaFileObject; +import org.apidesign.livedb.LiveDB; +import org.openide.util.lookup.ServiceProvider; + +/** + * + * @author Jaroslav Tulach + */ +@SupportedAnnotationTypes("org.apidesign.livedb.LiveDB") +@SupportedSourceVersion(SourceVersion.RELEASE_6) +@ServiceProvider(service=Processor.class) +public class LiveDBProcessor extends AbstractProcessor { + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + for (Element e : roundEnv.getElementsAnnotatedWith(LiveDB.class)) { + LiveDB db = e.getAnnotation(LiveDB.class); + PackageElement pe = (PackageElement)e; + String clsName = pe.getQualifiedName().toString() + "." + db.classname(); + try { + JavaFileObject src = processingEnv.getFiler().createSourceFile(clsName, pe); + Writer w = src.openWriter(); + w.append("package " + pe.getQualifiedName() + ";\n"); + w.append("class " + db.classname() + " {\n"); + w.append(" public String " + db.field() + ";\n"); + w.append("}"); + w.close(); + } catch (IOException ex) { + throw new IllegalStateException(ex); + } + } + return true; + } + +} diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/test/org/apidesign/livedb/example/LiveDBTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/test/org/apidesign/livedb/example/LiveDBTest.java Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,26 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.apidesign.livedb.example; + +import junit.framework.TestCase; + +/** + * + * @author Jaroslav Tulach + */ +public class LiveDBTest extends TestCase { + + public LiveDBTest(String testName) { + super(testName); + } + + public void testSomeMethod() { + DBAccess db = new DBAccess(); + db.jarda = "Ahoj"; + assertEquals("Ahoj", db.jarda); + } + +} diff -r f77d863ac962 -r d1e7424dc988 samples/livedb/test/org/apidesign/livedb/example/package-info.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/livedb/test/org/apidesign/livedb/example/package-info.java Tue Jun 01 12:08:47 2010 +0200 @@ -0,0 +1,6 @@ + + +@LiveDB(classname="DBAccess", field="jarda") +package org.apidesign.livedb.example; + +import org.apidesign.livedb.LiveDB;