2015-04-20 15:36:01 -07:00
|
|
|
//: annotations/InterfaceExtractorProcessor.java
|
|
|
|
// APT-based annotation processing.
|
2015-04-29 12:53:35 -07:00
|
|
|
// {CompileTimeError} Not working in Java 8
|
2015-04-20 15:36:01 -07:00
|
|
|
// {Exec: apt -factory
|
|
|
|
// annotations.InterfaceExtractorProcessorFactory
|
|
|
|
// Multiplier.java -s ../annotations}
|
|
|
|
package annotations;
|
2015-04-29 12:53:35 -07:00
|
|
|
import com.sun.mirror.apt.*;
|
|
|
|
import com.sun.mirror.declaration.*;
|
2015-04-20 15:36:01 -07:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class InterfaceExtractorProcessor
|
2015-04-29 12:53:35 -07:00
|
|
|
implements AnnotationProcessor {
|
|
|
|
private final AnnotationProcessorEnvironment env;
|
|
|
|
private ArrayList<MethodDeclaration> interfaceMethods =
|
2015-05-05 11:20:13 -07:00
|
|
|
new ArrayList<>();
|
2015-04-20 15:36:01 -07:00
|
|
|
public InterfaceExtractorProcessor(
|
2015-04-29 12:53:35 -07:00
|
|
|
AnnotationProcessorEnvironment env) { this.env = env; }
|
|
|
|
public void process() {
|
|
|
|
for(TypeDeclaration typeDecl :
|
|
|
|
env.getSpecifiedTypeDeclarations()) {
|
2015-04-20 15:36:01 -07:00
|
|
|
ExtractInterface annot =
|
2015-04-29 12:53:35 -07:00
|
|
|
typeDecl.getAnnotation(ExtractInterface.class);
|
2015-04-20 15:36:01 -07:00
|
|
|
if(annot == null)
|
|
|
|
break;
|
2015-04-29 12:53:35 -07:00
|
|
|
for(MethodDeclaration m : typeDecl.getMethods())
|
2015-04-20 15:36:01 -07:00
|
|
|
if(m.getModifiers().contains(Modifier.PUBLIC) &&
|
|
|
|
!(m.getModifiers().contains(Modifier.STATIC)))
|
|
|
|
interfaceMethods.add(m);
|
|
|
|
if(interfaceMethods.size() > 0) {
|
|
|
|
try {
|
2015-05-05 14:05:39 -07:00
|
|
|
try (PrintWriter writer = env.getFiler().createSourceFile(annot.value())) {
|
|
|
|
writer.println("package " +
|
|
|
|
typeDecl.getPackage().getQualifiedName() +";");
|
|
|
|
writer.println("public interface " +
|
|
|
|
annot.value() + " {");
|
|
|
|
for(MethodDeclaration m : interfaceMethods) {
|
|
|
|
writer.print(" public ");
|
|
|
|
writer.print(m.getReturnType() + " ");
|
|
|
|
writer.print(m.getSimpleName() + " (");
|
|
|
|
int i = 0;
|
|
|
|
for(ParameterDeclaration parm :
|
|
|
|
m.getParameters()) {
|
|
|
|
writer.print(parm.getType() + " " +
|
|
|
|
parm.getSimpleName());
|
|
|
|
if(++i < m.getParameters().size())
|
|
|
|
writer.print(", ");
|
|
|
|
}
|
|
|
|
writer.println(");");
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
2015-05-05 14:05:39 -07:00
|
|
|
writer.println("}");
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
} catch(IOException ioe) {
|
|
|
|
throw new RuntimeException(ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
} ///:~
|