forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectedCommandExecutor.java
More file actions
215 lines (201 loc) · 7.84 KB
/
ReflectedCommandExecutor.java
File metadata and controls
215 lines (201 loc) · 7.84 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
package act.cli.bytecode;
import act.app.App;
import act.app.CliContext;
import act.app.data.StringValueResolverManager;
import act.cli.CliError;
import act.cli.CommandExecutor;
import act.cli.meta.*;
import act.cli.util.CommandLineParser;
import act.conf.AppConfig;
import com.esotericsoftware.reflectasm.MethodAccess;
import org.osgl.$;
import org.osgl.util.E;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
/**
* Implement {@link act.cli.CommandExecutor} using
* https://github.com/EsotericSoftware/reflectasm
*/
public class ReflectedCommandExecutor extends CommandExecutor {
private CommanderClassMetaInfo classMetaInfo;
private CommandMethodMetaInfo methodMetaInfo;
private App app;
private ClassLoader cl;
private Class[] paramTypes;
private Class<?> commanderClass;
private Method method;
private MethodAccess methodAccess;
private int commandIndex;
public ReflectedCommandExecutor(CommanderClassMetaInfo classMetaInfo, CommandMethodMetaInfo methodMetaInfo, App app) {
this.classMetaInfo = $.notNull(classMetaInfo);
this.methodMetaInfo = $.notNull(methodMetaInfo);
this.app = $.NPE(app);
this.cl = app.classLoader();
this.paramTypes = paramTypes();
this.commanderClass = $.classForName(methodMetaInfo.classInfo().className(), cl);
if (!methodMetaInfo.isStatic()) {
methodAccess = MethodAccess.get(commanderClass);
commandIndex = methodAccess.getIndex(methodMetaInfo.methodName(), paramTypes);
} else {
try {
method = commanderClass.getMethod(methodMetaInfo.methodName(), paramTypes);
} catch (NoSuchMethodException e) {
throw E.unexpected(e);
}
method.setAccessible(true);
}
}
@Override
public Object execute(CliContext context) {
Object cmd = commanderInstance(context);
Object[] params = params(context);
return invoke(cmd, params);
}
@Override
protected void releaseResources() {
app = null;
cl = null;
commandIndex = 0;
commanderClass = null;
method = null;
methodAccess = null;
paramTypes = null;
super.releaseResources();
}
private Object commanderInstance(CliContext context) {
String commander = commanderClass.getName();
Object inst = context.__commanderInstance(commander);
if (null == inst) {
inst = context.newInstance(commanderClass);
context.__commanderInstance(commander, inst);
}
$.Var<Integer> argIdx = $.var(0);
List<FieldOptionAnnoInfo> list = classMetaInfo.fieldOptionAnnoInfoList(app.classLoader());
for (FieldOptionAnnoInfo fieldOptionAnnoInfo : list) {
String fieldName = fieldOptionAnnoInfo.fieldName();
Object val = optionVal(fieldOptionAnnoInfo.fieldType(), fieldOptionAnnoInfo, argIdx, false, fieldOptionAnnoInfo.readFileContent(), context);
Class instClass = inst.getClass();
try {
Field field = instClass.getField(fieldName);
field.setAccessible(true);
field.set(val, inst);
} catch (Exception e) {
try {
Method method = instClass.getMethod("set" + S.capFirst(fieldName), fieldOptionAnnoInfo.fieldType());
method.invoke(inst, val);
} catch (Exception e1) {
throw E.unexpected("Cannot find the setter for field %s on class %s", fieldName, instClass);
}
}
}
return inst;
}
private Class<?>[] paramTypes() {
int paramCount = methodMetaInfo.paramCount();
Class<?>[] ca = new Class[paramCount];
if (0 == paramCount) {
return ca;
}
for (int i = 0; i < paramCount; ++i) {
CommandParamMetaInfo param = methodMetaInfo.param(i);
String className = param.type().getClassName();
ca[i] = $.classForName(className, cl);
}
return ca;
}
private Object[] params(CliContext ctx) {
int paramCount = methodMetaInfo.paramCount();
int ctxParamCount = methodMetaInfo.ctxParamCount();
Object[] oa = new Object[paramCount];
if (0 == paramCount) {
return oa;
}
$.Var<Integer> argIdx = $.var(0);
for (int i = 0; i < paramCount; ++i) {
CommandParamMetaInfo param = methodMetaInfo.param(i);
Class<?> paramType = paramTypes[i];
if (param.isContext()) {
oa[i] = app.newInstance(paramType);
} else {
oa[i] = optionVal(paramType, param.optionInfo(), argIdx, (paramCount - ctxParamCount) == 1, param.readFileContent(), ctx);
}
}
return oa;
}
private Object optionVal(Class<?> optionType, OptionAnnoInfoBase option, $.Var<Integer> argIdx, boolean useArgumentIfOptionNotFound, boolean readFileContent, CliContext ctx) {
StringValueResolverManager resolverManager = app.resolverManager();
CommandLineParser parser = ctx.commandLine();
List<String> args = ctx.arguments();
if (CliContext.class.equals(optionType)) {
return ctx;
} else if (App.class.equals(optionType)) {
return ctx.app();
} else if (AppConfig.class.equals(optionType)) {
return ctx.app().config();
} else {
String argStr;
if (null == option) {
int i = argIdx.get();
argStr = args.get(i);
argIdx.set(i + 1);
} else {
argStr = parser.getString(option.lead1(), option.lead2());
if (S.blank(argStr)) {
if (useArgumentIfOptionNotFound) {
// try to use the single param as the option
List<String> args0 = parser.arguments();
if (args0.size() == 1) {
return resolverManager.resolve(args0.get(0), optionType);
}
}
if (option.required()) {
throw new CliError("Missing required option [%s]", option);
}
}
}
if (File.class.isAssignableFrom(optionType)) {
if (argStr.startsWith(File.separator) || argStr.startsWith("/")) {
return new File(argStr);
} else {
return new File(ctx.curDir(), argStr);
}
} else if (readFileContent) {
File file;
if (argStr.startsWith(File.separator) || argStr.startsWith("/")) {
file = new File(argStr).getAbsoluteFile();
} else {
file = new File(ctx.curDir(), argStr);
}
if (file.exists()) {
if (List.class.isAssignableFrom(optionType)) {
return IO.readLines(file);
} else {
argStr = IO.readContentAsString(file);
}
}
}
return resolverManager.resolve(argStr, optionType);
}
}
private Object invoke(Object commander, Object[] params) {
Object result;
if (null != methodAccess) {
try {
result = methodAccess.invoke(commander, commandIndex, params);
} catch (Exception e) {
throw E.unexpected(e);
}
} else {
try {
result = method.invoke(null, params);
} catch (Exception e) {
throw E.unexpected(e);
}
}
return result;
}
}