samples/componentinjection/anagram-modular/src-api-compiletimecaches/org/apidesign/anagram/impl/annotations/WordsProcessor.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 20 Jun 2009 16:06:19 +0200
changeset 336 219810ff3c72
permissions -rw-r--r--
Example of "compile time caches" and their possible usage in Component Injection area
jtulach@336
     1
/*
jtulach@336
     2
 * To change this template, choose Tools | Templates
jtulach@336
     3
 * and open the template in the editor.
jtulach@336
     4
 */
jtulach@336
     5
jtulach@336
     6
package org.apidesign.anagram.impl.annotations;
jtulach@336
     7
jtulach@336
     8
import java.util.Set;
jtulach@336
     9
import javax.annotation.processing.Processor;
jtulach@336
    10
import javax.annotation.processing.RoundEnvironment;
jtulach@336
    11
import javax.annotation.processing.SupportedAnnotationTypes;
jtulach@336
    12
import javax.annotation.processing.SupportedSourceVersion;
jtulach@336
    13
import javax.lang.model.SourceVersion;
jtulach@336
    14
import javax.lang.model.element.Element;
jtulach@336
    15
import javax.lang.model.element.TypeElement;
jtulach@336
    16
import javax.lang.model.util.Elements;
jtulach@336
    17
import org.apidesign.anagram.api.annotations.Words;
jtulach@336
    18
import org.openide.filesystems.annotations.LayerBuilder.File;
jtulach@336
    19
import org.openide.filesystems.annotations.LayerGeneratingProcessor;
jtulach@336
    20
import org.openide.filesystems.annotations.LayerGenerationException;
jtulach@336
    21
import org.openide.util.lookup.ServiceProvider;
jtulach@336
    22
jtulach@336
    23
/** Compile time caches example. Processor that is triggered
jtulach@336
    24
 * during compilation when a <code>@Words</code> annotations is found.
jtulach@336
    25
 * It generates some declarative registrations, so the annotated object
jtulach@336
    26
 * is found later during runtime, but not instantiated before it is really
jtulach@336
    27
 * needed.
jtulach@336
    28
 *
jtulach@336
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@336
    30
 */
jtulach@336
    31
// BEGIN: anagram.api.WordsProcessor
jtulach@336
    32
@ServiceProvider(service=Processor.class)
jtulach@336
    33
@SupportedAnnotationTypes("org.apidesign.anagram.api.annotations.Words")
jtulach@336
    34
@SupportedSourceVersion(SourceVersion.RELEASE_5)
jtulach@336
    35
public class WordsProcessor extends LayerGeneratingProcessor {
jtulach@336
    36
jtulach@336
    37
    @Override
jtulach@336
    38
    protected boolean handleProcess(
jtulach@336
    39
        Set<? extends TypeElement> set, RoundEnvironment env
jtulach@336
    40
    ) throws LayerGenerationException {
jtulach@336
    41
        Elements elements = processingEnv.getElementUtils();
jtulach@336
    42
jtulach@336
    43
        for (Element e : env.getElementsAnnotatedWith(Words.class)) {
jtulach@336
    44
            Words w = e.getAnnotation(Words.class);
jtulach@336
    45
jtulach@336
    46
            TypeElement te = (TypeElement)e.getEnclosingElement();
jtulach@336
    47
            String teName = elements.getBinaryName(te).toString();
jtulach@336
    48
// FINISH: anagram.api.WordsProcessor
jtulach@336
    49
jtulach@336
    50
// BEGIN: anagram.api.WordsProcessorLayer
jtulach@336
    51
            File f = layer(e).file(
jtulach@336
    52
                "Services/" + teName.replace('.', '-') + ".instance"
jtulach@336
    53
            );
jtulach@336
    54
            f.methodvalue(
jtulach@336
    55
                "instanceCreate",
jtulach@336
    56
                "org.apidesign.anagram.impl.annotations.WordsImpl",
jtulach@336
    57
                "create"
jtulach@336
    58
            );
jtulach@336
    59
            f.stringvalue(
jtulach@336
    60
                "instanceClass",
jtulach@336
    61
                "org.apidesign.anagram.impl.annotations.WordsImpl"
jtulach@336
    62
            );
jtulach@336
    63
            f.stringvalue(
jtulach@336
    64
                "instanceOf",
jtulach@336
    65
                "org.apidesign.anagram.api.WordLibrary"
jtulach@336
    66
            );
jtulach@336
    67
            f.methodvalue(
jtulach@336
    68
                "words",
jtulach@336
    69
                teName,
jtulach@336
    70
                e.getSimpleName().toString()
jtulach@336
    71
            );
jtulach@336
    72
            f.write();
jtulach@336
    73
// END: anagram.api.WordsProcessorLayer
jtulach@336
    74
        }
jtulach@336
    75
jtulach@336
    76
        return true;
jtulach@336
    77
    }
jtulach@336
    78
jtulach@336
    79
}