1212See the License for the specific language governing permissions and
1313limitations under the License.*/
1414
15- package apijson .server ;
15+ package apijson .orm ;
1616
1717import java .lang .reflect .InvocationTargetException ;
1818import java .util .Arrays ;
3232/**可远程调用的函数类
3333 * @author Lemon
3434 */
35- public class RemoteFunction {
36- // private static final String TAG = "RemoteFunction ";
35+ public class AbstractFunctionParser implements FunctionParser {
36+ // private static final String TAG = "AbstractFunctionParser ";
3737
3838 // <methodName, JSONObject>
3939 // <isContain, <arguments:"array,key", tag:null, methods:null>>
4040 public static final Map <String , JSONObject > FUNCTION_MAP ;
4141 static {
4242 FUNCTION_MAP = new HashMap <>();
4343 }
44-
45- private final RequestMethod method ;
46- private final String tag ;
47- private final int version ;
48- public RemoteFunction (RequestMethod method , String tag , int version ) {
49- this .method = method == null ? RequestMethod .GET : method ;
50- this .tag = tag ;
51- this .version = version ;
44+
45+ private RequestMethod method ;
46+ private String tag ;
47+ private int version ;
48+ private JSONObject request ;
49+ public AbstractFunctionParser () {
50+ this (null , null , 0 , null );
51+ }
52+ public AbstractFunctionParser (RequestMethod method , String tag , int version , @ NotNull JSONObject request ) {
53+ setMethod (method == null ? RequestMethod .GET : method );
54+ setTag (tag );
55+ setVersion (version );
56+ setRequest (request );
5257 }
5358
59+ @ Override
5460 public RequestMethod getMethod () {
5561 return method ;
5662 }
63+ @ Override
64+ public AbstractFunctionParser setMethod (RequestMethod method ) {
65+ this .method = method ;
66+ return this ;
67+ }
68+ @ Override
5769 public String getTag () {
5870 return tag ;
5971 }
72+ @ Override
73+ public AbstractFunctionParser setTag (String tag ) {
74+ this .tag = tag ;
75+ return this ;
76+ }
77+ @ Override
6078 public int getVersion () {
6179 return version ;
6280 }
81+ @ Override
82+ public AbstractFunctionParser setVersion (int version ) {
83+ this .version = version ;
84+ return this ;
85+ }
86+
87+ private String key ;
88+ @ Override
89+ public String getKey () {
90+ return key ;
91+ }
92+ @ Override
93+ public AbstractFunctionParser setKey (String key ) {
94+ this .key = key ;
95+ return this ;
96+ }
97+
98+ private String parentPath ;
99+ @ Override
100+ public String getParentPath () {
101+ return parentPath ;
102+ }
103+ @ Override
104+ public AbstractFunctionParser setParentPath (String parentPath ) {
105+ this .parentPath = parentPath ;
106+ return this ;
107+ }
108+ private String currentName ;
109+ @ Override
110+ public String getCurrentName () {
111+ return currentName ;
112+ }
113+ @ Override
114+ public AbstractFunctionParser setCurrentName (String currentName ) {
115+ this .currentName = currentName ;
116+ return this ;
117+ }
118+
119+ @ NotNull
120+ @ Override
121+ public JSONObject getRequest () {
122+ return request ;
123+ }
124+ @ Override
125+ public AbstractFunctionParser setRequest (@ NotNull JSONObject request ) {
126+ this .request = request ;
127+ return this ;
128+ }
129+
130+ private JSONObject currentObject ;
131+ @ NotNull
132+ @ Override
133+ public JSONObject getCurrentObject () {
134+ return currentObject ;
135+ }
136+ @ Override
137+ public AbstractFunctionParser setCurrentObject (@ NotNull JSONObject currentObject ) {
138+ this .currentObject = currentObject ;
139+ return this ;
140+ }
141+
142+
143+ /**反射调用
144+ * @param function 例如get(object,key),参数只允许引用,不能直接传值
145+ * @param currentObject 不作为第一个参数,就不能远程调用invoke,避免死循环
146+ * @return {@link #invoke(AbstractFunctionParser, String, JSONObject)}
147+ */
148+ @ Override
149+ public Object invoke (@ NotNull String function , @ NotNull JSONObject currentObject ) throws Exception {
150+ return invoke (this , function , currentObject );
151+ }
63152
64153 /**反射调用
65- * @param fun
154+ * @param parser
66155 * @param request
67156 * @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
68- * @return
157+ * @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
69158 */
70- public static Object invoke (@ NotNull RemoteFunction fun , @ NotNull JSONObject request , @ NotNull String function ) throws Exception {
159+ public static Object invoke (@ NotNull AbstractFunctionParser parser , @ NotNull String function , @ NotNull JSONObject currentObject ) throws Exception {
160+
161+ FunctionBean fb = parseFunction (function , currentObject , false );
71162
72- FunctionBean fb = parseFunction (function , request , false );
73-
74163 JSONObject row = FUNCTION_MAP .get (fb .getMethod ());
75164 if (row == null ) {
76165 throw new UnsupportedOperationException ("不允许调用远程函数 " + fb .getMethod () + " !" );
77166 }
78-
167+
79168 int v = row .getIntValue ("version" );
80- if (fun .getVersion () < v ) {
81- throw new UnsupportedOperationException ("不允许 version = " + fun .getVersion () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 version >= " + v + " !" );
169+ if (parser .getVersion () < v ) {
170+ throw new UnsupportedOperationException ("不允许 version = " + parser .getVersion () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 version >= " + v + " !" );
82171 }
83172 String t = row .getString ("tag" );
84- if (t != null && t .equals (fun .getTag ()) == false ) {
85- throw new UnsupportedOperationException ("不允许 tag = " + fun .getTag () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 tag = " + t + " !" );
173+ if (t != null && t .equals (parser .getTag ()) == false ) {
174+ throw new UnsupportedOperationException ("不允许 tag = " + parser .getTag () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 tag = " + t + " !" );
86175 }
87176 String [] methods = StringUtil .split (row .getString ("methods" ));
88177 List <String > ml = methods == null || methods .length <= 0 ? null : Arrays .asList (methods );
89- if (ml != null && ml .contains (fun .getMethod ().toString ()) == false ) {
90- throw new UnsupportedOperationException ("不允许 method = " + fun .getMethod () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 method 在 " + methods + "内 !" );
178+ if (ml != null && ml .contains (parser .getMethod ().toString ()) == false ) {
179+ throw new UnsupportedOperationException ("不允许 method = " + parser .getMethod () + " 的请求调用远程函数 " + fb .getMethod () + " ! 必须满足 method 在 " + methods + "内 !" );
91180 }
92181
93182 try {
94- return invoke (fun , fb .getMethod (), fb .getTypes (), fb .getValues ());
183+ return invoke (parser , fb .getMethod (), fb .getTypes (), fb .getValues ());
95184 } catch (Exception e ) {
96185 if (e instanceof NoSuchMethodException ) {
97186 throw new IllegalArgumentException ("字符 " + function + " 对应的远程函数 " + getFunction (fb .getMethod (), fb .getKeys ()) + " 不在后端工程的DemoFunction内!"
@@ -112,6 +201,16 @@ public static Object invoke(@NotNull RemoteFunction fun, @NotNull JSONObject req
112201 }
113202
114203 }
204+
205+ /**反射调用
206+ * @param methodName
207+ * @param parameterTypes
208+ * @param args
209+ * @return
210+ */
211+ public static Object invoke (@ NotNull AbstractFunctionParser parser , @ NotNull String methodName , @ NotNull Class <?>[] parameterTypes , @ NotNull Object [] args ) throws Exception {
212+ return parser .getClass ().getMethod (methodName , parameterTypes ).invoke (parser , args );
213+ }
115214
116215 /**解析函数
117216 * @param function
@@ -200,16 +299,6 @@ else if (v instanceof JSONArray) { // Collection) {
200299 }
201300
202301
203- /**反射调用
204- * @param methodName
205- * @param parameterTypes
206- * @param args
207- * @return
208- */
209- public static Object invoke (@ NotNull RemoteFunction fun , @ NotNull String methodName , @ NotNull Class <?>[] parameterTypes , @ NotNull Object [] args ) throws Exception {
210- return fun .getClass ().getDeclaredMethod (methodName , parameterTypes ).invoke (fun , args );
211- }
212-
213302 /**
214303 * @param method
215304 * @param keys
@@ -300,7 +389,7 @@ public String toFunctionCallString(boolean useValue, String quote) {
300389 s += (i <= 0 ? "" : "," ) + (arg instanceof Boolean || arg instanceof Number ? arg : quote + arg + quote );
301390 }
302391 }
303-
392+
304393 return s + ")" ;
305394 }
306395
0 commit comments