-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
NewExprChanger.java
117 lines (104 loc) · 3.52 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
package me.nallar.tickthreading.patcher;
import java.io.IOException;
import java.util.ArrayList;
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 {
public void fixUnsafeCollections(CtClass ctClass) throws CannotCompileException {
final Map<String, List<String>> replacementClasses = new HashMap<String, List<String>>();
for (CtMethod method : ctClass.getDeclaredMethods()) {
final List<String> newExprType = new ArrayList<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;
newExprType.add("");
}
}
@Override
public void edit(FieldAccess e) throws CannotCompileException {
NewExpr myLastNewExpr = lastNewExpr;
lastNewExpr = null;
if (myLastNewExpr != null) {
newExprType.add(newExprType.size() - 1, signatureToName(e.getSignature()));
}
}
@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 {
if (e.getClassName().equals("java.util.HashMap") && newExprType.size() > 0) {
String hashMapType, assignedType = newExprType.get(0);
newExprType.remove(0);
System.out.println(assignedType + " at " + e.getFileName() + ":" + e.getLineNumber());
if (assignedType.equals("java.util.Map")) {
hashMapType = "java.util.concurrent.ConcurrentHashMap";
} else if (assignedType.equals("java.util.HashMap")) {
hashMapType = "me.nallar.tickthreading.concurrentcollections.CHashMap";
} else {
return;
}
String block = "{$_=new " + hashMapType + "($$);}";
System.out.println("Replaced with " + block);
e.replace(block);
}
}
});
}
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("/", ".");
}
}