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
|
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.IdentityHashMap;
public class Invocations {
@Override
public String toString() {
return b.toString();
}
public <T> T trace(final Object target, final Class<T> type) {
return trace(target, type, false);
}
private void declare(final Object object, final Class<?> type, final boolean force) {
if (!void.class.equals(type)) {
if (force || (object != null && !variables.containsKey(object) && !isPrimitive(type))) {
b.append(typeName(type));
b.append(" ");
b.append(register(object));
b.append(";\n");
}
}
}
private boolean isPrimitive(final Class<?> type) {
boolean isPrimitive = type.isPrimitive();
if (!isPrimitive) {
for (final Class<?> c : PRIMITIVES) {
if (c.equals(type)) {
isPrimitive = true;
break;
}
}
}
return isPrimitive;
}
private String name(final Object object) {
String name = "null";
if (object != null) {
name = variables.get(object);
if (name == null) {
if (isPrimitive(object.getClass())) {
return render(object);
} else {
throw new RuntimeException("Could not find name for: " + object);
}
}
}
return name;
}
private String register(final Object object) {
String name;
name = variables.get(object);
if (name == null) {
name = "v" + (++var);
variables.put(object, name);
}
return name;
}
private String render(final Object object) {
final Class<?> type = object.getClass();
if (String.class.equals(type)) {
return "\"" + object.toString() + "\"";
} else if (short.class.equals(type) || Short.class.equals(type)) {
return "(short)" + object.toString();
} else if (long.class.equals(type) || Long.class.equals(type)) {
return object.toString() + "L";
} else if (char.class.equals(type) || Character.class.equals(type)) {
return "'" + object.toString() + "'";
} else {
return object.toString();
}
}
@SuppressWarnings("unchecked")
private <T> T trace(final Object target, final Class<T> type, final boolean force) {
T result = null;
if (target != null) {
if (target.getClass().getInterfaces().length > 0 && !isPrimitive(target.getClass())) {
result =
type.cast(
Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
target.getClass().getInterfaces(),
new Recorder(target)));
} else {
result = (T) target;
}
}
declare(result, type, force);
return result;
}
private String typeName(final Class<?> type) {
if (type.isArray()) {
return typeName(type.getComponentType()) + "[]";
} else {
return type.getName();
}
}
private class Recorder implements InvocationHandler {
Recorder(final Object target) {
this.target = target;
}
public Object invoke(final Object proxy, final Method method, final Object[] args)
throws Throwable {
Object result = null;
final Class<?> type = method.getReturnType();
try {
result = method.invoke(target, args);
} catch (final InvocationTargetException e) {
final Throwable t = e.getCause();
for (final Class<?> ex : method.getExceptionTypes()) {
if (ex.isAssignableFrom(t.getClass())) {
throw t;
}
}
throw e;
}
result = trace(result, type, true);
record(result, proxy, method, args);
return result;
}
private void record(
final Object result, final Object proxy, final Method method, final Object[] args) {
if (args != null) {
final Class<?>[] types = method.getParameterTypes();
for (int i = 0; i < args.length; ++i) {
declare(args[i], types[i], false);
}
}
if (result != null) {
b.append(name(result));
b.append(" = ");
}
b.append(name(proxy));
b.append('.');
b.append(method.getName());
b.append('(');
if (args != null) {
for (int i = 0; i < args.length; ++i) {
b.append(name(args[i]));
if (i < args.length - 1) {
b.append(',');
}
}
}
b.append(");\n");
}
private final Object target;
}
;
private final StringBuilder b = new StringBuilder();
private int var = 0;
private final IdentityHashMap<Object, String> variables = new IdentityHashMap<Object, String>();
private static final Class<?>[] PRIMITIVES = {
String.class,
Integer.class,
Boolean.class,
Double.class,
Long.class,
Short.class,
Byte.class,
Character.class,
Float.class
};
}
|