Skip to content

Commit f889c46

Browse files
author
Andreas Dann
committed
update printing
1 parent b7dbcdc commit f889c46

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package de.upb.soot.diff.printing;
2+
3+
import com.google.common.base.Strings;
4+
import soot.ArrayType;
5+
import soot.PrimType;
6+
import soot.RefType;
7+
import soot.Scene;
8+
import soot.SootClass;
9+
import soot.SootMethod;
10+
import soot.Type;
11+
import soot.VoidType;
12+
13+
import java.util.List;
14+
15+
public class PrinterUtils {
16+
public static String simplifyTypeName(Type type) {
17+
String typeName = type.toQuotedString();
18+
if (type instanceof RefType) {
19+
return simplifyClassName(((RefType) type).getSootClass());
20+
} else if (type instanceof ArrayType) {
21+
return simplifyTypeName(((ArrayType) type).baseType)
22+
+ Strings.repeat("[]", ((ArrayType) type).numDimensions);
23+
} else if (type instanceof PrimType) {
24+
return typeName;
25+
} else if (type instanceof VoidType) {
26+
return typeName;
27+
}
28+
29+
throw new IllegalStateException("Unhandled Soot type " + type);
30+
}
31+
32+
public static String simplifyClassName(SootClass cl) {
33+
if (Scene.v().isExcluded(cl)) {
34+
return (Scene.v().quotedNameOf(cl.getName()));
35+
} else {
36+
return (Scene.v().quotedNameOf(cl.getShortName()));
37+
}
38+
}
39+
40+
public static String getSignature(SootClass cl, String subSignature) {
41+
StringBuilder buffer = new StringBuilder();
42+
buffer.append("<");
43+
buffer.append(simplifyClassName(cl));
44+
buffer.append(": ");
45+
buffer.append(subSignature);
46+
buffer.append(">");
47+
return buffer.toString();
48+
}
49+
50+
public static String getSubSignature(String name, List<Type> params, Type returnType) {
51+
StringBuilder buffer = new StringBuilder();
52+
buffer.append(simplifyTypeName(returnType));
53+
buffer.append(" ");
54+
buffer.append(Scene.v().quotedNameOf(name));
55+
buffer.append("(");
56+
if (params != null) {
57+
for (int i = 0; i < params.size(); ++i) {
58+
buffer.append(simplifyTypeName((Type) params.get(i)));
59+
if (i < params.size() - 1) {
60+
buffer.append(",");
61+
}
62+
}
63+
}
64+
65+
buffer.append(")");
66+
return buffer.toString();
67+
}
68+
69+
public static String getMethodSignature(SootMethod method) {
70+
return getSubSignature(method.getName(), method.getParameterTypes(), method.getReturnType());
71+
}
72+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package de.upb.soot.diff.printing;
2+
3+
import soot.Body;
4+
import soot.NormalUnitPrinter;
5+
import soot.RefType;
6+
import soot.Scene;
7+
import soot.SootFieldRef;
8+
import soot.SootMethodRef;
9+
import soot.Type;
10+
import soot.jimple.IdentityRef;
11+
12+
/**
13+
* Omits the package name of classes that are not in the JDK. Useful for minhashing for example.
14+
*/
15+
public class SimpleUnitPrinter extends NormalUnitPrinter {
16+
public SimpleUnitPrinter(Body body) {
17+
super(body);
18+
}
19+
20+
public static String getSimpleMethodRef(SootMethodRef methodRef) {
21+
String subSignature =
22+
PrinterUtils.getSubSignature(
23+
methodRef.getName(), methodRef.getParameterTypes(), methodRef.getReturnType());
24+
return PrinterUtils.getSignature(methodRef.getDeclaringClass(), subSignature);
25+
}
26+
27+
public static String getSimpleFieldRef(SootFieldRef f) {
28+
return PrinterUtils.getSignature(f.declaringClass(), getSubSignature(f.name(), f.type()));
29+
}
30+
31+
public static String getSubSignature(String name, Type type) {
32+
StringBuilder buffer = new StringBuilder();
33+
buffer.append(PrinterUtils.simplifyTypeName(type) + " " + Scene.v().quotedNameOf(name));
34+
return buffer.toString();
35+
}
36+
37+
@Override
38+
public void type(Type t) {
39+
handleIndent();
40+
String s = t == null ? "<null>" : PrinterUtils.simplifyTypeName(t);
41+
output.append(s);
42+
}
43+
44+
@Override
45+
public void methodRef(SootMethodRef m) {
46+
handleIndent();
47+
output.append(getSimpleMethodRef(m));
48+
}
49+
50+
@Override
51+
public void fieldRef(SootFieldRef f) {
52+
handleIndent();
53+
output.append(getSimpleFieldRef(f));
54+
}
55+
56+
@Override
57+
public void identityRef(IdentityRef r) {
58+
Type type = r.getType();
59+
60+
if (type instanceof RefType) {
61+
handleIndent();
62+
output.append(PrinterUtils.simplifyClassName(((RefType) type).getSootClass()));
63+
} else {
64+
super.identityRef(r);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)