-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReflector.java
More file actions
328 lines (301 loc) · 8.75 KB
/
Reflector.java
File metadata and controls
328 lines (301 loc) · 8.75 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
package api.cli;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import api.util.Tuple;
/**
* The Class Reflector.
*
* @author Gwindow
*/
public class Reflector {
/** The class stack. */
private Stack<Class<?>> classStack = new Stack<Class<?>>();
/** The object stack. */
private Stack<Object> objectStack = new Stack<Object>();
/** The tokenizer. */
private StringTokenizer tokenizer;
/**
* Invoke.
*
* @param input
* the input
*/
public void invoke(String input) {
if (input.startsWith("new")) {
String className = splitClassName(input);
List<String> constructorParams = splitParametersList(input);
try {
Utils.print(className);
Utils.print(constructorParams);
invokeConstructor(className, constructorParams);
} catch (Exception e) {
e.printStackTrace();
}
} else {
String methodName = splitMethodName(input);
List<String> methodParams = splitParametersList(input);
try {
invokeMethod(methodName, methodParams);
Utils.print("method invoked");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Split class name.
*
* @param input
* the input
* @return the string
*/
private String splitClassName(String input) {
String className = input.replace("new", "").trim();
className = className.replaceAll("\\((.*?)\\)", "");
return className;
}
/**
* Split method name.
*
* @param input
* the input
* @return the string
*/
private String splitMethodName(String input) {
String methodName = input.replaceAll("\\((.*?)\\)", "").trim();
return methodName;
}
/**
* Invoke constructor.
*
* @param className
* the class name
* @param params
* the params
* @throws Exception
* the exception
*/
private void invokeConstructor(String className, List<String> params) throws Exception {
Tuple<Class<?>[], Object[]> tuple = translateToParams(params);
invokeConstructor(className, tuple.getA(), tuple.getB());
}
/**
* Invoke method.
*
* @param methodName
* the method name
* @param params
* the params
* @throws Exception
* the exception
*/
private void invokeMethod(String methodName, List<String> params) throws Exception {
Tuple<Class<?>[], Object[]> tuple = translateToParams(params);
invokeMethod(methodName, tuple.getA(), tuple.getB());
}
/**
* Split parameters list.
*
* @param input
* the input
* @return the array list
*/
private ArrayList<String> splitParametersList(String input) {
ArrayList<String> parametersList = new ArrayList<String>();
Pattern pattern = Pattern.compile("\\((.*?)\\)", Pattern.DOTALL);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
tokenizer = new StringTokenizer(matcher.group(1), ",");
while (tokenizer.hasMoreTokens()) {
parametersList.add(tokenizer.nextToken().trim());
}
return parametersList;
}
return null;
}
/**
* Translate to params.
*
* @param parametersList
* the parameters list
* @return the tuple
*/
private Tuple<Class<?>[], Object[]> translateToParams(List<String> parametersList) {
Class<?>[] classParams = new Class[parametersList.size()];
Object[] methodParams = new Object[parametersList.size()];
for (int i = 0; i < parametersList.size(); i++) {
String s;
s = Utils.split(parametersList.get(i), "\"", "\"");
if (s != null) {
classParams[i] = String.class;
methodParams[i] = s;
// System.err.println("string");
} else if (parametersList.get(i).equalsIgnoreCase("true")) {
classParams[i] = boolean.class;
methodParams[i] = true;
// System.err.println("true boolean");
} else if (parametersList.get(i).equalsIgnoreCase("false")) {
classParams[i] = boolean.class;
methodParams[i] = false;
// System.err.println("false boolean");
} else if (parametersList.get(i).equalsIgnoreCase("null") || parametersList.equals("")) {
classParams = null;
methodParams = null;
// System.err.println("null");
} else {
classParams[i] = Integer.class;
methodParams[i] = parametersList.get(i);
// System.err.println("integer");
}
}
return new Tuple<Class<?>[], Object[]>(classParams, methodParams);
}
/**
* Invoke constructor.
*
* @param className
* the class name
* @param classParams
* the class params
* @param constructorParams
* the constructor params
* @throws ClassNotFoundException
* the class not found exception
* @throws SecurityException
* the security exception
* @throws NoSuchMethodException
* the no such method exception
* @throws IllegalArgumentException
* the illegal argument exception
* @throws InstantiationException
* the instantiation exception
* @throws IllegalAccessException
* the illegal access exception
* @throws InvocationTargetException
* the invocation target exception
*/
private void invokeConstructor(String className, Class<?>[] classParams, Object[] constructorParams)
throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
classStack.add(ClassLoader.getClassForKey(className));
Constructor<?> constructor = classStack.peek().getConstructor(classParams);
objectStack.add(constructor.newInstance(constructorParams));
}
/**
* Invoke method.
*
* @param methodName
* the method name
* @param classParams
* the class params
* @param methodParams
* the method params
* @throws SecurityException
* the security exception
* @throws NoSuchMethodException
* the no such method exception
* @throws IllegalArgumentException
* the illegal argument exception
* @throws IllegalAccessException
* the illegal access exception
* @throws InvocationTargetException
* the invocation target exception
*/
private void invokeMethod(String methodName, Class<?>[] classParams, Object[] methodParams) throws SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Method method = classStack.peek().getMethod(methodName, classParams);
// objectStack.add(method.invoke(objectStack.peek(), methodParams));
method.invoke(objectStack.peek(), methodParams);
Utils.printSingleIndent(objectStack.peek());
System.out.println("return type");
System.out.println(method.getGenericReturnType());
}
/**
* Prints the classes.
*/
public void printClasses() {
Utils.print("List of classes");
Utils.printSingleIndent(ClassLoader.getClassMap().keySet());
}
/**
* Prints the methods.
*/
public void printMethods() {
if (!classStack.isEmpty()) {
Utils.print("List of methods");
Class<?> myClass = classStack.peek();
for (int i = 0; i < myClass.getDeclaredMethods().length; i++) {
String name = myClass.getDeclaredMethods()[i].getName();
String params = "";
for (int j = 0; j < myClass.getDeclaredMethods()[i].getParameterTypes().length; j++) {
params = params + myClass.getDeclaredMethods()[i].getParameterTypes()[j].getName() + ",";
}
Utils.printSingleIndent(name + "(" + params + ")");
}
} else {
Utils.print("No classes in stack");
}
}
/**
* Checks if is stacks empty.
*
* @return true, if is stacks empty
*/
public boolean isStacksEmpty() {
if (classStack.isEmpty() || objectStack.isEmpty())
return true;
else
return false;
}
/**
* Clear stacks.
*/
public void clearStacks() {
classStack.clear();
objectStack.clear();
Utils.print("Cleared");
}
/**
* Pop stacks.
*/
public void popStacks() {
try {
classStack.pop();
objectStack.pop();
Utils.print("Popped");
} catch (Exception e) {
Utils.print("Nothing to pop");
}
}
/**
* Prints the stack.
*/
public void printStack() {
try {
Utils.print(classStack.peek().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Utils.print(objectStack.peek().toString());
System.out.println("Size: " + objectStack.size());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!objectStack.empty()) {
Utils.print("Current Object: " + classStack.peek().toString().replace("class ", ""));
Utils.print(classStack.toString().replace("class ", ""));
} else {
Utils.print("Stack empty");
}
}
}