All Downloads are FREE. Search and download functionalities are using the official Maven repository.

panda.aop.InterceptorChain Maven / Gradle / Ivy

The newest version!
package panda.aop;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

import panda.log.Logs;

/**
 * 拦截器链,持有被调用方法的信息
 * 
 * @author wendal([email protected])
 */
public class InterceptorChain {

	protected Method callingMethod;

	protected int methodIndex;

	protected Object args[];

	protected AopCallback callingObj;

	protected Object returnValue;

	protected List miList;

	private boolean invoked = false;

	private int currentMI = 0;

	public InterceptorChain(int methodIndex, Object obj, Method method, List miList, Object[] args) {
		this.methodIndex = methodIndex;
		this.callingObj = (AopCallback)obj;
		this.callingMethod = method;
		this.args = args;
		this.miList = miList;
	}

	/**
	 * 继续执行下一个拦截器,如果已经没有剩下的拦截器,则执行原方法
	 * 
	 * @return 拦截器链本身
	 * @throws Throwable 下层拦截器或原方法抛出的一切异常
	 */
	public InterceptorChain doChain() throws Throwable {
		if (currentMI == miList.size())
			invoke();
		else {
			currentMI++;
			miList.get(currentMI - 1).filter(this);
		}
		return this;

	}

	/**
	 * 执行原方法,一般情况下不应该直接被调用
	 * 
	 * @throws Throwable 原方法抛出的一切异常
	 */
	public void invoke() throws Throwable {
		if (invoked) {
			Logs.getLog(getClass()).warn("!! Calling Method more than once! Method --> " +  callingMethod.toString());
		}
		else {
			invoked = true;
		}
		this.returnValue = callingObj._aop_invoke(methodIndex, args);
	}

	/**
	 * 获取返回值
	 * 
	 * @return 返回值
	 */
	public Object getReturn() {
		return returnValue;
	}

	/**
	 * @return 正在被调用的Method
	 */
	public Method getCallingMethod() {
		return callingMethod;
	}

	/**
	 * 方法调用的参数数组,如果你要改变参数,那么必须保证参数类型与方法参数兼容.
	 * @return the arguments array
	 */
	public Object[] getArgs() {
		return args;
	}

	public void setReturnValue(Object returnValue) {
		this.returnValue = returnValue;
	}

	public AopCallback getCallingObj() {
		return callingObj;
	}

	public boolean isInvoked() {
		return invoked;
	}

	/**
	 * 获取当前的方法拦截器列表,注意,这个列表是不可修改的.如果需要修改,那么请通过{@link #setMethodInterceptors(List)}
	 * @return method intercepter list
	 */
	public List getMethodInterceptors() {
		return Collections.unmodifiableList(miList);
	}

	/**
	 * 设置当前调用的方法拦截器列表,注意,这个set只对当前方法调用有效.
	 * 
	 * @param miList method intercepter list
	 */
	public void setMethodInterceptors(List miList) {
		this.miList = miList;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy