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