+ ---- annotation- processor | + ---- annotation- lib | + ---- build.gradle | + ---- src | + ---- main | | + ---- java | | + ---- com | | + ---- prhythm | | + ---- annotations | | + ---- PrintUsage.java | | + ---- PrintUsageProcessor.java | | + ---- resources | + ---- META-INF | + ---- services | + ---- javax.annotation.processing.Processor | + ---- foo | + ---- build.gradle | + ---- src | + ---- main | + ---- java | + ---- com | + ---- prhythm | + ---- foo | + ---- Bar.java | + ---- build.gradle | + ---- settings.gradle
PrintUsage.java
package com.prhythm.annotations; /** * Print usage * Created by nanashi07 on 15/12/2. */ public @interface PrintUsage { }
PrintUsageProcessor.java
package com.prhythm.annotations; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Messager; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; import java.util.Set; /** * Processor of {@link PrintUsage} * Created by nanashi07 on 15/12/2. */ @SupportedAnnotationTypes("com.prhythm.annotations.PrintUsage") public class PrintUsageProcessor extends AbstractProcessor { @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { Messager messager = processingEnv.getMessager(); for (TypeElement typeElement : annotations) { for (Element element : roundEnv.getElementsAnnotatedWith(typeElement)) { String msg = String.format("Use %s on %s#%s", typeElement, element.getEnclosingElement(), element); messager.printMessage(Diagnostic.Kind.NOTE, msg); } } return true; } }
javax.annotation.processing.Processor
com.prhythm.annotations.PrintUsageProcessor
Bar.java
package com.prhythm.foo; import com.prhythm.annotations.PrintUsage; /** * Test class * Created by nanashi07 on 15/12/2. */ public class Bar { @PrintUsage public void move(String name) { } @PrintUsage public long time() { return System.currentTimeMillis(); } }
build.gradle (foo)
group 'com.prhythm' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.6 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' compile project(':annotation-lib') }
Enable annotation processor on preference
Build with gradle
gradle clean build
result
References:
- Pluggable Annotation Processing API とは
- Android Annotation Processing: POJO string generator
- ANNOTATION PROCESSING 101
- Java注解处理器
Type Memo
Note: ================================================== Note: TypeElement: com.prhythm.annotations.PrintUsage Note: TypeElement.getClass(): class com.sun.tools.javac.code.Symbol$ClassSymbol Note: TypeElement.getSimpleName(): PrintUsage Note: TypeElement.getEnclosedElements(): Note: TypeElement.getEnclosingElement(): com.prhythm.annotations Note: TypeElement.getInterfaces(): java.lang.annotation.Annotation Note: TypeElement.getNestingKind(): TOP_LEVEL Note: TypeElement.getQualifiedName(): com.prhythm.annotations.PrintUsage Note: TypeElement.getSuperclass(): none Note: TypeElement.getTypeParameters(): Note: TypeElement.getAnnotationMirrors(): Note: TypeElement.asType(): com.prhythm.annotations.PrintUsage Note: TypeElement.getKind(): ANNOTATION_TYPE Note: TypeElement.getModifiers(): [public, abstract] Note: ---------------------------------------------------------- Note: Element: move(java.lang.String) Note: Element.getModifiers(): [public] Note: Element.getClass(): class com.sun.tools.javac.code.Symbol$MethodSymbol Note: Element.getKind(): METHOD Note: Element.getAnnotationMirrors(): @com.prhythm.annotations.PrintUsage Note: Element.getSimpleName(): move Note: Element.asType(): (java.lang.String)void Note: Element.getEnclosedElements(): Note: Element.getEnclosingElement(): com.prhythm.foo.Bar Note: ---------------------------------------------------------- Note: Element: time() Note: Element.getModifiers(): [public] Note: Element.getClass(): class com.sun.tools.javac.code.Symbol$MethodSymbol Note: Element.getKind(): METHOD Note: Element.getAnnotationMirrors(): @com.prhythm.annotations.PrintUsage Note: Element.getSimpleName(): time Note: Element.asType(): ()long Note: Element.getEnclosedElements(): Note: Element.getEnclosingElement(): com.prhythm.foo.Bar Note: ================================================== Note: TypeElement: com.prhythm.annotations.Mark Note: TypeElement.getClass(): class com.sun.tools.javac.code.Symbol$ClassSymbol Note: TypeElement.getSimpleName(): Mark Note: TypeElement.getEnclosedElements(): value(),names() Note: TypeElement.getEnclosingElement(): com.prhythm.annotations Note: TypeElement.getInterfaces(): java.lang.annotation.Annotation Note: TypeElement.getNestingKind(): TOP_LEVEL Note: TypeElement.getQualifiedName(): com.prhythm.annotations.Mark Note: TypeElement.getSuperclass(): none Note: TypeElement.getTypeParameters(): Note: TypeElement.getAnnotationMirrors(): Note: TypeElement.asType(): com.prhythm.annotations.Mark Note: TypeElement.getKind(): ANNOTATION_TYPE Note: TypeElement.getModifiers(): [public, abstract] Note: ---------------------------------------------------------- Note: Element: com.prhythm.foo.Yahoo Note: Element.getModifiers(): [public, abstract] Note: Element.getClass(): class com.sun.tools.javac.code.Symbol$ClassSymbol Note: Element.getKind(): INTERFACE Note: Element.getAnnotationMirrors(): @com.prhythm.annotations.Mark("2323") Note: Element.getSimpleName(): Yahoo Note: Element.asType(): com.prhythm.foo.Yahoo Note: Element.getEnclosedElements(): home(java.lang.String) Note: Element.getEnclosingElement(): com.prhythm.foo Note: ---------------------------------------------------------- Note: Element: home(java.lang.String) Note: Element.getModifiers(): [public, abstract] Note: Element.getClass(): class com.sun.tools.javac.code.Symbol$MethodSymbol Note: Element.getKind(): METHOD Note: Element.getAnnotationMirrors(): @com.prhythm.annotations.Mark("110"),@com.prhythm.core.generic.http.annotation.Get("https://tw.yahoo.com") Note: Element.getSimpleName(): home Note: Element.asType(): (java.lang.String)java.lang.String Note: Element.getEnclosedElements(): Note: Element.getEnclosingElement(): com.prhythm.foo.Yahoo