-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNullCheckAnalysis.java
More file actions
56 lines (50 loc) · 2.08 KB
/
NullCheckAnalysis.java
File metadata and controls
56 lines (50 loc) · 2.08 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
import soot.*;
import soot.jimple.*;
import soot.options.Options;
import soot.util.*;
import soot.jimple.internal.*;
import java.util.*;
import java.lang.Object;
public class NullCheckAnalysis {
public static void main(String[] args) {
String classpath = ".";
String className = "RedundantNullChecks";
String[] sootArgs={
"-pp",
"-f","c",
"-keep-line-number",
"-main-class", className,
className,
"-print-tags",
};
// Set up Soot
Options.v().set_soot_classpath(classpath + ":" + System.getProperty("java.home") + "/lib/rt.jar");
SootClass sc = Scene.v().loadClassAndSupport(className);
sc.setApplicationClass();
Scene.v().loadNecessaryClasses();
// Run analysis1
PackManager.v().getPack("jtp").add(new Transform("jtp.myTransform", new BodyTransformer() {
@Override
protected void internalTransform(Body body, String phase, Map<String, String> options) {
for (Unit u : body.getUnits()) {
// System.out.println(u);
if (u instanceof JIfStmt) {
// System.out.println('b');
JIfStmt ifStmt = (JIfStmt) u;
Value condition = ifStmt.getCondition();
if (condition instanceof EqExpr || condition instanceof NeExpr) {
Value op1 = ((BinopExpr) condition).getOp1();
Value op2 = ((BinopExpr) condition).getOp2();
if ((op1 instanceof NullConstant && !(op2 instanceof NullConstant))
|| (!(op1 instanceof NullConstant) && op2 instanceof NullConstant)) {
System.out.println("Redundant null check found at line: " + ifStmt.getJavaSourceStartLineNumber());
}
}
}
}
}
}));
// Run Soot
soot.Main.main(sootArgs);
}
}