|
| 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 | +} |
0 commit comments