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.
@MyCustomAnnotation
public void myMethod(){}
@MyCustomAnnotation
public void myMethod(){}
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
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @keplerk! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3mo ago
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 existing
keplerk
keplerkOP3mo ago
The 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
dan1st
dan1st3mo ago
you can get a method by its signature YourClass.class.getMethod("myMethod"/*, YourParametersHere.class*/) or objectOfYourClass.getClass().getMethod("myMethod"/*, YourParametersHere.class*/)
keplerk
keplerkOP3mo ago
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.
dan1st
dan1st3mo ago
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
Class<?> cl = yourObject.getClass();
for(Method method : cl.getMethods()){
MyCustomAnnotation annotation = method.getAnnotation(MyCustomAnnotation.class);
if(annotation != null) {
//...
}
}
Class<?> cl = yourObject.getClass();
for(Method method : cl.getMethods()){
MyCustomAnnotation annotation = method.getAnnotation(MyCustomAnnotation.class);
if(annotation != null) {
//...
}
}
keplerk
keplerkOP3mo ago
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
dan1st
dan1st3mo ago
You can get all spring managed components and look for methods with the annotation
keplerk
keplerkOP3mo ago
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
@MyCustomAnnotation(methodName = "myMethod")
public void myMethod(){}
@MyCustomAnnotation(methodName = "myMethod")
public void myMethod(){}
dan1st
dan1st3mo ago
ApplicationContext ctx;//TODO get it from Spring via DI or similar
Collection<Object> controllers = ctx.getBeansWithAnnotation(Controller.class);//if you just want controllers
for(Object controller : controllers){
Class<?> cl = yourObject.getClass();
for(Method method : cl.getMethods()){
MyCustomAnnotation annotation = method.getAnnotation(MyCustomAnnotation.class);
if(annotation != null) {
//...
}
}
}
ApplicationContext ctx;//TODO get it from Spring via DI or similar
Collection<Object> controllers = ctx.getBeansWithAnnotation(Controller.class);//if you just want controllers
for(Object controller : controllers){
Class<?> cl = yourObject.getClass();
for(Method method : cl.getMethods()){
MyCustomAnnotation annotation = method.getAnnotation(MyCustomAnnotation.class);
if(annotation != null) {
//...
}
}
}
keplerk
keplerkOP3mo ago
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
joinPoint.getTarget().getClass().getMethods()
joinPoint.getTarget().getClass().getMethods()
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.
JavaBot
JavaBot3mo ago
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.
JavaBot
JavaBot3mo ago
Post Closed
This post has been closed by <@306591040406683649>.
Want results from more Discord servers?
Add your server