59

The question is short and simple: Is there a way to get the Method object from an apsectj ProceedingJoinPoint?

Currently I am doing

Class[] parameterTypes = new Class[joinPoint.getArgs().length];
Object[] args = joinPoint.getArgs();
for(int i=0; i<args.length; i++) {
    if(args[i] != null) {
        parameterTypes[i] = args[i].getClass();
    }
    else {
        parameterTypes[i] = null;
    }
}

String methodName = joinPoint.getSignature().getName();
Method method = joinPoint.getSignature()
    .getDeclaringType().getMethod(methodName, parameterTypes);

but I don't think this is the way to go ...

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Erik
  • 11,944
  • 18
  • 87
  • 126

2 Answers2

103

Your method is not wrong, but there's a better one. You have to cast to MethodSignature

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • When used in spring boot application the above cast fails. The signature is then is of type MethodSignatureImpl which is a private inner class of springs MethodInvocationProceedingJoinPoint. – Heri Jun 11 '15 at 18:03
  • 6
    Update to above comment (which is wrong): In my test I imported the wrong MethodSignature class which led to a ClassCastException. The correct class is org.aspectj.lang.reflect.MethodSignature. – Heri Jun 11 '15 at 21:55
  • How could I do this, but instead of get the method, get the class where the method is placed? Would `MethodSignature#getDeclaringType()` do it? – lealceldeiro Aug 09 '18 at 19:34
  • But developer should be sure, joinpoint is method not constructor right? Otherwise ClassCastException. Please correct me if I am wrong. – Volkan Okçu Jan 29 '19 at 10:16
48

You should be careful because Method method = signature.getMethod() will return the method of the interface, you should add this to be sure to get the method of the implementation class:

    if (method.getDeclaringClass().isInterface()) {
        try {
            method= jointPoint.getTarget().getClass().getDeclaredMethod(jointPoint.getSignature().getName(),
                    method.getParameterTypes());
        } catch (final SecurityException exception) {
            //...
        } catch (final NoSuchMethodException exception) {
            //...                
        }
    }

(The code in catch is voluntary empty, you better add code to manage the exception)

With this you'll have the implementation if you want to access method or parameter annotations if this one are not in the interface

MurifoX
  • 14,991
  • 3
  • 36
  • 60
Nordine
  • 481
  • 4
  • 2