|
| 1 | +//: annotations/IfaceExtractorProcessor.java |
| 2 | +// javac-based annotation processing. |
| 3 | +package annotations; |
| 4 | +import javax.annotation.processing.*; |
| 5 | +import javax.lang.model.SourceVersion; |
| 6 | +import javax.lang.model.element.*; |
| 7 | +import java.util.*; |
| 8 | +import java.io.*; |
| 9 | + |
| 10 | +@SupportedAnnotationTypes( |
| 11 | + "annotations.ExtractInterface") |
| 12 | +@SupportedSourceVersion(SourceVersion.RELEASE_8) |
| 13 | +public class IfaceExtractorProcessor |
| 14 | +extends AbstractProcessor { |
| 15 | + private ArrayList<Element> |
| 16 | + interfaceMethods = new ArrayList<>(); |
| 17 | + private ProcessingEnvironment processingEnv; |
| 18 | + @Override public void |
| 19 | + init(ProcessingEnvironment processingEnv) { |
| 20 | + this.processingEnv = processingEnv; |
| 21 | + } |
| 22 | + @Override public boolean |
| 23 | + process(Set<? extends TypeElement> annotations, |
| 24 | + RoundEnvironment env) { |
| 25 | + for(Element elem : |
| 26 | + env.getElementsAnnotatedWith(ExtractInterface.class)) { |
| 27 | + if(elem.getModifiers().contains(Modifier.PUBLIC) && |
| 28 | + !(elem.getModifiers().contains(Modifier.STATIC))) |
| 29 | + interfaceMethods.add(elem); |
| 30 | + if(interfaceMethods.size() > 0) { |
| 31 | + for(Element el : interfaceMethods) |
| 32 | + System.out.println(el.getKind() + |
| 33 | + " : " + el.getModifiers() + |
| 34 | + " : " + el.getSimpleName() + |
| 35 | + " : " + el.asType()); |
| 36 | + try { |
| 37 | + try(Writer writer = |
| 38 | + processingEnv.getFiler() |
| 39 | + .createSourceFile("Filename.txt") |
| 40 | + .openWriter()) { |
| 41 | + /* writer.write("package " + |
| 42 | + typeDecl.getPackage().getQualifiedName() +";"); |
| 43 | + writer.write("public interface " + |
| 44 | + annot.value() + " {"); |
| 45 | + for(MethodDeclaration m : interfaceMethods) { |
| 46 | + writer.print(" public "); |
| 47 | + writer.print(m.getReturnType() + " "); |
| 48 | + writer.print(m.getSimpleName() + " ("); |
| 49 | + int i = 0; |
| 50 | + for(ParameterDeclaration parm : |
| 51 | + m.getParameters()) { |
| 52 | + writer.print(parm.getType() + " " + |
| 53 | + parm.getSimpleName()); |
| 54 | + if(++i < m.getParameters().size()) |
| 55 | + writer.print(", "); |
| 56 | + } |
| 57 | + writer.write(");"); |
| 58 | + } |
| 59 | + */ writer.write("}"); |
| 60 | + } |
| 61 | + } catch(Exception e) { |
| 62 | + throw new RuntimeException(e); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return false; |
| 67 | + } |
| 68 | +} ///:~ |
0 commit comments