Java8,如何发现在visitMethodInvocation类和方法的名字吗? [英] Java8, how discover the class and method name in visitMethodInvocation?

查看:176
本文介绍了Java8,如何发现在visitMethodInvocation类和方法的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着java7目录和Java8,我想生成一个警告,如果一些方法被调用。
该警告将打印,如果一个特定的罐子present当用户再编译。

With Java7 and Java8, I would like to generate a warning if some methods was called. The warning will be print if a specific jar is present when then user compile.

我写的注解处理器,赶上visitMethodInvocation()。现在,我想提取的类和方法的名称将被调用。

I write an Annotation Processor and catch the visitMethodInvocation(). Now, I want extract the class and method names will be invoked.

是否有可能做到这一点?
或如何处理呢?

Is it possible to do that ? Or how to approach this?

推荐答案

作为一种替代从@emory伟大的答案,你可以考虑使用的类型检查框架(JSR 308)。它的优点是它可以帮助您轻松确定的方法调用的类型。这里是一个基于检查框架的示例处理器(添加checker.jar到classpath编译时)。

As an alternative to the great answer from @emory, you can consider using the annotation processing provided by the Type Checker framework (JSR 308). The advantage is it can help you to easily determinate the type of the method invoker. Here is an example processor based on the checker framework (add checker.jar to the classpath when compile).

@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class MyTypeProcessor extends AbstractTypeProcessor {
    class MyTreePathScanner extends TreePathScanner<Void, Void> {
        private final Trees trees;
        private final TreePath root;

        public MyTreePathScanner(TreePath root) {
            this.trees = Trees.instance(processingEnv);
            this.root = root;
        }

        @Override
        public Void visitMemberSelect(MemberSelectTree node, Void aVoid) {
            ExpressionTree expression = node.getExpression();
            TreePath expr = TreePath.getPath(root, expression);
            TypeMirror type = trees.getTypeMirror(expr);
            Element typeElement = processingEnv.getTypeUtils().asElement(type);
            Optional<? extends Element> invoker = typeElement.getEnclosedElements().stream().filter(
                    e -> e.getSimpleName().equals(node.getIdentifier())).findFirst();
            if (invoker.isPresent() && invoker.get().getKind() == ElementKind.METHOD) {
                System.out.println("Type: " + typeElement + ", method: " + invoker.get());
            }
            return super.visitMemberSelect(node, aVoid);
        }

    }

    @Override
    public void typeProcess(TypeElement typeElement, TreePath root) {
        new MyTreePathScanner(root).scan(root, null);
    }
}

这是处理下列输入源。

Which is processing the following input source.

public class Test {

    public void foo() {
    }

    public static void main(String[] args) {
        System.out.println("Hello world!");
        Test t = new Test();
        t.foo();
    }
}

下面是输出:

Type: java.io.PrintStream, method: println()
Type: Test, method: foo()

这篇关于Java8,如何发现在visitMethodInvocation类和方法的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆