12
12
See the License for the specific language governing permissions and
13
13
limitations under the License.*/
14
14
15
- package apijson .server ;
15
+ package apijson .orm ;
16
16
17
17
import java .lang .reflect .InvocationTargetException ;
18
18
import java .util .Arrays ;
32
32
/**可远程调用的函数类
33
33
* @author Lemon
34
34
*/
35
- public class RemoteFunction {
36
- // private static final String TAG = "RemoteFunction ";
35
+ public class AbstractFunctionParser implements FunctionParser {
36
+ // private static final String TAG = "AbstractFunctionParser ";
37
37
38
38
// <methodName, JSONObject>
39
39
// <isContain, <arguments:"array,key", tag:null, methods:null>>
40
40
public static final Map <String , JSONObject > FUNCTION_MAP ;
41
41
static {
42
42
FUNCTION_MAP = new HashMap <>();
43
43
}
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 );
52
57
}
53
58
59
+ @ Override
54
60
public RequestMethod getMethod () {
55
61
return method ;
56
62
}
63
+ @ Override
64
+ public AbstractFunctionParser setMethod (RequestMethod method ) {
65
+ this .method = method ;
66
+ return this ;
67
+ }
68
+ @ Override
57
69
public String getTag () {
58
70
return tag ;
59
71
}
72
+ @ Override
73
+ public AbstractFunctionParser setTag (String tag ) {
74
+ this .tag = tag ;
75
+ return this ;
76
+ }
77
+ @ Override
60
78
public int getVersion () {
61
79
return version ;
62
80
}
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
+ }
63
152
64
153
/**反射调用
65
- * @param fun
154
+ * @param parser
66
155
* @param request
67
156
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
68
- * @return
157
+ * @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
69
158
*/
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 );
71
162
72
- FunctionBean fb = parseFunction (function , request , false );
73
-
74
163
JSONObject row = FUNCTION_MAP .get (fb .getMethod ());
75
164
if (row == null ) {
76
165
throw new UnsupportedOperationException ("不允许调用远程函数 " + fb .getMethod () + " !" );
77
166
}
78
-
167
+
79
168
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 + " !" );
82
171
}
83
172
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 + " !" );
86
175
}
87
176
String [] methods = StringUtil .split (row .getString ("methods" ));
88
177
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 + "内 !" );
91
180
}
92
181
93
182
try {
94
- return invoke (fun , fb .getMethod (), fb .getTypes (), fb .getValues ());
183
+ return invoke (parser , fb .getMethod (), fb .getTypes (), fb .getValues ());
95
184
} catch (Exception e ) {
96
185
if (e instanceof NoSuchMethodException ) {
97
186
throw new IllegalArgumentException ("字符 " + function + " 对应的远程函数 " + getFunction (fb .getMethod (), fb .getKeys ()) + " 不在后端工程的DemoFunction内!"
@@ -112,6 +201,16 @@ public static Object invoke(@NotNull RemoteFunction fun, @NotNull JSONObject req
112
201
}
113
202
114
203
}
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
+ }
115
214
116
215
/**解析函数
117
216
* @param function
@@ -200,16 +299,6 @@ else if (v instanceof JSONArray) { // Collection) {
200
299
}
201
300
202
301
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
-
213
302
/**
214
303
* @param method
215
304
* @param keys
@@ -300,7 +389,7 @@ public String toFunctionCallString(boolean useValue, String quote) {
300
389
s += (i <= 0 ? "" : "," ) + (arg instanceof Boolean || arg instanceof Number ? arg : quote + arg + quote );
301
390
}
302
391
}
303
-
392
+
304
393
return s + ")" ;
305
394
}
306
395
0 commit comments