|
|
@@ -29,9 +29,9 @@ import com.jfinal.core.Controller;
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public class Invocation {
|
|
|
-
|
|
|
+
|
|
|
private static final Object[] NULL_ARGS = new Object[0]; // Prevent new Object[0] by jvm for args of method invoking
|
|
|
-
|
|
|
+
|
|
|
private Action action;
|
|
|
private Object target;
|
|
|
private Method method;
|
|
|
@@ -39,53 +39,53 @@ public class Invocation {
|
|
|
private Callback callback;
|
|
|
private Interceptor[] inters;
|
|
|
private Object returnValue;
|
|
|
-
|
|
|
+
|
|
|
private int index = 0;
|
|
|
-
|
|
|
+
|
|
|
public Invocation(Object target, Long proxyMethodKey, Callback callback, Object... args) {
|
|
|
this.action = null;
|
|
|
this.target = target;
|
|
|
-
|
|
|
+
|
|
|
ProxyMethod proxyMethod = ProxyMethodCache.get(proxyMethodKey);
|
|
|
this.method = proxyMethod.getMethod();
|
|
|
this.inters = proxyMethod.getInterceptors();
|
|
|
-
|
|
|
+
|
|
|
this.callback = callback;
|
|
|
this.args = args;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Invocation(Object target, Long proxyMethodKey, Callback callback) {
|
|
|
this(target, proxyMethodKey, callback, NULL_ARGS);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 用于扩展 ProxyFactory
|
|
|
*/
|
|
|
public Invocation(Object target, Method method, Interceptor[] inters, Callback callback, Object[] args) {
|
|
|
this.action = null;
|
|
|
this.target = target;
|
|
|
-
|
|
|
+
|
|
|
this.method = method;
|
|
|
this.inters = inters;
|
|
|
-
|
|
|
+
|
|
|
this.callback = callback;
|
|
|
this.args = args;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// InvocationWrapper need this constructor
|
|
|
protected Invocation() {
|
|
|
this.action = null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Invocation(Action action, Controller controller) {
|
|
|
this.action = action;
|
|
|
this.inters = action.getInterceptors();
|
|
|
this.target = controller;
|
|
|
-
|
|
|
+
|
|
|
// this.args = NULL_ARGS;
|
|
|
this.args = action.getParameterGetter().get(action, controller);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void invoke() {
|
|
|
if (index < inters.length) {
|
|
|
inters[index++].intercept(this);
|
|
|
@@ -114,23 +114,25 @@ public class Invocation {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Object getArg(int index) {
|
|
|
- if (index >= args.length)
|
|
|
+ if (index >= args.length) {
|
|
|
throw new ArrayIndexOutOfBoundsException();
|
|
|
+ }
|
|
|
return args[index];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void setArg(int index, Object value) {
|
|
|
- if (index >= args.length)
|
|
|
+ if (index >= args.length) {
|
|
|
throw new ArrayIndexOutOfBoundsException();
|
|
|
+ }
|
|
|
args[index] = value;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Object[] getArgs() {
|
|
|
return args;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Get the target object which be intercepted
|
|
|
* <pre>
|
|
|
@@ -141,71 +143,70 @@ public class Invocation {
|
|
|
public <T> T getTarget() {
|
|
|
return (T)target;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Return the method of this action.
|
|
|
* <p>
|
|
|
* You can getMethod.getAnnotations() to get annotation on action method to do more things
|
|
|
*/
|
|
|
public Method getMethod() {
|
|
|
- if (action != null)
|
|
|
- return action.getMethod();
|
|
|
- return method;
|
|
|
+ return action != null ? action.getMethod() : method;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Return the method name of this action's method.
|
|
|
*/
|
|
|
public String getMethodName() {
|
|
|
- if (action != null)
|
|
|
- return action.getMethodName();
|
|
|
- return method.getName();
|
|
|
+ return action != null ? action.getMethodName() : method.getName();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Get the return value of the target method
|
|
|
*/
|
|
|
public <T> T getReturnValue() {
|
|
|
return (T)returnValue;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Set the return value of the target method
|
|
|
*/
|
|
|
public void setReturnValue(Object returnValue) {
|
|
|
this.returnValue = returnValue;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// ---------
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Return the controller of this action.
|
|
|
*/
|
|
|
public Controller getController() {
|
|
|
- if (action == null)
|
|
|
+ if (action == null) {
|
|
|
throw new RuntimeException("This method can only be used for action interception");
|
|
|
+ }
|
|
|
return (Controller)target;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Return the action key.
|
|
|
* actionKey = controllerPath + methodName
|
|
|
*/
|
|
|
public String getActionKey() {
|
|
|
- if (action == null)
|
|
|
+ if (action == null) {
|
|
|
throw new RuntimeException("This method can only be used for action interception");
|
|
|
+ }
|
|
|
return action.getActionKey();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Return the controller path.
|
|
|
*/
|
|
|
public String getControllerPath() {
|
|
|
- if (action == null)
|
|
|
+ if (action == null) {
|
|
|
throw new RuntimeException("This method can only be used for action interception");
|
|
|
+ }
|
|
|
return action.getControllerPath();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 该方法已改名为 getControllerPath()
|
|
|
*/
|
|
|
@@ -213,16 +214,17 @@ public class Invocation {
|
|
|
public String getControllerKey() {
|
|
|
return getControllerPath();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Return view path of this controller.
|
|
|
*/
|
|
|
public String getViewPath() {
|
|
|
- if (action == null)
|
|
|
+ if (action == null) {
|
|
|
throw new RuntimeException("This method can only be used for action interception");
|
|
|
+ }
|
|
|
return action.getViewPath();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* return true if it is action invocation.
|
|
|
*/
|