如何在注释处理中引用方法的实现? [英] How can I refer to implementations of a method in annotation processing?

查看:229
本文介绍了如何在注释处理中引用方法的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩Java(javax)注释处理。

I am playing around with Java (javax) annotation processing.

假设我有方法的注释:

@Target(ElementType.METHOD)
public @interface MethodAnnotation { }

现在我想处理所有使用带注释方法从类型覆盖的方法:

Now I want to process all the methods which are overridden from a type with the annotated method:

interface MyInterface() {
    @MethodAnnotation
    void f()
}

class MyClass implements MyInterface {
    override void f() { } // <- I want to process this method
}

@Inherited 元注释似乎不适合:


请注意,此元注释类型没有如果带注释的类型用于注释除类之外的任何内容,则会生效。

Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class.

此外,是否可以处理继承的类方法是不是在子类中重写?像这样:

Also, is it possible to process an inherited class method which is not overridden in a subclass? Like this:

class MyClass {
    @MethodAnnotation
    void f() { }
}

class MySubClass extends MyClass { } // <- I want to process its f()
                                     //    or at least to find out that it doesn't
                                     //    override the method

如何在 AbstractProcessor ?

我想,要实现这一点,我需要找到eclosing类的子类,但我还没有找到办法这样做。

I guess, to achieve this I need to find subclasses of the eclosing class, but I haven't found a way to do this either.

UPD :我想可以使用 RoundEnvironment.getRootElements()但仍然没有找到合适的方法。

UPD: I suppose it's possible using RoundEnvironment.getRootElements() but still found no proper way of doing this.

推荐答案

简短的回答是开箱即用的注释处理不会让你这么容易,但可以完成。

The short answer is that out-of-the-box annotation processing isn't going to make this easy for you, but it can be done.

而不是使用正常的调度机制进行处理,您实际上必须处理每个方法并自行进行过滤。

Rather than using the normal dispatch mechanism for processing, you're actually going to have to process every method and do the filtering yourself.

定义处理器,使其支持所有注释,使用*作为其支持的注释类型。这意味着你的处理器每轮都会被调用。

Define your processor so that it supports all annotations by using "*" as its supported annotation type. This will mean that your processor will get invoked every round.

使用 getRootElements 每轮获得整套元素。

Use getRootElements to get the entire set of elements every round.

创建 ElementScanner8 遍历您找到的任何元素 ExecutableElement s。如果您愿意相信已覆盖的方法使用 @Override 进行注释,则可以对这些方法进行快速过滤。否则,只需查看所有这些。

Create an ElementScanner8 to traverse any element that you find to look for ExecutableElements. If you're willing to trust that overridden methods are annotated with @Override, you can do a quick filter on those. Otherwise, just look at all of them.

现在您需要查看方法使用您正在寻找的注释覆盖方法。获取给定方法已覆盖的方法没有简单的方法,因此您需要获取封闭元素,请查看其超类实现接口(递归),获取封闭元素过滤掉方法测试是否已经过通过相关方法覆盖。如果有,你可以检查注释,看看它是否有你关心的。

Now you need to see if the method overrides a method with the annotation you're looking for. There's no easy way to get methods that a given method has overridden, so you need to get the enclosing element of the method, look at its superclass and implemented interfaces (recursively), get their enclosed elements, filter out the methods, and test to see if it has been overridden by the method in question. If it has, you can check the annotations to see if it has one you care about.

此时,您应该拥有重写方法,重写方法和您正在寻找的注释镜像,因此您应该能够实现您想要的任何逻辑。

At this point, you should have the overriding method, the overridden method and the annotation mirror that you were looking for, so you should be able to implement whatever logic you wanted.

这篇关于如何在注释处理中引用方法的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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