programing

java.lang.reflect를 가져오는 중입니다.Proceeding JoinPoint의 메서드?

stoneblock 2023. 8. 12. 09:47

java.lang.reflect를 가져오는 중입니다.Proceeding JoinPoint의 메서드?

질문은 짧고 간단합니다.appectjProceedingJoinPoint에서 메서드 개체를 가져올 수 있는 방법이 있습니까?

현재 저는 하고 있습니다.

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

하지만 이 방법은 아닌 것 같아요...

당신의 방법이 틀린 것은 아니지만, 더 좋은 방법이 있습니다.캐스팅을 해야 합니다.

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();

당신은 조심해야 합니다 왜냐하면Method method = signature.getMethod()인터페이스의 메서드를 반환합니다. 구현 클래스의 메서드를 가져오려면 이 메서드를 추가해야 합니다.

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

(캐치된 코드는 자발적으로 비어 있습니다. 예외를 관리할 코드를 추가하는 것이 좋습니다.)

인터페이스에 없는 메서드 또는 매개 변수 주석에 액세스하려는 경우 이를 사용하여 구현할 수 있습니다.

언급URL : https://stackoverflow.com/questions/5714411/getting-the-java-lang-reflect-method-from-a-proceedingjoinpoint