Created
February 12, 2013 13:30
-
-
Save mike-neck/4769902 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.mikeneck.annotation; | |
import javax.annotation.processing.*; | |
import javax.inject.Inject; | |
import javax.lang.model.SourceVersion; | |
import javax.lang.model.element.AnnotationMirror; | |
import javax.lang.model.element.Element; | |
import javax.lang.model.element.TypeElement; | |
import javax.tools.Diagnostic; | |
import javax.tools.FileObject; | |
import javax.tools.StandardLocation; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Set; | |
/** | |
* @author mike | |
*/ | |
@SupportedSourceVersion(SourceVersion.RELEASE_7) | |
@SupportedAnnotationTypes({"javax.inject.Inject"}) | |
public class InjectProcessor extends AbstractProcessor { | |
private static final char comma = ','; | |
@Override | |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | |
if (annotations.size() == 0) return true; | |
Messager messager = processingEnv.getMessager(); | |
Filer filer = processingEnv.getFiler(); | |
try (PrintWriter writer = new PrintWriter( | |
filer.createResource( | |
StandardLocation.SOURCE_OUTPUT, "", "result.txt").openWriter(), true)) { | |
// print supporting annotation | |
writer.print(this.getClass().getCanonicalName()); | |
writer.println(" supports "); | |
for (String type : this.getSupportedAnnotationTypes()) { | |
writer.println(" " + type); | |
} | |
// print root elements information | |
writer.println("====RoundEnvironment#getRootElements===="); | |
writer.println(" returns Set<? extends Element> contains"); | |
List<? extends Element> roots = | |
convertToUnmodifiableList(roundEnv.getRootElements()); | |
for (int index = 0, size = roots.size(); index < size; index += 1) { | |
Element element = roots.get(index); | |
scanEndPrintElement(index, writer, element); | |
} | |
writer.println("====RoundEnvironment#getRootElements===="); | |
writer.println(""); | |
// print annotations information | |
writer.println("====Set<? extends TypeElement> annotations===="); | |
List<? extends TypeElement> list = convertToUnmodifiableList(annotations); | |
for (int index = 0, size = list.size(); index < size; index += 1) { | |
TypeElement element = list.get(index); | |
scanEndPrintElement(index, writer, element); | |
} | |
writer.println("====Set<? extends TypeElement> annotations===="); | |
writer.println(""); | |
// print annotated elements information | |
writer.println("====RoundEnvironment#getElementsAnnotatedWith===="); | |
writer.println(" " + Inject.class.getCanonicalName()); | |
List<? extends Element> elements = | |
convertToUnmodifiableList(roundEnv.getElementsAnnotatedWith(Inject.class)); | |
for (int index = 0, len = elements.size(); index < len; index += 1) { | |
Element element = elements.get(index); | |
scanEndPrintElement(index, writer, element); | |
} | |
writer.println("====RoundEnvironment#getElementsAnnotatedWith===="); | |
writer.println(""); | |
} catch (IOException e) { | |
messager.printMessage(Diagnostic.Kind.ERROR, "cannot write file object."); | |
} | |
return true; | |
} | |
/** | |
* convert {@code java.util.Set<T>} into {@code java.util.List<T>} | |
* @param set source | |
* @param <T> type | |
* @return unmodifiable list of <T> | |
*/ | |
private <T> List<T> convertToUnmodifiableList (Set<T> set) { | |
return Collections.unmodifiableList(new ArrayList<>(set)); | |
} | |
/** | |
* print {@code javax.lang.model.Element} information | |
* @param index the number of elements | |
* @param writer a writer object | |
* @param element {@code javax.lang.model.Element} to be printed | |
*/ | |
private void scanEndPrintElement (int index, PrintWriter writer, Element element) { | |
writer.println(index + " : " + element.getClass().getCanonicalName()); | |
writer.println(" asType : " + element.asType()); | |
writer.println(" simpleName : " + element.getSimpleName()); | |
printElement(writer, element); | |
Element elm = element.getEnclosingElement(); | |
writer.println(" enclosingElement : "); | |
printElement(writer, elm); | |
writer.println(" enclosingElements :"); | |
for (Element e : element.getEnclosedElements()) { | |
printElement(writer, e); | |
} | |
writer.println(" annotationMirrors :"); | |
for (AnnotationMirror mirror : element.getAnnotationMirrors()) { | |
writer.println(" type : " + mirror.getAnnotationType()); | |
} | |
} | |
/** | |
* print {@code javax.lang.model.Element} information | |
* @param writer a writer object | |
* @param e {@code javax.lang.model.Element} to be printed | |
*/ | |
private void printElement (PrintWriter writer, Element e) { | |
writer.println(" class : " + e.getClass().getCanonicalName() + comma + | |
" modifiers " + e.getModifiers() + comma + | |
" kind : " + e.getKind() + comma + | |
" type - kind : " + e.asType().getKind().name() + comma + | |
" type : " + e.asType() + comma + | |
" name : " + e.getSimpleName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment