开发者

Java annotations and method conformance

开发者 https://www.devze.com 2022-12-14 12:36 出处:网络
I have a newly defined annotation that is targeted at methods. Is there a way to annotate it in such a way that when I apply it to a method a() of class A, it will also apply to a method b() of class

I have a newly defined annotation that is targeted at methods. Is there a way to annotate it in such a way that when I apply it to a method a() of class A, it will also apply to a method b() of class B which inherits from A, provided that b() conforms to a() in some way- similar names, conforming input types, c开发者_运维问答onforming return types, etc.?


The short answer is "no". Annotation only applies to the explicitly annotated target (method / field / class). If annotation itself is annotated with @Inherited it will also apply to descendants of annotated target; but that won't help with your case (if I understood correctly).

However, depending on what you intend to use that annotation for, you can either write your own Annotation Processor to do whatever you want or simply use reflection to look up appropriate method and decide whether your annotation should logically apply to it as well.


As far as I'm able to determine, there isn't a way to mark an annotation is inherited for anything other than classes (via @Inherited).

0

精彩评论

暂无评论...
验证码 换一张
取 消