Skip to content

Commit

Permalink
Merge pull request #119 from caelum/OneReflectionInsteadFive
Browse files Browse the repository at this point in the history
One reflection instead five
  • Loading branch information
Turini committed Sep 24, 2013
2 parents 88b4fa9 + b94f75a commit 79bcb85
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.lang.reflect.Method;

import br.com.caelum.vraptor.AroundCall;

public class AroundExecutor implements StepExecutor<Object> {

private final StepInvoker stepInvoker;
Expand All @@ -12,10 +10,10 @@ public class AroundExecutor implements StepExecutor<Object> {

public AroundExecutor(StepInvoker stepInvoker,
InterceptorMethodParametersResolver parametersResolver,
Class<?> interceptorClass) {
Method method, Class<?> interceptorClass) {
this.stepInvoker = stepInvoker;
this.parametersResolver = parametersResolver;
this.method = stepInvoker.findMethod(AroundCall.class,interceptorClass);
this.method = method;
}

public boolean accept(Class<?> interceptorClass) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package br.com.caelum.vraptor.interceptor;

import static org.slf4j.LoggerFactory.getLogger;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import net.vidageek.mirror.list.dsl.MirrorList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.caelum.vraptor.Accepts;
import br.com.caelum.vraptor.AfterCall;
import br.com.caelum.vraptor.AroundCall;
import br.com.caelum.vraptor.BeforeCall;
import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.VRaptorException;
Expand All @@ -17,13 +25,13 @@ public class AspectStyleInterceptorHandler implements InterceptorHandler {
private final StepInvoker stepInvoker;
private final Container container;
private final Class<?> interceptorClass;
private static final Logger logger = LoggerFactory
.getLogger(AspectStyleInterceptorHandler.class);
private static final Logger logger = getLogger(AspectStyleInterceptorHandler.class);
private final InterceptorMethodParametersResolver parametersResolver;
private StepExecutor<Boolean> acceptsExecutor;
private StepExecutor<?> after;
private StepExecutor<?> around;
private StepExecutor<?> before;
private MirrorList<Method> interceptorMethods;

public AspectStyleInterceptorHandler(Class<?> interceptorClass, StepInvoker stepInvoker,
Container container, InterceptorMethodParametersResolver parametersResolver) {
Expand All @@ -32,14 +40,15 @@ public AspectStyleInterceptorHandler(Class<?> interceptorClass, StepInvoker step
this.stepInvoker = stepInvoker;
this.container = container;
this.parametersResolver = parametersResolver;
this.interceptorMethods = stepInvoker.findAllMethods(interceptorClass);
configure();
}

private void configure() {

after = new NoStackParameterStepExecutor(stepInvoker, AfterCall.class, interceptorClass);
around = new AroundExecutor(stepInvoker,parametersResolver, interceptorClass);
before = new NoStackParameterStepExecutor(stepInvoker, BeforeCall.class, interceptorClass);
after = new NoStackParameterStepExecutor(stepInvoker, find(AfterCall.class), interceptorClass);
around = new AroundExecutor(stepInvoker,parametersResolver, find(AroundCall.class), interceptorClass);
before = new NoStackParameterStepExecutor(stepInvoker, find(BeforeCall.class), interceptorClass);

boolean doNotAcceptAfter = !after.accept(interceptorClass);
boolean doNotAcceptAround = !around.accept(interceptorClass);
Expand All @@ -54,8 +63,8 @@ private void configure() {
"at least one method whith @AfterCall, @AroundCall or @BeforeCall annotation");
}

CustomAcceptsExecutor customAcceptsExecutor = new CustomAcceptsExecutor(stepInvoker, container, interceptorClass);
InterceptorAcceptsExecutor interceptorAcceptsExecutor = new InterceptorAcceptsExecutor(stepInvoker, parametersResolver, interceptorClass);
CustomAcceptsExecutor customAcceptsExecutor = new CustomAcceptsExecutor(stepInvoker, container, find(CustomAcceptsFailCallback.class), interceptorClass);
InterceptorAcceptsExecutor interceptorAcceptsExecutor = new InterceptorAcceptsExecutor(stepInvoker, parametersResolver, find(Accepts.class), interceptorClass);
boolean customAccepts = customAcceptsExecutor.accept(interceptorClass);
boolean internalAccepts = interceptorAcceptsExecutor.accept(interceptorClass);
if(customAccepts && internalAccepts){
Expand All @@ -64,6 +73,10 @@ private void configure() {
this.acceptsExecutor = customAccepts?customAcceptsExecutor:interceptorAcceptsExecutor;
}

private Method find(Class<? extends Annotation> step) {
return stepInvoker.findMethod(interceptorMethods, step, interceptorClass);
}

public void execute(InterceptorStack stack,
ControllerMethod controllerMethod, Object currentController) {
Object interceptor = container.instanceFor(interceptorClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class CustomAcceptsExecutor implements StepExecutor<Boolean> {
private Container container;
private Method method;

public CustomAcceptsExecutor(StepInvoker stepInvoker, Container container, Class<?> interceptorClass) {
super();
public CustomAcceptsExecutor(StepInvoker stepInvoker,
Container container, Method method, Class<?> interceptorClass) {

this.stepInvoker = stepInvoker;
this.container = container;
method = stepInvoker.findMethod(CustomAcceptsFailCallback.class, interceptorClass);
this.method = method;
}


@Override
public boolean accept(Class<?> interceptorClass){
List<Annotation> constraints = CustomAcceptsVerifier.getCustomAcceptsAnnotations(interceptorClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.lang.reflect.Method;

import br.com.caelum.vraptor.Accepts;
import br.com.caelum.vraptor.VRaptorException;

import com.google.common.base.Objects;
Expand All @@ -15,10 +14,11 @@ public class InterceptorAcceptsExecutor implements StepExecutor<Boolean>{

public InterceptorAcceptsExecutor(StepInvoker stepInvoker,
InterceptorMethodParametersResolver parameterResolver,
Class<?> interceptorClass) {
Method method, Class<?> interceptorClass) {

this.stepInvoker = stepInvoker;
this.parameterResolver = parameterResolver;
method = stepInvoker.findMethod(Accepts.class, interceptorClass);
this.method = method;
}

public boolean accept(Class<?> interceptorClass) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package br.com.caelum.vraptor.interceptor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class NoStackParameterStepExecutor implements StepExecutor<Void> {
Expand All @@ -9,9 +8,9 @@ public class NoStackParameterStepExecutor implements StepExecutor<Void> {
private Method method;

public NoStackParameterStepExecutor(StepInvoker stepInvoker,
Class<? extends Annotation> step, Class<?> interceptorClass) {
Method method, Class<?> interceptorClass) {
this.stepInvoker = stepInvoker;
this.method = stepInvoker.findMethod(step, interceptorClass);
this.method = method;
}

public boolean accept(Class<?> interceptorClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ private Object invokeMethod(Object interceptor, Method stepMethod, Object... par
}
}

public Method findMethod(Class<? extends Annotation> step,Class<?> interceptorClass) {
MirrorList<Method> possibleMethods = findPossibleMethods(step, interceptorClass);
if (possibleMethods.size() > 1 && isNotSameClass(possibleMethods, interceptorClass)) {
throw new IllegalStateException("You should not " +
"have more than one @"+step.getSimpleName()+" annotated method");
}
return possibleMethods.isEmpty() ? null : possibleMethods.get(0);
}

private MirrorList<Method> findPossibleMethods(Class<? extends Annotation> step, Class<?> classs) {
return new Mirror().on(classs).reflectAll().methods().matching(new InvokeMatcher(step));
}

private boolean isNotSameClass(MirrorList<Method> methods, Class<?> interceptorClass) {

for (Method possibleMethod : methods) {
Expand All @@ -78,4 +65,18 @@ private boolean isNotSameClass(MirrorList<Method> methods, Class<?> interceptorC
return true;
}

}
public MirrorList<Method> findAllMethods(Class<?> interceptorClass) {
return new Mirror().on(interceptorClass).reflectAll().methods();
}

public Method findMethod(MirrorList<Method> interceptorMethods,
Class<? extends Annotation> step, Class<?> interceptorClass) {

MirrorList<Method> possibleMethods = interceptorMethods.matching(new InvokeMatcher(step));
if (possibleMethods.size() > 1 && isNotSameClass(possibleMethods, interceptorClass)) {
throw new IllegalStateException("You should not " +
"have more than one @"+step.getSimpleName()+" annotated method");
}
return possibleMethods.isEmpty() ? null : possibleMethods.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.spy;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import net.vidageek.mirror.list.dsl.MirrorList;

import org.junit.Test;

import br.com.caelum.vraptor.AroundCall;
Expand All @@ -22,30 +25,39 @@ public class StepInvokerTest {
@Test
public void shouldNotReadInheritedMethods() throws Exception {
Class<?> interceptorClass = InterceptorWithInheritance.class;
Method method = stepInvoker.findMethod(BeforeCall.class,interceptorClass);
Method method = findMethod(interceptorClass, BeforeCall.class);
assertEquals(method, interceptorClass.getDeclaredMethod("begin"));
}

@Test(expected=IllegalStateException.class)
public void shouldThrowsExceptionWhenInterceptorHasMoreThanOneAnnotatedMethod() {
stepInvoker.findMethod(BeforeCall.class,InterceptorWithMoreThanOneBeforeCallMethod.class);
Class<?> interceptorClass = InterceptorWithMoreThanOneBeforeCallMethod.class;
findMethod(interceptorClass, BeforeCall.class);
}

@Test
public void shouldFindFirstMethodAnnotatedWithInterceptorStep(){
ExampleOfSimpleStackInterceptor proxy = spy(new ExampleOfSimpleStackInterceptor());
assertNotNull(stepInvoker.findMethod(AroundCall.class, proxy.getClass()));
findMethod(proxy.getClass(), BeforeCall.class);
}

@Test
public void shouldFindMethodFromWeldStyleInterceptor() throws SecurityException, NoSuchMethodException{
assertNotNull(stepInvoker.findMethod(AroundCall.class, WeldProxy$$$StyleInterceptor.class));
Class<?> interceptorClass = WeldProxy$$$StyleInterceptor.class;
assertNotNull(findMethod(interceptorClass, AroundCall.class));
}

@Test(expected=InterceptionException.class)
public void shouldWrapMirrorException() throws SecurityException, NoSuchMethodException {
Method method = stepInvoker.findMethod(BeforeCall.class, ExceptionThrowerInterceptor.class);
Class<ExceptionThrowerInterceptor> interceptorClass = ExceptionThrowerInterceptor.class;
Method method = findMethod(interceptorClass, BeforeCall.class);
assertNotNull(method);
stepInvoker.tryToInvoke(new ExceptionThrowerInterceptor(), method);
}
}

private Method findMethod(Class<?> interceptorClass, Class<? extends Annotation> step) {
MirrorList<Method> methods = stepInvoker.findAllMethods(interceptorClass);
Method method = stepInvoker.findMethod(methods, step, interceptorClass);
return method;
}
}

0 comments on commit 79bcb85

Please sign in to comment.