2015-09-07 11:44:36 -06:00
|
|
|
// annotations/ifx/IfaceExtractorProcessor.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// javac-based annotation processing
|
2015-06-15 17:47:35 -07:00
|
|
|
package annotations.ifx;
|
|
|
|
import javax.annotation.processing.*;
|
|
|
|
import javax.lang.model.SourceVersion;
|
|
|
|
import javax.lang.model.element.*;
|
|
|
|
import javax.lang.model.util.*;
|
|
|
|
import java.util.*;
|
2017-01-22 16:48:11 -08:00
|
|
|
import java.util.stream.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
@SupportedAnnotationTypes(
|
|
|
|
"annotations.ifx.ExtractInterface")
|
|
|
|
@SupportedSourceVersion(SourceVersion.RELEASE_8)
|
|
|
|
public class IfaceExtractorProcessor
|
|
|
|
extends AbstractProcessor {
|
|
|
|
private ArrayList<Element>
|
|
|
|
interfaceMethods = new ArrayList<>();
|
|
|
|
Elements elementUtils;
|
|
|
|
private ProcessingEnvironment processingEnv;
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public void init(
|
2017-01-22 16:48:11 -08:00
|
|
|
ProcessingEnvironment processingEnv) {
|
2015-06-15 17:47:35 -07:00
|
|
|
this.processingEnv = processingEnv;
|
|
|
|
elementUtils = processingEnv.getElementUtils();
|
|
|
|
}
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public boolean process(
|
2017-01-22 16:48:11 -08:00
|
|
|
Set<? extends TypeElement> annotations,
|
|
|
|
RoundEnvironment env) {
|
2015-06-15 17:47:35 -07:00
|
|
|
for(Element elem:env.getElementsAnnotatedWith(
|
|
|
|
ExtractInterface.class)) {
|
|
|
|
String interfaceName = elem.getAnnotation(
|
|
|
|
ExtractInterface.class).interfaceName();
|
|
|
|
for(Element enclosed :
|
|
|
|
elem.getEnclosedElements()) {
|
|
|
|
if(enclosed.getKind()
|
|
|
|
.equals(ElementKind.METHOD) &&
|
|
|
|
enclosed.getModifiers()
|
|
|
|
.contains(Modifier.PUBLIC) &&
|
|
|
|
!enclosed.getModifiers()
|
|
|
|
.contains(Modifier.STATIC)) {
|
|
|
|
interfaceMethods.add(enclosed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(interfaceMethods.size() > 0)
|
|
|
|
writeInterfaceFile(interfaceName);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private void
|
|
|
|
writeInterfaceFile(String interfaceName) {
|
2016-09-06 12:32:51 -06:00
|
|
|
try(
|
|
|
|
Writer writer = processingEnv.getFiler()
|
|
|
|
.createSourceFile(interfaceName)
|
|
|
|
.openWriter()
|
|
|
|
) {
|
2015-12-15 11:47:04 -08:00
|
|
|
String packageName = elementUtils
|
|
|
|
.getPackageOf(interfaceMethods
|
|
|
|
.get(0)).toString();
|
|
|
|
writer.write(
|
|
|
|
"package " + packageName + ";\n");
|
|
|
|
writer.write("public interface " +
|
|
|
|
interfaceName + " {\n");
|
|
|
|
for(Element elem : interfaceMethods) {
|
|
|
|
ExecutableElement method =
|
|
|
|
(ExecutableElement)elem;
|
|
|
|
String signature = " public ";
|
2017-01-22 16:48:11 -08:00
|
|
|
signature += method.getReturnType() + " ";
|
2015-12-15 11:47:04 -08:00
|
|
|
signature += method.getSimpleName();
|
|
|
|
signature += createArgList(
|
|
|
|
method.getParameters());
|
|
|
|
System.out.println(signature);
|
|
|
|
writer.write(signature + ";\n");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-12-15 11:47:04 -08:00
|
|
|
writer.write("}");
|
2015-06-15 17:47:35 -07:00
|
|
|
} catch(Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private String createArgList(
|
|
|
|
List<? extends VariableElement> parameters) {
|
2017-01-22 16:48:11 -08:00
|
|
|
String args = parameters.stream()
|
|
|
|
.map(p -> p.asType() + " " + p.getSimpleName())
|
|
|
|
.collect(Collectors.joining(", "));
|
|
|
|
return "(" + args + ")";
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|