Skip to content

Commit

Permalink
Fix the bug when resolving original method to get annotation (#111)
Browse files Browse the repository at this point in the history
- The method from the signature will return the method of interface, so we need to resolve declared method in target class

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 authored Aug 31, 2018
1 parent b61be26 commit 9164bb1
Showing 1 changed file with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.util.StringUtil;
Expand Down Expand Up @@ -53,7 +52,7 @@ public void sentinelResourceAnnotationPointcut() {

@Around("sentinelResourceAnnotationPointcut()")
public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {
Method originMethod = getMethod(pjp);
Method originMethod = resolveMethod(pjp);

SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class);
if (annotation == null) {
Expand Down Expand Up @@ -127,7 +126,7 @@ private Method extractFallbackMethod(ProceedingJoinPoint pjp, String fallbackNam
}

private Method resolveFallbackInternal(ProceedingJoinPoint pjp, /*@NonNull*/ String name) {
Method originMethod = getMethod(pjp);
Method originMethod = resolveMethod(pjp);
Class<?>[] parameterTypes = originMethod.getParameterTypes();
return findMethod(false, pjp.getTarget().getClass(), name, originMethod.getReturnType(), parameterTypes);
}
Expand Down Expand Up @@ -161,7 +160,7 @@ private Method extractBlockHandlerMethod(ProceedingJoinPoint pjp, String name, C

private Method resolveBlockHandlerInternal(ProceedingJoinPoint pjp, /*@NonNull*/ String name, Class<?> clazz,
boolean mustStatic) {
Method originMethod = getMethod(pjp);
Method originMethod = resolveMethod(pjp);
Class<?>[] originList = originMethod.getParameterTypes();
Class<?>[] parameterTypes = Arrays.copyOf(originList, originList.length + 1);
parameterTypes[parameterTypes.length - 1] = BlockException.class;
Expand Down Expand Up @@ -200,8 +199,35 @@ private boolean isStatic(Method method) {
return Modifier.isStatic(method.getModifiers());
}

private Method getMethod(ProceedingJoinPoint joinPoint) {
private Method resolveMethod(ProceedingJoinPoint joinPoint) {
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
return signature.getMethod();
Class<?> targetClass = joinPoint.getTarget().getClass();

Method method = getDeclaredMethodFor(targetClass, signature.getName(), signature.getMethod().getParameterTypes());
if (method == null) {
throw new IllegalStateException("Cannot resolve target method: " + signature.getMethod().getName());
}
return method;
}

/**
* Get declared method with provided name and parameterTypes in given class and its super classes.
* All parameters should be valid.
*
* @param clazz class where the method is located
* @param name method name
* @param parameterTypes method parameter type list
* @return resolved method, null if not found
*/
private Method getDeclaredMethodFor(Class<?> clazz, String name, Class<?>... parameterTypes) {
try {
return clazz.getDeclaredMethod(name, parameterTypes);
} catch (NoSuchMethodException e) {
Class<?> superClass = clazz.getSuperclass();
if (superClass != null) {
return getDeclaredMethodFor(superClass, name, parameterTypes);
}
}
return null;
}
}

0 comments on commit 9164bb1

Please sign in to comment.