How do I access the annotation of the super class/interface

Hi,

I am trying to write a custom rule in java for classes that contain a particular annotation in their super class/interface. So for example, if my custom annotation exists in any of the super classes or super interfaces of the class that Im declaring. Then I need to implement my logic.

I’ve got the logic figured out, however I cant seem to access the annotations of the super class/interface.

When checking the AST, the tree.superInterfaces().annotations()() or tree.superClass().annotations() always seem to return 0.

Is there a way in which I can access the annotations of the super classes/interfaces?

1 Like

Hello there Areeb, thank you for your precious feedback!
It looks like the Api is missing to decorate the .superClass() and .superInterfaces() elements with the annotation info, during the construction of the AST. I created a ticket to tackle this, as it is a pretty nice and important improvement to the API.

I fear though, that for now there is no direct way to get this information in your rule implementation :frowning:

EDIT:
sorry about that, I found another route that you can follow to get to the annotations of the superInterfaces:
superInterfaces().get(0).symbolType().symbol().metadata().annotations()
Give it a try, and let me know if this helps!

Hi,

Yeah that’s the workaround I used for interfaces. However, if it comes to checking the super interfaces of the super class we will have to first get the superClass by superClass.symbolType().symbol().declaration().superClass(). Afterwards, we can check the super interfaces of the super class. Then the code starts to get a bit lengthy due to casting and accessing multiple method invocations.

So yeah would be great if the API can decorate the elements with the annotation info during the construction of the AST. Then we can directly access without having to go through multiple method invocations.

Do update me when the ticket is resolved. Many thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.