-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
NewExprChanger.java
142 lines (128 loc) · 4.18 KB
/
NewExprChanger.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
package me.nallar.tickthreading.patcher;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.expr.Cast;
import javassist.expr.ConstructorCall;
import javassist.expr.ExprEditor;
import javassist.expr.FieldAccess;
import javassist.expr.Handler;
import javassist.expr.Instanceof;
import javassist.expr.MethodCall;
import javassist.expr.NewArray;
import javassist.expr.NewExpr;
/**
* Replaces new instances of one object with others
*/
public class NewExprChanger {
Map<String, List<String>> replacementClasses;
final Field positionField;
public NewExprChanger(Map replacementClasses) throws NoSuchFieldException {
positionField = NewExpr.class.getDeclaredField("newPos");
positionField.setAccessible(true);
this.replacementClasses = replacementClasses;
}
public void patchClass(CtClass ctClass) throws CannotCompileException {
for (CtMethod method : ctClass.getDeclaredMethods()) {
final Map<Integer, String> newExprType = new HashMap<Integer, String>();
method.instrument(new ExprEditor() {
NewExpr lastNewExpr;
@Override
public void edit(NewExpr e) throws CannotCompileException {
lastNewExpr = null;
if (replacementClasses.containsKey(e.getClassName())) {
System.out.println("(" + e.getSignature() + ") " + e.getClassName() + " at " + e.getFileName() + ":" + e.getLineNumber());
lastNewExpr = e;
}
}
@Override
public void edit(FieldAccess e) throws CannotCompileException {
NewExpr myLastNewExpr = lastNewExpr;
lastNewExpr = null;
if (myLastNewExpr != null) {
try {
newExprType.put((Integer) positionField.get(myLastNewExpr), signatureToName(e.getSignature()));
} catch (IllegalAccessException e1) {
e1.printStackTrace();
//This should never happen.
}
}
}
@Override
public void edit(MethodCall e) throws CannotCompileException {
lastNewExpr = null;
}
@Override
public void edit(NewArray e) throws CannotCompileException {
lastNewExpr = null;
}
@Override
public void edit(Cast e) throws CannotCompileException {
lastNewExpr = null;
}
@Override
public void edit(Instanceof e) throws CannotCompileException {
lastNewExpr = null;
}
@Override
public void edit(Handler e) throws CannotCompileException {
lastNewExpr = null;
}
@Override
public void edit(ConstructorCall e) throws CannotCompileException {
lastNewExpr = null;
}
});
method.instrument(new ExprEditor() {
@Override
public void edit(NewExpr e) throws CannotCompileException {
int newPos;
try {
newPos = (Integer) positionField.get(e);
} catch (IllegalAccessException e1) {
e1.printStackTrace();
//This should never happen
return;
}
try {
if (newExprType.containsKey(newPos)) {
String replacementType = null, assignedType = newExprType.get(newPos);
System.out.println(assignedType + " at " + e.getFileName() + ":" + e.getLineNumber());
Class<?> assignTo = Class.forName(assignedType);
for (String replacementClass : replacementClasses.get(e.getClassName())) {
if (assignTo.isAssignableFrom(Class.forName(replacementClass))) {
replacementType = replacementClass;
break;
}
}
if (replacementType == null) {
return;
}
String block = "{$_=new " + replacementType + "($$);}";
System.out.println("Replaced with " + block);
e.replace(block);
}
} catch (ClassNotFoundException el) {
el.printStackTrace();
}
}
});
}
try {
ctClass.writeFile();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String signatureToName(String signature) {
return signature.substring(1, signature.length() - 1).replace("/", ".");
}
}