jtulach@336: /*
jtulach@336: * To change this template, choose Tools | Templates
jtulach@336: * and open the template in the editor.
jtulach@336: */
jtulach@336:
jtulach@336: package org.apidesign.anagram.impl.annotations;
jtulach@336:
jtulach@336: import java.util.Set;
jtulach@336: import javax.annotation.processing.Processor;
jtulach@336: import javax.annotation.processing.RoundEnvironment;
jtulach@336: import javax.annotation.processing.SupportedAnnotationTypes;
jtulach@336: import javax.annotation.processing.SupportedSourceVersion;
jtulach@336: import javax.lang.model.SourceVersion;
jtulach@336: import javax.lang.model.element.Element;
jtulach@336: import javax.lang.model.element.TypeElement;
jtulach@336: import javax.lang.model.util.Elements;
jtulach@336: import org.apidesign.anagram.api.annotations.Words;
jtulach@336: import org.openide.filesystems.annotations.LayerBuilder.File;
jtulach@336: import org.openide.filesystems.annotations.LayerGeneratingProcessor;
jtulach@336: import org.openide.filesystems.annotations.LayerGenerationException;
jtulach@336: import org.openide.util.lookup.ServiceProvider;
jtulach@336:
jtulach@336: /** Compile time caches example. Processor that is triggered
jtulach@336: * during compilation when a @Words
annotations is found.
jtulach@336: * It generates some declarative registrations, so the annotated object
jtulach@336: * is found later during runtime, but not instantiated before it is really
jtulach@336: * needed.
jtulach@336: *
jtulach@336: * @author Jaroslav Tulach
jtulach@336: */
jtulach@336: // BEGIN: anagram.api.WordsProcessor
jtulach@336: @ServiceProvider(service=Processor.class)
jtulach@336: @SupportedAnnotationTypes("org.apidesign.anagram.api.annotations.Words")
jtulach@336: @SupportedSourceVersion(SourceVersion.RELEASE_5)
jtulach@336: public class WordsProcessor extends LayerGeneratingProcessor {
jtulach@336:
jtulach@336: @Override
jtulach@336: protected boolean handleProcess(
jtulach@336: Set extends TypeElement> set, RoundEnvironment env
jtulach@336: ) throws LayerGenerationException {
jtulach@336: Elements elements = processingEnv.getElementUtils();
jtulach@336:
jtulach@336: for (Element e : env.getElementsAnnotatedWith(Words.class)) {
jtulach@336: Words w = e.getAnnotation(Words.class);
jtulach@336:
jtulach@336: TypeElement te = (TypeElement)e.getEnclosingElement();
jtulach@336: String teName = elements.getBinaryName(te).toString();
jtulach@336: // FINISH: anagram.api.WordsProcessor
jtulach@336:
jtulach@336: // BEGIN: anagram.api.WordsProcessorLayer
jtulach@336: File f = layer(e).file(
jtulach@336: "Services/" + teName.replace('.', '-') + ".instance"
jtulach@336: );
jtulach@336: f.methodvalue(
jtulach@336: "instanceCreate",
jtulach@336: "org.apidesign.anagram.impl.annotations.WordsImpl",
jtulach@336: "create"
jtulach@336: );
jtulach@336: f.stringvalue(
jtulach@336: "instanceClass",
jtulach@336: "org.apidesign.anagram.impl.annotations.WordsImpl"
jtulach@336: );
jtulach@336: f.stringvalue(
jtulach@336: "instanceOf",
jtulach@336: "org.apidesign.anagram.api.WordLibrary"
jtulach@336: );
jtulach@336: f.methodvalue(
jtulach@336: "words",
jtulach@336: teName,
jtulach@336: e.getSimpleName().toString()
jtulach@336: );
jtulach@336: f.write();
jtulach@336: // END: anagram.api.WordsProcessorLayer
jtulach@336: }
jtulach@336:
jtulach@336: return true;
jtulach@336: }
jtulach@336:
jtulach@336: }