Interceptor API tweak

This commit is contained in:
James Agnew 2019-01-20 14:37:16 -05:00
parent c22aeaa2e5
commit af9153d64e
2 changed files with 22 additions and 7 deletions

View File

@ -35,6 +35,14 @@ public interface IInterceptorRegistry {
@VisibleForTesting @VisibleForTesting
void clearAnonymousHookForUnitTest(); void clearAnonymousHookForUnitTest();
/**
* Register an interceptor
*
* @param theInterceptor The interceptor to register
* @return Returns <code>true</code> if at least one valid hook method was found on this interceptor
*/
boolean registerGlobalInterceptor(Object theInterceptor);
/** /**
* Invoke the interceptor methods * Invoke the interceptor methods
*/ */

View File

@ -95,14 +95,17 @@ public class InterceptorRegistry implements IInterceptorRegistry, ApplicationCon
} }
private void registerGlobalInterceptor(Object theNextInterceptor) { @Override
public boolean registerGlobalInterceptor(Object theInterceptor) {
boolean retVal = false;
int typeOrder = DEFAULT_ORDER; int typeOrder = DEFAULT_ORDER;
Order typeOrderAnnotation = AnnotationUtils.findAnnotation(theNextInterceptor.getClass(), Order.class); Order typeOrderAnnotation = AnnotationUtils.findAnnotation(theInterceptor.getClass(), Order.class);
if (typeOrderAnnotation != null) { if (typeOrderAnnotation != null) {
typeOrder = typeOrderAnnotation.value(); typeOrder = typeOrderAnnotation.value();
} }
for (Method nextMethod : theNextInterceptor.getClass().getDeclaredMethods()) { for (Method nextMethod : theInterceptor.getClass().getDeclaredMethods()) {
Hook hook = AnnotationUtils.findAnnotation(nextMethod, Hook.class); Hook hook = AnnotationUtils.findAnnotation(nextMethod, Hook.class);
if (hook != null) { if (hook != null) {
@ -113,14 +116,16 @@ public class InterceptorRegistry implements IInterceptorRegistry, ApplicationCon
methodOrder = methodOrderAnnotation.value(); methodOrder = methodOrderAnnotation.value();
} }
HookInvoker invoker = new HookInvoker(hook, theNextInterceptor, nextMethod, methodOrder); HookInvoker invoker = new HookInvoker(hook, theInterceptor, nextMethod, methodOrder);
for (Pointcut nextPointcut : hook.value()) { for (Pointcut nextPointcut : hook.value()) {
myInvokers.put(nextPointcut, invoker); myInvokers.put(nextPointcut, invoker);
} }
retVal = true;
} }
} }
myGlobalInterceptors.add(theNextInterceptor); myGlobalInterceptors.add(theInterceptor);
// Make sure we're always sorted according to the order declared in // Make sure we're always sorted according to the order declared in
// @Order // @Order
@ -129,6 +134,8 @@ public class InterceptorRegistry implements IInterceptorRegistry, ApplicationCon
List<BaseInvoker> nextInvokerList = myInvokers.get(nextPointcut); List<BaseInvoker> nextInvokerList = myInvokers.get(nextPointcut);
nextInvokerList.sort(Comparator.naturalOrder()); nextInvokerList.sort(Comparator.naturalOrder());
} }
return retVal;
} }
private void sortByOrderAnnotation(List<Object> theObjects) { private void sortByOrderAnnotation(List<Object> theObjects) {