-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionMachine.java
187 lines (156 loc) · 5.5 KB
/
ReflectionMachine.java
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
package Forth;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionMachine {
static Field getField(Object actor, String name){
Field f = null;
try {
f = actor.getClass().getField(name);
} catch (NoSuchFieldException ignored) {
}
return f;
}
static Object getByString(Object actor, String name){
Field f = getField(actor, name);
Method m = null;
for(Method p:actor.getClass().getMethods()){
if(p.getName().equals(name)){
m = p;
break;
}
}
if(m != null) return m;
return f;
}
public static void methods(Object target, State o){
Method[] l = target.getClass().getMethods();
for(Method f:l){
o.origin.outputln("Method: " + f.toString(), o.id);
}
o.origin.outputln("", o.id);
}
public static void fields(Object target, State o){
Field[] l = target.getClass().getFields();
if(l.length == 0){
o.origin.outputln("No fields found", o.id);
return;
}
for(Field f:l){
o.origin.outputln(f.getName(), o.id);
}
o.origin.outputln("", o.id);
}
public static void set_operator(Object actor, String field, int val, State state){ try{
// get actual type of attribute
Field theField = ReflectionMachine.getField(actor, field);
if (theField == null) {
state.origin.outputln("field or class " + field + " not found as field", state.id);
}
if(theField.getType() == int.class)
theField.setInt(actor, val);
if(theField.getType() == float.class)
theField.setFloat(actor, Float.intBitsToFloat(val));
}catch(Exception e){
state.origin.outputln("Something went wrong, see if the new value is an integer", state.id);
}}
public static void set_operator_object(Object actor, String field, int val, State state){ try{
// get actual type of attribute
Field theField = ReflectionMachine.getField(actor, field);
if (theField == null) {
state.origin.outputln("field or class " + field + " not found as field", state.id);
}
theField.set(actor, state.objects.get((-val)-1));
}catch(Exception e){
state.origin.outputln("Something went wrong, see if the new value is right type", state.id);
}}
public static void dot_operator(Object actor, String fieldOrClass, State state){ try{
// get actual type of attribute
Object attribute = ReflectionMachine.getByString(actor, fieldOrClass);
if (attribute == null) {
state.origin.outputln("field or class " + fieldOrClass + " not found as attribute", state.id);
}
// call a method and push return to stack
else if(attribute instanceof Method){
Method themethod = ((Method)attribute);
// get parameters
Object[] params = new Object[themethod.getParameterTypes().length];
for(int i=0; i<params.length;i++) {
Class theType = themethod.getParameterTypes()[i];
int stackElem = state.stack.pop();
if (theType == int.class || theType == Integer.class)
params[i] = stackElem;
else if (theType == float.class || theType == Float.class) {
params[i] = Float.intBitsToFloat(stackElem);
}else //is object
params[i] = state.objects.get(-stackElem-1);
}
// invoke
Object returnval = themethod.invoke(actor, params);
// manage return as object or integer
if(returnval == null){
}else if(returnval instanceof Integer){
state.stack.add((int)returnval);
} else {// is object
state.objects.add(returnval);
state.stack.add(-state.objects.size());
}
}
// get the value of field
else if(attribute instanceof Field) {
Field thefield = ((Field)attribute);
if (thefield.getType() == int.class || thefield.getType() == Integer.class) {
state.stack.add(thefield.getInt(actor));
}else if (thefield.getType() == float.class || thefield.getType() == Float.class) {
state.stack.add(Float.floatToIntBits(thefield.getFloat(actor)));
}
else {//is object
state.addObject(thefield.get(actor));
}
}
}catch(Exception e){
state.origin.outputln("Method parameters not found", state.id);
}}
public static void new_operator(String classname, State state) throws IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor[] constructors;
try {
constructors = Class.forName(classname).getConstructors();
} catch (ClassNotFoundException | NoClassDefFoundError e) {
state.origin.outputln(classname + " was not found. Fully qualified names required", state.id);
return;
}
if (constructors.length != 0) {
Class[] o = constructors[0].getParameterTypes();
state.origin.output(o.length == 0?
"Using no-arg constructor":"Using constructor with args: ", state.id);
for (Class i : o)
state.origin.output(i + " ", state.id);
state.origin.output("\n", state.id);
}else{
// no arg constructor
try {
state.addObject(Class.forName(classname).newInstance());
return;
} catch (ClassNotFoundException ignored){
// never happens, we checked earlier
}
}
Object[] params = new Object[constructors[0].getParameterTypes().length];
for(int i=0; i<params.length; i++){
params[i] = state.stack.pop();
}
try {
state.addObject(constructors[0].newInstance(params));
} catch (IllegalArgumentException e){
state.origin.outputln("Creation failed. Expected constructor args: ", state.id);
if (constructors.length != 0) {
Class[] o = constructors[0].getParameterTypes();
for (Class i : o)
state.origin.output(i + " ", state.id);
}
}
}
public static void classes(State state){
}
}