11//: annotations/InterfaceExtractorProcessor.java
22// APT-based annotation processing.
3+ // {CompileTimeError} Not working in Java 8
34// {Exec: apt -factory
45// annotations.InterfaceExtractorProcessorFactory
56// Multiplier.java -s ../annotations}
67package annotations ;
7- //import com.sun.mirror.apt.*;
8- import javax .annotation .processing .*;
9- //import com.sun.mirror.declaration.*;
10- import javax .lang .model .element .*;
11- import javax .lang .model .SourceVersion ;
8+ import com .sun .mirror .apt .*;
9+ import com .sun .mirror .declaration .*;
1210import java .io .*;
1311import java .util .*;
1412
1513public class InterfaceExtractorProcessor
16- implements Processor {
17- private final ProcessingEnvironment env ;
18- private ArrayList <ExecutableElement > interfaceMethods =
19- new ArrayList <ExecutableElement >();
20- public void init (ProcessingEnvironment processingEnv ) {
21-
22- }
23- public SourceVersion getSupportedSourceVersion () {
24- return SourceVersion .RELEASE_7 ;
25- }
14+ implements AnnotationProcessor {
15+ private final AnnotationProcessorEnvironment env ;
16+ private ArrayList <MethodDeclaration > interfaceMethods =
17+ new ArrayList <MethodDeclaration >();
2618 public InterfaceExtractorProcessor (
27- ProcessingEnvironment env ) { this .env = env ; }
28- public boolean process (Set <? extends TypeElement > annotations ,
29- RoundEnvironment roundEnv ) {
30- for ( TypeElement typeElem : env .getElementUtils ()) {
19+ AnnotationProcessorEnvironment env ) { this .env = env ; }
20+ public void process () {
21+ for ( TypeDeclaration typeDecl :
22+ env .getSpecifiedTypeDeclarations ()) {
3123 ExtractInterface annot =
32- typeElem .getAnnotation (ExtractInterface .class );
24+ typeDecl .getAnnotation (ExtractInterface .class );
3325 if (annot == null )
3426 break ;
35- for (ExecutableElement m : typeElem .getMethods ())
27+ for (MethodDeclaration m : typeDecl .getMethods ())
3628 if (m .getModifiers ().contains (Modifier .PUBLIC ) &&
3729 !(m .getModifiers ().contains (Modifier .STATIC )))
3830 interfaceMethods .add (m );
@@ -41,17 +33,17 @@ public boolean process(Set<? extends TypeElement> annotations,
4133 PrintWriter writer =
4234 env .getFiler ().createSourceFile (annot .value ());
4335 writer .println ("package " +
44- typeElem .getPackage ().getQualifiedName () +";" );
36+ typeDecl .getPackage ().getQualifiedName () +";" );
4537 writer .println ("public interface " +
4638 annot .value () + " {" );
47- for (ExecutableElement m : interfaceMethods ) {
39+ for (MethodDeclaration m : interfaceMethods ) {
4840 writer .print (" public " );
4941 writer .print (m .getReturnType () + " " );
5042 writer .print (m .getSimpleName () + " (" );
5143 int i = 0 ;
52- for (VariableElement parm :
44+ for (ParameterDeclaration parm :
5345 m .getParameters ()) {
54- writer .print (parm .getKind () + " " +
46+ writer .print (parm .getType () + " " +
5547 parm .getSimpleName ());
5648 if (++i < m .getParameters ().size ())
5749 writer .print (", " );
@@ -66,16 +58,4 @@ public boolean process(Set<? extends TypeElement> annotations,
6658 }
6759 }
6860 }
69- public Set <String > getSupportedAnnotationTypes () {
70- return
71- Collections .singleton ("annotations.ExtractInterface" );
72- }
73- public Set <String > getSupportedOptions () {
74- return Collections .emptySet ();
75- }
76- public Iterable <? extends Completion > getCompletions (
77- Element element , AnnotationMirror annotation ,
78- ExecutableElement member , String userText ) {
79- return Collections .emptyList ();
80- }
81- } ///:~
61+ } ///:~
0 commit comments