Conseguir el java.lang.reflejar.¿Método de un punto común de procedimiento?


La pregunta es corta y simple: ¿Hay alguna manera de obtener el objeto Method de un punto de procedimiento apsectj?

Actualmente estoy haciendo

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);

Pero no creo que este sea el camino a seguir ...

Author: Bozho, 2011-04-19

2 answers

Tu método no está mal, pero hay uno mejor. Tienes que lanzar a MethodSignature

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
 72
Author: Bozho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-19 11:31:27

Debe tener cuidado porque Method method = signature.getMethod() devolverá el método de la interfaz, debe agregar esto para asegurarse de obtener el método de la clase de implementación:

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

(El código en catch es voluntario vacío, es mejor agregar código para administrar la excepción)

Con esto tendrás la implementación si quieres acceder a anotaciones de método o parámetro si éste no está en la interfaz

 39
Author: Nordine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-26 12:46:27