Example of "compile time caches" and their possible usage in Component Injection area
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 20 Jun 2009 16:06:19 +0200
changeset 336219810ff3c72
parent 335 3a98792518f0
child 337 d5b6a877e5a8
Example of "compile time caches" and their possible usage in Component Injection area
samples/componentinjection/anagram-modular/build.xml
samples/componentinjection/anagram-modular/nbproject/project.xml
samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/api/annotations/Words.java
samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/impl/annotations/WordsImpl.java
samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/impl/annotations/WordsProcessor.java
samples/componentinjection/anagram-modular/src-app-lookup/org/apidesign/anagram/app/lookup/Main.java
samples/componentinjection/anagram-modular/src-word-annotated/org/apidesign/anagram/wordannotated/WordsFactory.java
     1.1 --- a/samples/componentinjection/anagram-modular/build.xml	Tue May 26 09:01:40 2009 +0200
     1.2 +++ b/samples/componentinjection/anagram-modular/build.xml	Sat Jun 20 16:06:19 2009 +0200
     1.3 @@ -1,5 +1,5 @@
     1.4  <?xml version="1.0" encoding="UTF-8"?>
     1.5 -<project name="Build Script" default="test" basedir=".">
     1.6 +<project name="Various Examples of Component Injection" default="test" basedir=".">
     1.7      <target name="clean">
     1.8          <delete dir="build"/>
     1.9      </target>
    1.10 @@ -11,6 +11,14 @@
    1.11              <param name="cp" value=""/>
    1.12          </antcall>
    1.13          <antcall target="-build-one">
    1.14 +            <param name="version" value="api-compiletimecaches"/>
    1.15 +            <param name="cp" value="build/api/classes:../../libs/dist/org-openide-util.jar:../../libs/dist/org-openide-filesystems.jar"/>
    1.16 +        </antcall>
    1.17 +        <antcall target="-build-one">
    1.18 +            <param name="version" value="word-annotated"/>
    1.19 +            <param name="cp" value="build/api/classes:build/api-compiletimecaches/classes:../../libs/dist/org-openide-util.jar:../../libs/dist/org-openide-filesystems.jar"/>
    1.20 +        </antcall>
    1.21 +        <antcall target="-build-one">
    1.22              <param name="version" value="word-static"/>
    1.23          </antcall>
    1.24          <antcall target="-build-one">
    1.25 @@ -96,6 +104,12 @@
    1.26              <param name="cp" value="build/api/classes:build/word-static/classes:build/scrambler-simple/classes:build/gui/classes:${junit.jar}:${nbjunit.jar}:${insane.jar}"/>
    1.27              <param name="main" value="org.apidesign.anagram.app.serviceloader.Main"/>
    1.28          </antcall>
    1.29 +        <echo level="info" message="Running @Words annotation version"/>
    1.30 +        <antcall target="-run-one">
    1.31 +            <param name="version" value="app-lookup"/>
    1.32 +            <param name="cp" value="build/api/classes:build/word-annotated/classes:build/api-compiletimecaches/classes:build/scrambler-simple/classes:build/gui/classes:../../libs/dist/org-openide-util.jar:../../libs/dist/org-openide-filesystems.jar"/>
    1.33 +            <param name="main" value="org.apidesign.anagram.app.lookup.Main"/>
    1.34 +        </antcall>
    1.35      </target>
    1.36  
    1.37      <target name="test" depends="build">
     2.1 --- a/samples/componentinjection/anagram-modular/nbproject/project.xml	Tue May 26 09:01:40 2009 +0200
     2.2 +++ b/samples/componentinjection/anagram-modular/nbproject/project.xml	Sat Jun 20 16:06:19 2009 +0200
     2.3 @@ -21,6 +21,18 @@
     2.4                      <encoding>UTF-8</encoding>
     2.5                  </source-folder>
     2.6                  <source-folder>
     2.7 +                    <label>src-api-compiletimecaches</label>
     2.8 +                    <type>java</type>
     2.9 +                    <location>src-api-compiletimecaches</location>
    2.10 +                    <encoding>UTF-8</encoding>
    2.11 +                </source-folder>
    2.12 +                <source-folder>
    2.13 +                    <label>src-word-annotated</label>
    2.14 +                    <type>java</type>
    2.15 +                    <location>src-word-annotated</location>
    2.16 +                    <encoding>UTF-8</encoding>
    2.17 +                </source-folder>
    2.18 +                <source-folder>
    2.19                      <label>src-word-static</label>
    2.20                      <type>java</type>
    2.21                      <location>src-word-static</location>
    2.22 @@ -123,6 +135,16 @@
    2.23              </export>
    2.24              <export>
    2.25                  <type>folder</type>
    2.26 +                <location>build/api-compiletimecaches/classes</location>
    2.27 +                <build-target>build</build-target>
    2.28 +            </export>
    2.29 +            <export>
    2.30 +                <type>folder</type>
    2.31 +                <location>build/word-annotated/classes</location>
    2.32 +                <build-target>build</build-target>
    2.33 +            </export>
    2.34 +            <export>
    2.35 +                <type>folder</type>
    2.36                  <location>build/word-static/classes</location>
    2.37                  <build-target>build</build-target>
    2.38              </export>
    2.39 @@ -178,6 +200,14 @@
    2.40                          <location>src-api</location>
    2.41                      </source-folder>
    2.42                      <source-folder style="packages">
    2.43 +                        <label>src-api-compiletimecaches</label>
    2.44 +                        <location>src-api-compiletimecaches</location>
    2.45 +                    </source-folder>
    2.46 +                    <source-folder style="packages">
    2.47 +                        <label>src-word-annotated</label>
    2.48 +                        <location>src-word-annotated</location>
    2.49 +                    </source-folder>
    2.50 +                    <source-folder style="packages">
    2.51                          <label>src-word-static</label>
    2.52                          <location>src-word-static</location>
    2.53                      </source-folder>
    2.54 @@ -243,6 +273,18 @@
    2.55                  <source-level>1.5</source-level>
    2.56              </compilation-unit>
    2.57              <compilation-unit>
    2.58 +                <package-root>src-api-compiletimecaches</package-root>
    2.59 +                <classpath mode="compile">src-api:../../libs/dist/org-openide-filesystems.jar:../../libs/dist/org-openide-util.jar</classpath>
    2.60 +                <built-to>build/api-compiletimecaches/classes</built-to>
    2.61 +                <source-level>1.5</source-level>
    2.62 +            </compilation-unit>
    2.63 +            <compilation-unit>
    2.64 +                <package-root>src-word-annotated</package-root>
    2.65 +                <classpath mode="compile">src-api:src-api-compiletimecaches:../../libs/dist/org-openide-filesystems.jar:../../libs/dist/org-openide-util.jar</classpath>
    2.66 +                <built-to>build/word-annotated/classes</built-to>
    2.67 +                <source-level>1.5</source-level>
    2.68 +            </compilation-unit>
    2.69 +            <compilation-unit>
    2.70                  <package-root>src-word-static</package-root>
    2.71                  <classpath mode="compile">src-api</classpath>
    2.72                  <built-to>build/word-static/classes</built-to>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/api/annotations/Words.java	Sat Jun 20 16:06:19 2009 +0200
     3.3 @@ -0,0 +1,28 @@
     3.4 +/*
     3.5 + * To change this template, choose Tools | Templates
     3.6 + * and open the template in the editor.
     3.7 + */
     3.8 +
     3.9 +package org.apidesign.anagram.api.annotations;
    3.10 +
    3.11 +import java.lang.annotation.ElementType;
    3.12 +import java.lang.annotation.Retention;
    3.13 +import java.lang.annotation.RetentionPolicy;
    3.14 +import java.lang.annotation.Target;
    3.15 +import org.apidesign.anagram.api.WordLibrary;
    3.16 +
    3.17 +/**
    3.18 + * Annotations to mark a static method returning array of Strings with.
    3.19 + * Such method is then treated as a provider of {@link WordLibrary}.
    3.20 + * <p>
    3.21 + * Its retention is set to source one, as it is
    3.22 + * processed by associated WordsProcessor during compile time.
    3.23 + *
    3.24 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.25 + */
    3.26 +// BEGIN: anagram.api.Words
    3.27 +@Retention(RetentionPolicy.SOURCE)
    3.28 +@Target(ElementType.METHOD)
    3.29 +public @interface Words {
    3.30 +}
    3.31 +// END: anagram.api.Words
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/impl/annotations/WordsImpl.java	Sat Jun 20 16:06:19 2009 +0200
     4.3 @@ -0,0 +1,31 @@
     4.4 +/*
     4.5 + * To change this template, choose Tools | Templates
     4.6 + * and open the template in the editor.
     4.7 + */
     4.8 +
     4.9 +package org.apidesign.anagram.impl.annotations;
    4.10 +
    4.11 +import java.util.Map;
    4.12 +import org.apidesign.anagram.api.WordLibrary;
    4.13 +
    4.14 +/**
    4.15 + *
    4.16 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.17 + */
    4.18 +// BEGIN: anagram.api.WordsImpl
    4.19 +public class WordsImpl implements WordLibrary {
    4.20 +    private Map<String,Object> map;
    4.21 +
    4.22 +    private WordsImpl(Map<String,Object> m) {
    4.23 +        this.map = m;
    4.24 +    }
    4.25 +
    4.26 +    public static WordsImpl create(Map attributes) {
    4.27 +        return new WordsImpl(attributes);
    4.28 +    }
    4.29 +
    4.30 +    public String[] getWords() {
    4.31 +        return (String[])map.get("words"); // NOI18N
    4.32 +    }
    4.33 +}
    4.34 +// END: anagram.api.WordsImpl
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/impl/annotations/WordsProcessor.java	Sat Jun 20 16:06:19 2009 +0200
     5.3 @@ -0,0 +1,79 @@
     5.4 +/*
     5.5 + * To change this template, choose Tools | Templates
     5.6 + * and open the template in the editor.
     5.7 + */
     5.8 +
     5.9 +package org.apidesign.anagram.impl.annotations;
    5.10 +
    5.11 +import java.util.Set;
    5.12 +import javax.annotation.processing.Processor;
    5.13 +import javax.annotation.processing.RoundEnvironment;
    5.14 +import javax.annotation.processing.SupportedAnnotationTypes;
    5.15 +import javax.annotation.processing.SupportedSourceVersion;
    5.16 +import javax.lang.model.SourceVersion;
    5.17 +import javax.lang.model.element.Element;
    5.18 +import javax.lang.model.element.TypeElement;
    5.19 +import javax.lang.model.util.Elements;
    5.20 +import org.apidesign.anagram.api.annotations.Words;
    5.21 +import org.openide.filesystems.annotations.LayerBuilder.File;
    5.22 +import org.openide.filesystems.annotations.LayerGeneratingProcessor;
    5.23 +import org.openide.filesystems.annotations.LayerGenerationException;
    5.24 +import org.openide.util.lookup.ServiceProvider;
    5.25 +
    5.26 +/** Compile time caches example. Processor that is triggered
    5.27 + * during compilation when a <code>@Words</code> annotations is found.
    5.28 + * It generates some declarative registrations, so the annotated object
    5.29 + * is found later during runtime, but not instantiated before it is really
    5.30 + * needed.
    5.31 + *
    5.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.33 + */
    5.34 +// BEGIN: anagram.api.WordsProcessor
    5.35 +@ServiceProvider(service=Processor.class)
    5.36 +@SupportedAnnotationTypes("org.apidesign.anagram.api.annotations.Words")
    5.37 +@SupportedSourceVersion(SourceVersion.RELEASE_5)
    5.38 +public class WordsProcessor extends LayerGeneratingProcessor {
    5.39 +
    5.40 +    @Override
    5.41 +    protected boolean handleProcess(
    5.42 +        Set<? extends TypeElement> set, RoundEnvironment env
    5.43 +    ) throws LayerGenerationException {
    5.44 +        Elements elements = processingEnv.getElementUtils();
    5.45 +
    5.46 +        for (Element e : env.getElementsAnnotatedWith(Words.class)) {
    5.47 +            Words w = e.getAnnotation(Words.class);
    5.48 +
    5.49 +            TypeElement te = (TypeElement)e.getEnclosingElement();
    5.50 +            String teName = elements.getBinaryName(te).toString();
    5.51 +// FINISH: anagram.api.WordsProcessor
    5.52 +
    5.53 +// BEGIN: anagram.api.WordsProcessorLayer
    5.54 +            File f = layer(e).file(
    5.55 +                "Services/" + teName.replace('.', '-') + ".instance"
    5.56 +            );
    5.57 +            f.methodvalue(
    5.58 +                "instanceCreate",
    5.59 +                "org.apidesign.anagram.impl.annotations.WordsImpl",
    5.60 +                "create"
    5.61 +            );
    5.62 +            f.stringvalue(
    5.63 +                "instanceClass",
    5.64 +                "org.apidesign.anagram.impl.annotations.WordsImpl"
    5.65 +            );
    5.66 +            f.stringvalue(
    5.67 +                "instanceOf",
    5.68 +                "org.apidesign.anagram.api.WordLibrary"
    5.69 +            );
    5.70 +            f.methodvalue(
    5.71 +                "words",
    5.72 +                teName,
    5.73 +                e.getSimpleName().toString()
    5.74 +            );
    5.75 +            f.write();
    5.76 +// END: anagram.api.WordsProcessorLayer
    5.77 +        }
    5.78 +
    5.79 +        return true;
    5.80 +    }
    5.81 +
    5.82 +}
     6.1 --- a/samples/componentinjection/anagram-modular/src-app-lookup/org/apidesign/anagram/app/lookup/Main.java	Tue May 26 09:01:40 2009 +0200
     6.2 +++ b/samples/componentinjection/anagram-modular/src-app-lookup/org/apidesign/anagram/app/lookup/Main.java	Sat Jun 20 16:06:19 2009 +0200
     6.3 @@ -6,6 +6,10 @@
     6.4      private Main() { }
     6.5      
     6.6      public static void main(String[] args) throws Exception {
     6.7 +        // BEGIN: anagram.words.annotation.layersetup
     6.8 +        System.setProperty("org.openide.util.Lookup.paths", "Services");
     6.9 +        // END: anagram.words.annotation.layersetup
    6.10 +
    6.11          UI ui = new AnagramsWithLookup();
    6.12          ui.display();
    6.13      }
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/samples/componentinjection/anagram-modular/src-word-annotated/org/apidesign/anagram/wordannotated/WordsFactory.java	Sat Jun 20 16:06:19 2009 +0200
     7.3 @@ -0,0 +1,19 @@
     7.4 +
     7.5 +package org.apidesign.anagram.wordannotated;
     7.6 +
     7.7 +import org.apidesign.anagram.api.annotations.Words;
     7.8 +
     7.9 +// BEGIN: anagram.words.annotation.usage
    7.10 +public class WordsFactory {
    7.11 +    @Words
    7.12 +    public static String[] myWords() {
    7.13 +        return new String[] {
    7.14 +            "microprocessor",
    7.15 +            "navigation",
    7.16 +            "optimization",
    7.17 +            "parameter",
    7.18 +            "patrick",
    7.19 +        };
    7.20 +    }
    7.21 +}
    7.22 +// END: anagram.words.annotation.usage