Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created February 6, 2013 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-neck/4722081 to your computer and use it in GitHub Desktop.
Save mike-neck/4722081 to your computer and use it in GitHub Desktop.
AnnotationProcessorをすこしさわってみた
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();
PrintWriter writer = null;
try {
FileObject fileObject = filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "result.txt");
writer = new PrintWriter(fileObject.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.");
} finally {
if (writer != null) writer.close();
}
return true;
}
/**
* convert {@code java.util.Set&lt;T&gt;} into {@code java.util.List&lt;T&gt;}
* @param set source
* @param <T> type
* @return unmodifiable list of &lt;T&gt;
*/
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());
}
}
package org.mikeneck.annotation;
import javax.inject.Inject;
import java.util.Map;
/**
* @author mike
*/
public class Login implements Redirectable {
private static final String KEY = "name";
@Inject
private UserService service;
/**
* {@inheritDoc}
*/
@Override
public String getPage (Map<String, String> info) {
if (service.isAuthorized()) {
info.put(KEY, service.getName());
return "index.html";
} else {
info.put(KEY, "guest");
return "login.html";
}
}
}
package org.mikeneck.annotation;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Map;
/**
* @author mike
*/
public class Logout implements Redirectable {
private static final String KEY = "name";
@Inject
@Named(value = "out")
private UserService service;
/**
* {@inheritDoc}
*/
@Override
public String getPage (Map<String, String> info) {
service.logout();
info.remove(KEY);
return "logout.html";
}
}
org.mikeneck.annotation.InjectProcessor supports
javax.inject.Inject
====RoundEnvironment#getRootElements====
returns Set<? extends Element> contains
0 : com.sun.tools.javac.code.Symbol.ClassSymbol
asType : org.mikeneck.annotation.Login
simpleName : Login
class : com.sun.tools.javac.code.Symbol.ClassSymbol, modifiers [public], kind : CLASS, type - kind : DECLARED, type : org.mikeneck.annotation.Login, name : Login
enclosingElement :
class : com.sun.tools.javac.code.Symbol.PackageSymbol, modifiers [], kind : PACKAGE, type - kind : PACKAGE, type : org.mikeneck.annotation, name : annotation
enclosingElements :
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : CONSTRUCTOR, type - kind : EXECUTABLE, type : ()void, name : <init>
class : com.sun.tools.javac.code.Symbol.VarSymbol, modifiers [private, static, final], kind : FIELD, type - kind : DECLARED, type : java.lang.String, name : KEY
class : com.sun.tools.javac.code.Symbol.VarSymbol, modifiers [private], kind : FIELD, type - kind : DECLARED, type : org.mikeneck.annotation.UserService, name : service
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : METHOD, type - kind : EXECUTABLE, type : (java.util.Map<java.lang.String,java.lang.String>)java.lang.String, name : getPage
annotationMirrors :
1 : com.sun.tools.javac.code.Symbol.ClassSymbol
asType : org.mikeneck.annotation.Logout
simpleName : Logout
class : com.sun.tools.javac.code.Symbol.ClassSymbol, modifiers [public], kind : CLASS, type - kind : DECLARED, type : org.mikeneck.annotation.Logout, name : Logout
enclosingElement :
class : com.sun.tools.javac.code.Symbol.PackageSymbol, modifiers [], kind : PACKAGE, type - kind : PACKAGE, type : org.mikeneck.annotation, name : annotation
enclosingElements :
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : CONSTRUCTOR, type - kind : EXECUTABLE, type : ()void, name : <init>
class : com.sun.tools.javac.code.Symbol.VarSymbol, modifiers [private, static, final], kind : FIELD, type - kind : DECLARED, type : java.lang.String, name : KEY
class : com.sun.tools.javac.code.Symbol.VarSymbol, modifiers [private], kind : FIELD, type - kind : DECLARED, type : org.mikeneck.annotation.UserService, name : service
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : METHOD, type - kind : EXECUTABLE, type : (java.util.Map<java.lang.String,java.lang.String>)java.lang.String, name : getPage
annotationMirrors :
2 : com.sun.tools.javac.code.Symbol.ClassSymbol
asType : org.mikeneck.annotation.UserServiceImpl
simpleName : UserServiceImpl
class : com.sun.tools.javac.code.Symbol.ClassSymbol, modifiers [public], kind : CLASS, type - kind : DECLARED, type : org.mikeneck.annotation.UserServiceImpl, name : UserServiceImpl
enclosingElement :
class : com.sun.tools.javac.code.Symbol.PackageSymbol, modifiers [], kind : PACKAGE, type - kind : PACKAGE, type : org.mikeneck.annotation, name : annotation
enclosingElements :
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : CONSTRUCTOR, type - kind : EXECUTABLE, type : ()void, name : <init>
class : com.sun.tools.javac.code.Symbol.VarSymbol, modifiers [private], kind : FIELD, type - kind : BOOLEAN, type : boolean, name : authorized
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : METHOD, type - kind : EXECUTABLE, type : ()boolean, name : isAuthorized
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : METHOD, type - kind : EXECUTABLE, type : ()java.lang.String, name : getName
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public], kind : METHOD, type - kind : EXECUTABLE, type : ()void, name : logout
annotationMirrors :
3 : com.sun.tools.javac.code.Symbol.ClassSymbol
asType : org.mikeneck.annotation.UserService
simpleName : UserService
class : com.sun.tools.javac.code.Symbol.ClassSymbol, modifiers [public, abstract], kind : INTERFACE, type - kind : DECLARED, type : org.mikeneck.annotation.UserService, name : UserService
enclosingElement :
class : com.sun.tools.javac.code.Symbol.PackageSymbol, modifiers [], kind : PACKAGE, type - kind : PACKAGE, type : org.mikeneck.annotation, name : annotation
enclosingElements :
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public, abstract], kind : METHOD, type - kind : EXECUTABLE, type : ()boolean, name : isAuthorized
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public, abstract], kind : METHOD, type - kind : EXECUTABLE, type : ()java.lang.String, name : getName
class : com.sun.tools.javac.code.Symbol.MethodSymbol, modifiers [public, abstract], kind : METHOD, type - kind : EXECUTABLE, type : ()void, name : logout
annotationMirrors :
====RoundEnvironment#getRootElements====
====Set<? extends TypeElement> annotations====
0 : com.sun.tools.javac.code.Symbol.ClassSymbol
asType : javax.inject.Inject
simpleName : Inject
class : com.sun.tools.javac.code.Symbol.ClassSymbol, modifiers [public, abstract], kind : ANNOTATION_TYPE, type - kind : DECLARED, type : javax.inject.Inject, name : Inject
enclosingElement :
class : com.sun.tools.javac.code.Symbol.PackageSymbol, modifiers [], kind : PACKAGE, type - kind : PACKAGE, type : javax.inject, name : inject
enclosingElements :
annotationMirrors :
type : java.lang.annotation.Target
type : java.lang.annotation.Retention
type : java.lang.annotation.Documented
====Set<? extends TypeElement> annotations====
====RoundEnvironment#getElementsAnnotatedWith====
javax.inject.Inject
0 : com.sun.tools.javac.code.Symbol.VarSymbol
asType : org.mikeneck.annotation.UserService
simpleName : service
class : com.sun.tools.javac.code.Symbol.VarSymbol, modifiers [private], kind : FIELD, type - kind : DECLARED, type : org.mikeneck.annotation.UserService, name : service
enclosingElement :
class : com.sun.tools.javac.code.Symbol.ClassSymbol, modifiers [public], kind : CLASS, type - kind : DECLARED, type : org.mikeneck.annotation.Login, name : Login
enclosingElements :
annotationMirrors :
type : javax.inject.Inject
1 : com.sun.tools.javac.code.Symbol.VarSymbol
asType : org.mikeneck.annotation.UserService
simpleName : service
class : com.sun.tools.javac.code.Symbol.VarSymbol, modifiers [private], kind : FIELD, type - kind : DECLARED, type : org.mikeneck.annotation.UserService, name : service
enclosingElement :
class : com.sun.tools.javac.code.Symbol.ClassSymbol, modifiers [public], kind : CLASS, type - kind : DECLARED, type : org.mikeneck.annotation.Logout, name : Logout
enclosingElements :
annotationMirrors :
type : javax.inject.Inject
type : javax.inject.Named
====RoundEnvironment#getElementsAnnotatedWith====
package org.mikeneck.annotation;
/**
* @author mike
*/
public interface UserService {
public boolean isAuthorized ();
public String getName ();
public void logout();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment