Skip to content

Commit

Permalink
handling MirrorException and wrapping in a InterceptionException closes
Browse files Browse the repository at this point in the history
  • Loading branch information
csokol authored and Turini committed Aug 28, 2013
1 parent 2aabb2e commit 9ea657b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import java.util.Map;
import java.util.Map.Entry;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

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

import br.com.caelum.vraptor4.Result;
import br.com.caelum.vraptor4.interceptor.ExceptionHandlerInterceptor;
import br.com.caelum.vraptor4.ioc.RequestScoped;
import br.com.caelum.vraptor4.proxy.Proxifier;

/**
Expand All @@ -51,7 +51,7 @@ public class DefaultExceptionMapper

private final Map<Class<? extends Exception>, ExceptionRecorder<Result>> exceptions;
private final Proxifier proxifier;

@Deprecated
public DefaultExceptionMapper() {
this(null);
Expand All @@ -61,7 +61,7 @@ public DefaultExceptionMapper(Proxifier proxifier) {
this.proxifier = proxifier;
this.exceptions = newLinkedHashMap();
}

public Result record(Class<? extends Exception> exception) {
if (exception == null) {
throw new NullPointerException("Exception cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;

import net.vidageek.mirror.dsl.Mirror;
import net.vidageek.mirror.exception.MirrorException;
import net.vidageek.mirror.list.dsl.Matcher;
import net.vidageek.mirror.list.dsl.MirrorList;

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

import br.com.caelum.vraptor4.InterceptionException;

public class StepInvoker {

private static final Logger logger = LoggerFactory
.getLogger(StepInvoker.class);

private class InvokeMatcher implements Matcher<Method> {

private Class<? extends Annotation> step;
Expand All @@ -36,20 +38,31 @@ public boolean accepts(Method element) {

public Object tryToInvoke(Object interceptor,Class<? extends Annotation> step,Object... params) {
Method stepMethod = findMethod(step, interceptor.getClass());
if(stepMethod==null){
if (stepMethod==null){
return null;
}
Object returnObject = createMirror().on(interceptor).invoke().method(stepMethod).withArgs(params);
Object returnObject = invokeMethod(interceptor, stepMethod, params);
if(stepMethod.getReturnType().equals(void.class)){
return new VoidReturn();
}
return returnObject;
}


public Method findMethod(Class<? extends Annotation> step,Class<?> interceptorClass) {
private Object invokeMethod(Object interceptor, Method stepMethod,
Object... params) {
try {
Object returnObject = createMirror().on(interceptor).invoke().method(stepMethod).withArgs(params);
return returnObject;
} catch (MirrorException e) {
throw new InterceptionException(e.getCause());
}
}


public Method findMethod(Class<? extends Annotation> step,Class<?> interceptorClass) {
MirrorList<Method> possibleMethods = createMirror().on(interceptorClass).reflectAll().methods().matching(new InvokeMatcher(step));
if(possibleMethods.size() > 1){
if (possibleMethods.size() > 1) {
throw new IllegalStateException("You should not have more than one @"+step.getSimpleName()+" annotated method");
}
if(possibleMethods.isEmpty()){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package br.com.caelum.vraptor4.interceptor;

import br.com.caelum.vraptor4.BeforeCall;

public class ExceptionThrowerInterceptor {
@BeforeCall
public void intercept() {
throw new RuntimeException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,36 @@
import org.junit.Test;

import br.com.caelum.vraptor4.AroundCall;
import br.com.caelum.vraptor4.BeforeCall;
import br.com.caelum.vraptor4.InterceptionException;
import br.com.caelum.vraptor4.interceptor.example.ExampleOfSimpleStackInterceptor;
import br.com.caelum.vraptor4.interceptor.example.InterceptorWithInheritance;
import br.com.caelum.vraptor4.interceptor.example.WeldProxy$$$StyleInterceptor;

public class StepInvokerTest {

private StepInvoker stepInvoker = new StepInvoker();

@Ignore
public void shouldNotReadInheritedMethods() throws Exception {
stepInvoker.findMethod(AroundCall.class,InterceptorWithInheritance.class);
stepInvoker.findMethod(AroundCall.class,InterceptorWithInheritance.class);
}

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

@Test
public void teste() throws SecurityException, NoSuchMethodException{
assertNotNull(stepInvoker.findMethod(AroundCall.class,WeldProxy$$$StyleInterceptor.class));
}


public void shouldFindMethodFromWeldStyleInterceptor() throws SecurityException, NoSuchMethodException{
assertNotNull(stepInvoker.findMethod(AroundCall.class, WeldProxy$$$StyleInterceptor.class));
}

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

}

0 comments on commit 9ea657b

Please sign in to comment.