package br.com.caelum.vraptor4.interceptor;

import java.lang.reflect.Method;

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

public class InterceptorAcceptsExecutor implements StepExecutor<Boolean>{

	private StepInvoker stepInvoker;
	private InterceptorMethodParametersResolver parameterResolver;
	private Method method;

	public InterceptorAcceptsExecutor(StepInvoker stepInvoker,
			InterceptorMethodParametersResolver parameterResolver,
			Class<?> interceptorClass) {
		super();
		this.stepInvoker = stepInvoker;
		this.parameterResolver = parameterResolver;
		method = stepInvoker.findMethod(Accepts.class, interceptorClass);
	}

	public boolean accept(Class<?> interceptorClass) {
		InternalAcceptsSignature internalAcceptsSignature = new InternalAcceptsSignature(
				new NoStackParameterSignatureAcceptor());
		if (method != null) {
			if (!internalAcceptsSignature.accepts(method)) {
				throw new VRaptorException(internalAcceptsSignature.errorMessage());
			}
			return true;
		}
		return false;
	}

	public Boolean execute(Object interceptor) {
		boolean interceptorAccepts = true;
		Object returnObject = null;
		if(method != null) {
			Object[] params = parameterResolver.parametersFor(method);
			returnObject = stepInvoker.tryToInvoke(interceptor, method, params);
		}
		if (returnObject != null) {
			interceptorAccepts = (Boolean) returnObject;
		}
		return interceptorAccepts;
	}
}

@@ -160,16 +160,56 @@
 ption;%0A%0A
+import com.google.common.base.Objects;%0A%0A
 public c
@@ -552,19 +552,8 @@
 ) %7B%0A
-%09%09super();%0A
 %09%09th
@@ -1126,75 +1126,8 @@
 ) %7B%0A
-%09%09boolean interceptorAccepts = true;%0A%09%09Object returnObject = null;%0A
 %09%09if
@@ -1206,24 +1206,31 @@
 method);%0A%09%09%09
+Object 
 returnObject
@@ -1292,64 +1292,37 @@
 ;%0A%09%09
-%7D%0A%09%09if (
+%09
 return
+ 
 Object
- != null) %7B%0A%09%09%09interceptorAccepts = 
+s.firstNonNull(
 (Boo
@@ -1339,16 +1339,24 @@
 rnObject
+, false)
 ;%0A%09%09%7D%0A%09%09
@@ -1366,29 +1366,14 @@
 urn 
-interceptorAccepts
+true
 ;%0A%09%7D%0A%7D
-%0A

refactor on execute method