forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlerMethodMetaInfo.java
More file actions
353 lines (300 loc) · 9.37 KB
/
HandlerMethodMetaInfo.java
File metadata and controls
353 lines (300 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package act.controller.meta;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.asm.Label;
import act.asm.Type;
import act.handler.builtin.controller.ControllerAction;
import act.handler.builtin.controller.Handler;
import act.sys.meta.InvokeType;
import act.sys.meta.ReturnTypeInfo;
import act.util.DestroyableBase;
import act.util.Prioritised;
import act.util.PropertySpec;
import org.osgl.$;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import java.util.HashMap;
import java.util.Map;
/**
* Common meta data storage for both {@link ControllerAction}
* and {@link Handler}
*/
public abstract class HandlerMethodMetaInfo<T extends HandlerMethodMetaInfo> extends DestroyableBase implements Prioritised {
private String name;
private InvokeType invokeType;
private ActContextInjection actContextInjection;
private ControllerClassMetaInfo clsInfo;
private C.List<HandlerParamMetaInfo> params = C.newList();
private volatile String fullName;
private ReturnTypeInfo returnType;
private String dateFormatPattern;
private boolean returnArray; // for performance tuning
private boolean throwRenderResult;
private PropertySpec.MetaInfo propertySpec;
private Map<Label, Map<Integer, LocalVariableMetaInfo>> locals = new HashMap<>();
private int appCtxLVT_id = -1;
private int ctxParamCnt = -1;
/**
* Construct a `HandlerMethodMetaInfo` from a copy with a different class info. This could be used
* to get Interceptors from parent controller
*
* @param copy an existing HandlerMethodMetaInfo
* @param clsInfo the new class info, usually a extended controller class
*/
protected HandlerMethodMetaInfo(HandlerMethodMetaInfo copy, ControllerClassMetaInfo clsInfo) {
E.illegalArgumentIf(!clsInfo.isMyAncestor(copy.classInfo()));
this.clsInfo = $.requireNotNull(clsInfo);
this.name = copy.name;
this.invokeType = copy.invokeType;
this.actContextInjection = copy.actContextInjection;
this.params = copy.params;
this.returnType = copy.returnType;
this.returnArray = copy.returnArray;
this.propertySpec = copy.propertySpec;
this.locals = copy.locals;
this.appCtxLVT_id = copy.appCtxLVT_id;
this.ctxParamCnt = copy.ctxParamCnt;
}
public HandlerMethodMetaInfo(ControllerClassMetaInfo clsInfo) {
this.clsInfo = clsInfo;
}
@Override
protected void releaseResources() {
clsInfo.destroy();
params.clear();
locals.clear();
super.releaseResources();
}
public ControllerClassMetaInfo classInfo() {
return clsInfo;
}
public T name(String name) {
this.name = name;
return me();
}
public String name() {
return name;
}
public String fullName() {
if (null == fullName) {
synchronized (this) {
if (null == fullName) {
fullName = S.concat(classInfo().className(), ".", name);
}
}
}
return fullName;
}
public T dateFormatPattern(String pattern) {
this.dateFormatPattern = pattern;
return me();
}
public String dateFormatPattern() {
return this.dateFormatPattern;
}
@Override
public int priority() {
return -1;
}
public T appContextViaField(String fieldName) {
actContextInjection = new ActContextInjection.FieldActContextInjection(fieldName);
return me();
}
public T appContextViaParam(int paramIndex) {
actContextInjection = new ActContextInjection.ParamAppContextInjection(paramIndex);
return me();
}
public T appContextViaLocalStorage() {
actContextInjection = new ActContextInjection.LocalAppContextInjection();
return me();
}
public ActContextInjection appContextInjection() {
return actContextInjection;
}
public T invokeStaticMethod() {
invokeType = InvokeType.STATIC;
return me();
}
public T invokeInstanceMethod() {
invokeType = InvokeType.VIRTUAL;
return me();
}
public boolean isStatic() {
return InvokeType.STATIC == invokeType;
}
public HandlerMethodMetaInfo propertySpec(PropertySpec.MetaInfo propertySpec) {
this.propertySpec = propertySpec;
return this;
}
public PropertySpec.MetaInfo propertySpec() {
return propertySpec;
}
public T setThrowRenderResult() {
this.throwRenderResult = true;
return me();
}
public boolean throwRenderResult() {
return throwRenderResult;
}
public T returnType(Type type) {
returnType = ReturnTypeInfo.of(type);
returnArray = type.getDescriptor().startsWith("[");
return me();
}
public T appCtxLocalVariableTableIndex(int index) {
appCtxLVT_id = index;
return me();
}
public int appCtxLocalVariableTableIndex() {
return appCtxLVT_id;
}
public Type returnType() {
return returnType.type();
}
public boolean isReturnArray() {
return returnArray;
}
public ReturnTypeInfo returnTypeInfo() {
return returnType;
}
public Type returnComponentType() {
return returnType.componentType();
}
public HandlerMethodMetaInfo returnComponentType(Type type) {
returnType.componentType(type);
return this;
}
public boolean hasReturn() {
return returnType.hasReturn();
}
public boolean hasReturnOrThrowResult() {
return hasReturn() || throwRenderResult;
}
public boolean hasLocalVariableTable() {
return !locals.isEmpty();
}
public HandlerMethodMetaInfo addParam(HandlerParamMetaInfo param) {
params.add(param);
return this;
}
public T addLocal(LocalVariableMetaInfo local) {
Label start = local.start();
Map<Integer, LocalVariableMetaInfo> m = locals.get(start);
if (null == m) {
m = new HashMap<>();
locals.put(start, m);
}
int index = local.index();
E.illegalStateIf(m.containsKey(index), "Local variable index conflict");
m.put(local.index(), local);
return me();
}
public LocalVariableMetaInfo localVariable(int index, Label start) {
Map<Integer, LocalVariableMetaInfo> l = locals.get(start);
if (null == l) return null;
return l.get(index);
}
public HandlerParamMetaInfo param(int id) {
return params.get(id);
}
public int paramCount() {
return params.size();
}
public synchronized int ctxParamCount() {
if (ctxParamCnt < 0) {
if (paramCount() == 0) {
ctxParamCnt = 0;
} else {
ctxParamCnt = 0;
for (HandlerParamMetaInfo param : params) {
if (param.isContext()) {
ctxParamCnt ++;
}
}
}
}
return ctxParamCnt;
}
@Override
public int hashCode() {
return $.hc(fullName());
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof HandlerMethodMetaInfo) {
HandlerMethodMetaInfo that = (HandlerMethodMetaInfo) obj;
return $.eq(that.fullName(), fullName());
}
return false;
}
@Override
public String toString() {
return toStrBuffer(S.newBuffer()).toString();
}
protected S.Buffer toStrBuffer(S.Buffer sb) {
sb.append(actContextInjection).append(" ");
sb.append(_invokeType())
.append(_return())
.append(fullName())
.append("(")
.append(_params())
.append(")");
return sb;
}
private String _invokeType() {
if (null == invokeType) {
return "";
}
switch (invokeType) {
case VIRTUAL:
return "";
case STATIC:
return "static ";
default:
assert false;
return "";
}
}
private String _return() {
if (null == returnType) {
return "";
}
if (returnType.hasReturn()) {
return returnType.type().getClassName() + " ";
} else {
return "";
}
}
private String _params() {
return S.join(", ", params.map(new $.Transformer<HandlerParamMetaInfo, String>() {
@Override
public String transform(HandlerParamMetaInfo paramMetaInfo) {
return paramMetaInfo.type().getClassName();
}
}));
}
private T me() {
return (T) this;
}
}