OnJava8-Examples/annotations/InterfaceExtractorProcessor.java

82 lines
2.8 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: annotations/InterfaceExtractorProcessor.java
// APT-based annotation processing.
// {Exec: apt -factory
// annotations.InterfaceExtractorProcessorFactory
// Multiplier.java -s ../annotations}
package annotations;
2015-04-25 18:27:58 -07:00
//import com.sun.mirror.apt.*;
import javax.annotation.processing.*;
//import com.sun.mirror.declaration.*;
import javax.lang.model.element.*;
import javax.lang.model.SourceVersion;
2015-04-20 15:36:01 -07:00
import java.io.*;
import java.util.*;
public class InterfaceExtractorProcessor
2015-04-25 18:27:58 -07:00
implements Processor {
private final ProcessingEnvironment env;
private ArrayList<ExecutableElement> interfaceMethods =
new ArrayList<ExecutableElement>();
public void init(ProcessingEnvironment processingEnv) {
}
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.RELEASE_7;
}
2015-04-20 15:36:01 -07:00
public InterfaceExtractorProcessor(
2015-04-25 18:27:58 -07:00
ProcessingEnvironment env) { this.env = env; }
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for(TypeElement typeElem : env.getElementUtils()) {
2015-04-20 15:36:01 -07:00
ExtractInterface annot =
2015-04-25 18:27:58 -07:00
typeElem.getAnnotation(ExtractInterface.class);
2015-04-20 15:36:01 -07:00
if(annot == null)
break;
2015-04-25 18:27:58 -07:00
for(ExecutableElement m : typeElem.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 {
PrintWriter writer =
env.getFiler().createSourceFile(annot.value());
writer.println("package " +
2015-04-25 18:27:58 -07:00
typeElem.getPackage().getQualifiedName() +";");
2015-04-20 15:36:01 -07:00
writer.println("public interface " +
annot.value() + " {");
2015-04-25 18:27:58 -07:00
for(ExecutableElement m : interfaceMethods) {
2015-04-20 15:36:01 -07:00
writer.print(" public ");
writer.print(m.getReturnType() + " ");
writer.print(m.getSimpleName() + " (");
int i = 0;
2015-04-25 18:27:58 -07:00
for(VariableElement parm :
2015-04-20 15:36:01 -07:00
m.getParameters()) {
2015-04-25 18:27:58 -07:00
writer.print(parm.getKind() + " " +
2015-04-20 15:36:01 -07:00
parm.getSimpleName());
if(++i < m.getParameters().size())
writer.print(", ");
}
writer.println(");");
}
writer.println("}");
writer.close();
} catch(IOException ioe) {
throw new RuntimeException(ioe);
}
}
}
}
2015-04-25 18:27:58 -07:00
public Set<String> getSupportedAnnotationTypes() {
return
Collections.singleton("annotations.ExtractInterface");
}
public Set<String> getSupportedOptions() {
return Collections.emptySet();
}
public Iterable<? extends Completion> getCompletions(
Element element, AnnotationMirror annotation,
ExecutableElement member, String userText) {
return Collections.emptyList();
}
2015-04-20 15:36:01 -07:00
} ///:~