Get method name within an annotation dinamically
Hi guys, is possible to get the method name from which a custom annotation is being called?
E.x.
From the aspect processing MyCustomAnnotation, it's possible to get to know that the method "annotated" it's myMethod ?
Thanks !
Edit: The idea is not having to depend in a custom attribute where the method name would be hardcoded
13 Replies
⌛
This post has been reserved for your question.
Hey @keplerk! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
yes you can use reflection for it
first get the
Method
object (using Class#getMethod
or similar)
and then you can use getAnnotation
on it to get the annotation and use the returned object to access the attributes
btw you can use interfaces to enforce the method existingThe problem is that there are several methods in that class, i've even tried with getEnclosingMethod
I've tried several options, so as a last resort i posted my question here
you can get a method by its signature
YourClass.class.getMethod("myMethod"/*, YourParametersHere.class*/)
or objectOfYourClass.getClass().getMethod("myMethod"/*, YourParametersHere.class*/)
Yeah, but the point is that i want to avoid having to use the method name in a "hardcoded" way, i have it working that way right now, but my goal is to make it work in a "dynamic" way.
oh I didn't read the "not" in your edit
You can use
Class#getMethods()
to get all methods of a class and then find the annotated method
The problem is that the custom annotation will be used across all methods with a requestmapping on it
:Sad:
So is not a single usage
I've squeezed my brain, and the only solution i can think about it's basically what i don't want to do
You can get all spring managed components
and look for methods with the annotation
Yeah but my need is for the Method where the annotation is executing at that moment
Looks like there is no other option but hardcode the method name as an attribute of the annotation
Is not that complicated, due all the logic being inside an aspect with aop
Inside it i have access to the class, so no application context necessary
There is no proper solution so, i'll close this post, i'll go for the one i wanted to avoid the most explained on the edit on my original post.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@306591040406683649>.