java - 自定义注释处理器 - 带注释的检测方法

标签 java annotations annotation-processing

我正在尝试编写一个注释处理器来检测使用 @PrintMethod 注释进行注释的方法。例如,在下面的测试类中,我想打印测试方法中的代码。有办法做到吗?

从下面所述的 AnnotationProcessor 类中,我只能获取方法名称,但不能获取方法的详细信息。

测试类

public class test {

    public static void main(String[] args) {
        System.out.println("Args");
    }

    @PrintMethod
    private boolean testMethod(String input) {
        if(input!=null) {  
            return true;
        }
        return false; 
    }
}

注释处理器类

public class AnnotationProcessor extends AbstractProcessor {
//......
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        //retrieve test Anntoation
        Set<? extends Element> ann =roundEnv.getElementsAnnotatedWith(PrintMethod.class);

        //Print the Method Name
        for(Element e: ann) {
            String msg="Element ee :"+ee.getSimpleName().toString();
            processingEnv.getMessager().printMessage( javax.tools.Diagnostic.Kind.ERROR, msg, e);
        }
    }
}

最佳答案

我也很好奇这个问题,所以我决定尝试弄清楚。事实证明比我想象的要容易。您所需要做的就是利用专有tools.jar 库中的Trees api。我在这里制作了一个快速注释处理器:https://github.com/johncarl81/printMethod

这是它的核心内容:

@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("org.printMethod.PrintMethod")
public class PrintMethodAnnotationProcessor extends AbstractProcessor {

    private Trees trees;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        trees = Trees.instance(processingEnv); //initialize the Trees api.
    }

    @Override
    public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnvironment) {

        MethodPrintScanner visitor = new MethodPrintScanner();

        for (Element e : roundEnvironment.getElementsAnnotatedWith(PrintMethod.class)) {
            TreePath tp = trees.getPath(e);
            // visit the annotated methods
            visitor.scan(tp, trees);
        }
        return true;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }
}

还有MethodPrintScanner:

public class MethodPrintScanner extends TreePathScanner {

    @Override
    public Object visitMethod(MethodTree methodTree, Object o) {
        System.out.println(methodTree);
        return null;
    }
}

您可以看到我们能够访问与给定带注释的元素关联的TreePath。对于每个方法,我们只需 println() methodTree 即可为我们提供方法的内容。

使用您的示例,以下是编译期间程序的输出:

@PrintMethod()
private boolean testMethod(String input) {
    if (input != null) {
        return true;
    }
    return false;
}

关于java - 自定义注释处理器 - 带注释的检测方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18028595/

相关文章:

java - 找不到 Maven 注释处理处理器

java - 将 byte[] 写入文件并从文件中读取 byte[]

java - 使用 Java CLASS Day 查找自特定日期以来的天数,例如生日?

iphone - 在 iPhone map 中的 route 添加注释

java - 如何导入 lombok JavacAnnotationHandler 进行自定义注释?

Java 11-Kotlin 注解处理器

java - 各处边距 8 dp,顶部、右侧、左侧和底部

java - 返回给定字符串中重复次数最多的字符

java - 加载 csv 文件时过滤特定行

使用 Lombok 为带有注释的代码生成 Java getter/setter