samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/impl/annotations/WordsProcessor.java
changeset 336 219810ff3c72
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/impl/annotations/WordsProcessor.java	Sat Jun 20 16:06:19 2009 +0200
     1.3 @@ -0,0 +1,79 @@
     1.4 +/*
     1.5 + * To change this template, choose Tools | Templates
     1.6 + * and open the template in the editor.
     1.7 + */
     1.8 +
     1.9 +package org.apidesign.anagram.impl.annotations;
    1.10 +
    1.11 +import java.util.Set;
    1.12 +import javax.annotation.processing.Processor;
    1.13 +import javax.annotation.processing.RoundEnvironment;
    1.14 +import javax.annotation.processing.SupportedAnnotationTypes;
    1.15 +import javax.annotation.processing.SupportedSourceVersion;
    1.16 +import javax.lang.model.SourceVersion;
    1.17 +import javax.lang.model.element.Element;
    1.18 +import javax.lang.model.element.TypeElement;
    1.19 +import javax.lang.model.util.Elements;
    1.20 +import org.apidesign.anagram.api.annotations.Words;
    1.21 +import org.openide.filesystems.annotations.LayerBuilder.File;
    1.22 +import org.openide.filesystems.annotations.LayerGeneratingProcessor;
    1.23 +import org.openide.filesystems.annotations.LayerGenerationException;
    1.24 +import org.openide.util.lookup.ServiceProvider;
    1.25 +
    1.26 +/** Compile time caches example. Processor that is triggered
    1.27 + * during compilation when a <code>@Words</code> annotations is found.
    1.28 + * It generates some declarative registrations, so the annotated object
    1.29 + * is found later during runtime, but not instantiated before it is really
    1.30 + * needed.
    1.31 + *
    1.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.33 + */
    1.34 +// BEGIN: anagram.api.WordsProcessor
    1.35 +@ServiceProvider(service=Processor.class)
    1.36 +@SupportedAnnotationTypes("org.apidesign.anagram.api.annotations.Words")
    1.37 +@SupportedSourceVersion(SourceVersion.RELEASE_5)
    1.38 +public class WordsProcessor extends LayerGeneratingProcessor {
    1.39 +
    1.40 +    @Override
    1.41 +    protected boolean handleProcess(
    1.42 +        Set<? extends TypeElement> set, RoundEnvironment env
    1.43 +    ) throws LayerGenerationException {
    1.44 +        Elements elements = processingEnv.getElementUtils();
    1.45 +
    1.46 +        for (Element e : env.getElementsAnnotatedWith(Words.class)) {
    1.47 +            Words w = e.getAnnotation(Words.class);
    1.48 +
    1.49 +            TypeElement te = (TypeElement)e.getEnclosingElement();
    1.50 +            String teName = elements.getBinaryName(te).toString();
    1.51 +// FINISH: anagram.api.WordsProcessor
    1.52 +
    1.53 +// BEGIN: anagram.api.WordsProcessorLayer
    1.54 +            File f = layer(e).file(
    1.55 +                "Services/" + teName.replace('.', '-') + ".instance"
    1.56 +            );
    1.57 +            f.methodvalue(
    1.58 +                "instanceCreate",
    1.59 +                "org.apidesign.anagram.impl.annotations.WordsImpl",
    1.60 +                "create"
    1.61 +            );
    1.62 +            f.stringvalue(
    1.63 +                "instanceClass",
    1.64 +                "org.apidesign.anagram.impl.annotations.WordsImpl"
    1.65 +            );
    1.66 +            f.stringvalue(
    1.67 +                "instanceOf",
    1.68 +                "org.apidesign.anagram.api.WordLibrary"
    1.69 +            );
    1.70 +            f.methodvalue(
    1.71 +                "words",
    1.72 +                teName,
    1.73 +                e.getSimpleName().toString()
    1.74 +            );
    1.75 +            f.write();
    1.76 +// END: anagram.api.WordsProcessorLayer
    1.77 +        }
    1.78 +
    1.79 +        return true;
    1.80 +    }
    1.81 +
    1.82 +}