From ab8e7e33640074c5f3661d19e60868660a6e2368 Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Sat, 15 Jul 2023 13:24:57 +0200 Subject: [PATCH 01/13] implement escape strategy for printing Jimple escaped like old soot; needs keywordlist! --- .../util/printer/AbstractStmtPrinter.java | 2 +- .../util/printer/LegacyJimplePrinter.java | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/AbstractStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/AbstractStmtPrinter.java index f4157a37bcc..70eb6d38a9f 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/AbstractStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/AbstractStmtPrinter.java @@ -128,7 +128,7 @@ public void typeSignature(@Nonnull Type type) { ((ArrayType) type).toString(this); } else { // primitive types: there should be no need to escape sth - output.append(type.toString()); + output.append(type); } } else { output.append(Jimple.escape(type.toString())); diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java index bd760e3c436..4e711058e9c 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java @@ -22,8 +22,14 @@ * #L% */ +import com.google.common.collect.ImmutableSet; +import java.util.Set; +import javax.annotation.Nonnull; import sootup.core.jimple.common.stmt.Stmt; import sootup.core.jimple.javabytecode.stmt.JSwitchStmt; +import sootup.core.types.ClassType; +import sootup.core.types.Type; +import sootup.core.util.StringTools; /** * StmtPrinter implementation for normal (full) Jimple for OldSoot @@ -35,10 +41,20 @@ */ public class LegacyJimplePrinter extends NormalStmtPrinter { + // FIXME: add keywords from (old) soot! + Set soot_jimple_keywords = ImmutableSet.of("class", "exception", "..."); + public LegacyJimplePrinter() { super(); } + String sootEscape(String str) { + if (str.length() == 0) { + return "\"\""; + } + return StringTools.getQuotedStringOf(str, soot_jimple_keywords.contains(str)); + } + @Override void enableImports(boolean enable) { if (enable) { @@ -47,6 +63,27 @@ void enableImports(boolean enable) { } } + @Override + public void typeSignature(@Nonnull Type type) { + handleIndent(); + if (type instanceof ClassType) { + ClassType ctype = (ClassType) type; + final String[] splits = ctype.getPackageName().getPackageName().split("\\."); + for (int i = 1; i < splits.length; i++) { + if (splits[i].length() == 0) { + continue; + } + output.append(sootEscape(splits[i])); + output.append("."); + } + output.append(sootEscape(ctype.getClassName())); + + } else { + // primitivetypes + output.append(type); + } + } + @Override public void stmt(Stmt currentStmt) { startStmt(currentStmt); From f21f56aa98786f54457c8fdfcfb7da6a55931afa Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Sat, 15 Jul 2023 13:25:56 +0200 Subject: [PATCH 02/13] unify jimple printing: concrete vs !concrete method --- .../main/java/sootup/core/util/printer/JimplePrinter.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java index d35256dae7c..86450bc0b47 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java @@ -238,17 +238,19 @@ private void printMethods(SootClass cl, LabeledStmtPrinter printer, PrintWrit while (methodIt.hasNext()) { SootMethod method = (SootMethod) methodIt.next(); + printer.handleIndent(); + method.toString(printer); + if (method.hasBody()) { Body body = method.getBody(); // print method's full signature information - method.toString(printer); printer.newline(); + incJimpleLnNum(); printBody(body, printer); } else { - printer.handleIndent(); - method.toString(printer); printer.literal(";"); + printer.newline(); incJimpleLnNum(); } From 341476ef5c5c271901db7f2e439bbb6379134610 Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Sat, 15 Jul 2023 13:39:56 +0200 Subject: [PATCH 03/13] added old soots keywords --- .../util/printer/LegacyJimplePrinter.java | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java index 4e711058e9c..66c47b137aa 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java @@ -41,8 +41,70 @@ */ public class LegacyJimplePrinter extends NormalStmtPrinter { - // FIXME: add keywords from (old) soot! - Set soot_jimple_keywords = ImmutableSet.of("class", "exception", "..."); + // source: + // https://github.com/soot-oss/soot/blob/1ad74494974165e8b5f2286c90f218a00eadc243/eclipse/ca.mcgill.sable.soot/src/ca/mcgill/sable/soot/editors/JimpleScanner.java + Set soot_jimple_keywords = + ImmutableSet.of( + "ignored", + "abstract", + "final", + "native", + "public", + "protected", + "private", + "static", + "synchronized", + "transient", + "volatile", + "class", + "interface", + "void", + "boolean", + "byte", + "short", + "char", + "int", + "long", + "float", + "double", + "null_type", + "unknown", + "extends", + "implements", + "breakpoint", + "case", + "catch", + "cmp", + "cmpg", + "cmpl", + "default", + "entermonitor", + "exitmonitor", + "goto", + "if", + "instanceof", + "interfaceinvoke", + "lengthof", + "lookupswitch", + "neg", + "new", + "newarray", + "newmultiarray", + "nop", + "ret", + "return", + "specialinvoke", + "staticinvoke", + "tableswitch", + "throw", + "throws", + "virtualinvoke", + "null", + "from", + "to", + "with", + "annotation", + "enum"); public LegacyJimplePrinter() { super(); From ea6e6fc5c66c0d57fb39aa873356fa9543ae51e9 Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Sat, 15 Jul 2023 13:45:44 +0200 Subject: [PATCH 04/13] fix off by one - refactoring bug --- .../java/sootup/core/util/printer/LegacyJimplePrinter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java index 66c47b137aa..48d4ee6f1d3 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java @@ -131,11 +131,11 @@ public void typeSignature(@Nonnull Type type) { if (type instanceof ClassType) { ClassType ctype = (ClassType) type; final String[] splits = ctype.getPackageName().getPackageName().split("\\."); - for (int i = 1; i < splits.length; i++) { - if (splits[i].length() == 0) { + for (String split : splits) { + if (split.length() == 0) { continue; } - output.append(sootEscape(splits[i])); + output.append(sootEscape(split)); output.append("."); } output.append(sootEscape(ctype.getClassName())); From ee00de7c09c2f707ddea55299b711da1efcd97ad Mon Sep 17 00:00:00 2001 From: Stefan Schott Date: Mon, 17 Jul 2023 17:05:36 +0200 Subject: [PATCH 05/13] legacy escaping test (should be escaped with single quotes) --- .../core/util/printer/JimplePrinter.java | 1 + .../core/printer/LegacyJimplePrinterTest.java | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java index 86450bc0b47..a8e13be95e0 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java @@ -325,6 +325,7 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { final Map labels = printer.getLabels(); for (Stmt currentStmt : linearizedStmtGraph) { + if (currentStmt == null) continue; previousStmt = currentStmt; // Print appropriate header. diff --git a/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java b/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java index 439b4b9a04b..80804837708 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java @@ -29,6 +29,10 @@ public class LegacyJimplePrinterTest { SootClass buildClass(Body.BodyBuilder builder) { + return buildClass(builder, "dummyMain", "main"); + } + + SootClass buildClass(Body.BodyBuilder builder, String className, String methodName) { Project project = JavaProject.builder(new JavaLanguage(8)).addInputLocation(new EagerInputLocation()).build(); @@ -36,7 +40,7 @@ SootClass buildClass(Body.BodyBuilder builder) { MethodSignature methodSignature = view.getIdentifierFactory() - .getMethodSignature("main", "dummyMain", "void", Collections.emptyList()); + .getMethodSignature(methodName, className, "void", Collections.emptyList()); Body body = builder .setMethodSignature(methodSignature) @@ -63,7 +67,7 @@ SootClass buildClass(Body.BodyBuilder builder) { null, null, null, - view.getIdentifierFactory().getClassType("dummyMain"), + view.getIdentifierFactory().getClassType(className), new EagerInputLocation()), SourceType.Application); } @@ -157,4 +161,17 @@ public void testValidOptions() { new JimplePrinter(JimplePrinter.Option.UseImports, JimplePrinter.Option.LegacyMode); p.printTo(buildClass(Body.builder()), new PrintWriter(new StringWriter())); } + + @Test + public void testLegacyEscaping() { + StringWriter out = new StringWriter(); + PrintWriter writer = new PrintWriter(out); + JimplePrinter printer = new JimplePrinter(JimplePrinter.Option.LegacyMode); + + SootClass clazz = buildClass(Body.builder(), "dummyMain", "from"); + printer.printTo(clazz, writer); + String jimple = out.toString(); + assertEquals("public class dummyMain\n{\n public static void \'from\'()\n {\n }\n}\n\r\n", jimple); + } + } From f5e7d53ca6a1eb4943f2209e2d8a53386adeda09 Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Mon, 17 Jul 2023 17:11:49 +0200 Subject: [PATCH 06/13] Update sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java --- .../main/java/sootup/core/util/printer/LegacyJimplePrinter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java index 48d4ee6f1d3..71d3b0193e5 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java @@ -112,7 +112,7 @@ public LegacyJimplePrinter() { String sootEscape(String str) { if (str.length() == 0) { - return "\"\""; + return "''"; } return StringTools.getQuotedStringOf(str, soot_jimple_keywords.contains(str)); } From 5b357552d61e66de87030efb5c937e14e70213b6 Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Mon, 17 Jul 2023 17:29:42 +0200 Subject: [PATCH 07/13] fix getquotedstringof to work like old soot --- .../java/sootup/core/util/StringTools.java | 26 ++++++++----------- .../util/printer/LegacyJimplePrinter.java | 6 ++--- .../bytecode/RuntimeJarConversionTests.java | 12 +++++++-- .../core/printer/LegacyJimplePrinterTest.java | 5 ++-- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/StringTools.java b/sootup.core/src/main/java/sootup/core/util/StringTools.java index 95742044b0e..2204888a6dc 100644 --- a/sootup.core/src/main/java/sootup/core/util/StringTools.java +++ b/sootup.core/src/main/java/sootup/core/util/StringTools.java @@ -82,54 +82,50 @@ public static String getEscapedStringOf(String fromString) { * Returns fromString, but with certain characters printed as if they were in a Java string * literal. Used by StringConstant.toString() */ - public static String getQuotedStringOf(String fromString, boolean needsQuotes) { + public static String getQuotedStringOf(String fromString, boolean neededQuotes) { // We definitely need fromString.length + 2, but let's have some // additional space StringBuilder builder = new StringBuilder(fromString.length() + 20); - builder.append("\""); + builder.append('"'); for (int i = 0; i < fromString.length(); i++) { char ch = fromString.charAt(i); if (ch == '\\') { builder.append("\\\\"); - needsQuotes = true; + neededQuotes = true; } else if (ch == '\'') { - builder.append("\\\'"); - needsQuotes = true; + builder.append("\\'"); + neededQuotes = true; } else if (ch == '\"') { builder.append("\\\""); - needsQuotes = true; + neededQuotes = true; } else if (ch == '\n') { builder.append("\\n"); - needsQuotes = true; + neededQuotes = true; } else if (ch == '\t') { builder.append("\\t"); - needsQuotes = true; + neededQuotes = true; } /* * 04.04.2006 mbatch added handling of \r, as compilers throw error if unicode */ else if (ch == '\r') { builder.append("\\r"); - needsQuotes = true; + neededQuotes = true; } /* * 10.04.2006 Nomait A Naeem added handling of \f, as compilers throw error if unicode */ else if (ch == '\f') { builder.append("\\f"); - needsQuotes = true; + neededQuotes = true; } else if (ch >= 32 && ch <= 126 /* is printable ascii */) { builder.append(ch); - // TODO: [ms] adapt this list to add quotes in cases where it is necessary - if (ch == ' ' || ch == ';' || ch == '/') { - needsQuotes = true; - } } else { builder.append(getUnicodeStringFromChar(ch)); } } - return needsQuotes + return neededQuotes ? builder.append('"').toString() : builder.subSequence(1, builder.length()).toString(); } diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java index 71d3b0193e5..0cf5e5531a8 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java @@ -112,7 +112,7 @@ public LegacyJimplePrinter() { String sootEscape(String str) { if (str.length() == 0) { - return "''"; + return "\"\""; } return StringTools.getQuotedStringOf(str, soot_jimple_keywords.contains(str)); } @@ -120,7 +120,7 @@ String sootEscape(String str) { @Override void enableImports(boolean enable) { if (enable) { - throw new RuntimeException( + throw new IllegalArgumentException( "Imports are not supported in Legacy Jimple: don't enable UseImports"); } } @@ -136,7 +136,7 @@ public void typeSignature(@Nonnull Type type) { continue; } output.append(sootEscape(split)); - output.append("."); + output.append('.'); } output.append(sootEscape(ctype.getClassName())); diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java index 12b486e0251..1e24ad0a4ec 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/RuntimeJarConversionTests.java @@ -3,6 +3,7 @@ import java.nio.file.Paths; import org.junit.Test; import sootup.core.inputlocation.AnalysisInputLocation; +import sootup.core.model.Body; import sootup.core.model.SootMethod; import sootup.core.signatures.MethodSignature; import sootup.java.bytecode.inputlocation.BytecodeClassLoadingOptions; @@ -15,7 +16,7 @@ public class RuntimeJarConversionTests { - private static void execute(String methodSignature1) { + private static Body execute(String methodSignature1) { AnalysisInputLocation inputLocation = new PathBasedAnalysisInputLocation( Paths.get(System.getProperty("java.home") + "/lib/rt.jar"), null); @@ -29,7 +30,7 @@ private static void execute(String methodSignature1) { project.createView(analysisInputLocation -> BytecodeClassLoadingOptions.Default); final SootMethod sootMethod = view.getMethod(methodSignature).get(); - sootMethod.getBody(); + return sootMethod.getBody(); } @Test @@ -73,4 +74,11 @@ public void testTrapsicwStubFactoryFactoryStaticImpl() { public void testFileDescriptorCloseAll() { execute(""); } + + @Test + public void testAbstractCollection_contains() { + // https://github.com/soot-oss/SootUp/issues/631 but for kotlin... + final Body body = execute(""); + System.out.println(body); + } } diff --git a/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java b/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java index 80804837708..b7db4556f57 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java @@ -171,7 +171,8 @@ public void testLegacyEscaping() { SootClass clazz = buildClass(Body.builder(), "dummyMain", "from"); printer.printTo(clazz, writer); String jimple = out.toString(); - assertEquals("public class dummyMain\n{\n public static void \'from\'()\n {\n }\n}\n\r\n", jimple); + assertEquals( + "public class dummyMain\n{\n public static void \"from\"()\n {\n }\n}\n\r\n", + jimple); } - } From 0bf24f3308b8699d3771d37e7058f4352101150f Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Mon, 17 Jul 2023 19:13:49 +0200 Subject: [PATCH 08/13] remove weird \r\n handling depending of current line.seperator system property - escape when it occurs - which will only depending on the os. --- .../main/java/sootup/core/util/StringTools.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/StringTools.java b/sootup.core/src/main/java/sootup/core/util/StringTools.java index 2204888a6dc..4cfb4e40fa4 100644 --- a/sootup.core/src/main/java/sootup/core/util/StringTools.java +++ b/sootup.core/src/main/java/sootup/core/util/StringTools.java @@ -30,20 +30,15 @@ public class StringTools { /** Returns fromString, but with non-isalpha() characters printed as '\\unnnn'. */ public static String getEscapedStringOf(String fromString) { - // TODO: [ms] possible performance+ maybe(!) work on .charAt(..) instead of .toCharArray)(..) char[] fromStringArray = fromString.toCharArray(); - // TODO: [ms] this makes the exported jimple platform dependent? improve! - char cr = lineSeparator.charAt(0); - char lf = lineSeparator.length() == 2 ? lineSeparator.charAt(1) : cr; - - // find if there is (find the first) a need to escape + // find the first char that has to be escaped int firstNonAlphaPos = -1; final int size = fromStringArray.length; for (int j = 0; j < size; j++) { char ch = fromStringArray[j]; final boolean isPrintableAscii = (ch >= 32 && ch <= 126); - if (!((isPrintableAscii || ch == cr || ch == lf) && ch != '\\')) { + if (!((isPrintableAscii || ch == '\r' || ch == '\n') && ch != '\\')) { firstNonAlphaPos = j; break; } @@ -65,7 +60,7 @@ public static String getEscapedStringOf(String fromString) { j < fromStringArrayLength; j++) { char ch = fromStringArray[j]; - if (((ch >= 32 && ch <= 126) || ch == cr || ch == lf) && ch != '\\') { + if (((ch >= 32 && ch <= 126) || ch == '\r' || ch == '\n') && ch != '\\') { sb.append(ch); } else { sb.append(getUnicodeStringFromChar(ch)); @@ -75,9 +70,6 @@ public static String getEscapedStringOf(String fromString) { return sb.toString(); } - /** Convenience field storing the system line separator. */ - public static final String lineSeparator = System.getProperty("line.separator"); - /** * Returns fromString, but with certain characters printed as if they were in a Java string * literal. Used by StringConstant.toString() From 76edbd6473607c83771abea880ce2caf523a2e8c Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Fri, 21 Jul 2023 09:25:44 +0200 Subject: [PATCH 09/13] set escape symbol to single quote; unwindowsing the test i.e. remove \r from \n --- .../src/main/java/sootup/core/util/StringTools.java | 4 ++-- .../sootup/core/util/printer/LegacyJimplePrinter.java | 5 +++-- .../sootup/java/core/printer/LegacyJimplePrinterTest.java | 8 +++++++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/StringTools.java b/sootup.core/src/main/java/sootup/core/util/StringTools.java index 4cfb4e40fa4..74306eaaa13 100644 --- a/sootup.core/src/main/java/sootup/core/util/StringTools.java +++ b/sootup.core/src/main/java/sootup/core/util/StringTools.java @@ -78,7 +78,7 @@ public static String getQuotedStringOf(String fromString, boolean neededQuotes) // We definitely need fromString.length + 2, but let's have some // additional space StringBuilder builder = new StringBuilder(fromString.length() + 20); - builder.append('"'); + builder.append('\''); for (int i = 0; i < fromString.length(); i++) { char ch = fromString.charAt(i); if (ch == '\\') { @@ -118,7 +118,7 @@ else if (ch == '\f') { } return neededQuotes - ? builder.append('"').toString() + ? builder.append('\'').toString() : builder.subSequence(1, builder.length()).toString(); } diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java index 0cf5e5531a8..68a5147280a 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LegacyJimplePrinter.java @@ -110,9 +110,10 @@ public LegacyJimplePrinter() { super(); } - String sootEscape(String str) { + @Nonnull + protected String sootEscape(String str) { if (str.length() == 0) { - return "\"\""; + return "''"; } return StringTools.getQuotedStringOf(str, soot_jimple_keywords.contains(str)); } diff --git a/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java b/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java index b7db4556f57..4fcbb721292 100644 --- a/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java +++ b/sootup.java.core/src/test/java/sootup/java/core/printer/LegacyJimplePrinterTest.java @@ -172,7 +172,13 @@ public void testLegacyEscaping() { printer.printTo(clazz, writer); String jimple = out.toString(); assertEquals( - "public class dummyMain\n{\n public static void \"from\"()\n {\n }\n}\n\r\n", + "public class dummyMain\n" + + "{\n" + + " public static void 'from'()\n" + + " {\n" + + " }\n" + + "}\n" + + "\n", jimple); } } From cd7349b142100b6f3a0b8849c0d8ab005f2cb579 Mon Sep 17 00:00:00 2001 From: "M.Schmidt" Date: Mon, 8 Jan 2024 09:48:04 +0100 Subject: [PATCH 10/13] make srcType final --- .../parser/JimpleAnalysisInputLocation.java | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/sootup.jimple.parser/src/main/java/sootup/jimple/parser/JimpleAnalysisInputLocation.java b/sootup.jimple.parser/src/main/java/sootup/jimple/parser/JimpleAnalysisInputLocation.java index cc961cb0ea4..698cd42b10e 100644 --- a/sootup.jimple.parser/src/main/java/sootup/jimple/parser/JimpleAnalysisInputLocation.java +++ b/sootup.jimple.parser/src/main/java/sootup/jimple/parser/JimpleAnalysisInputLocation.java @@ -31,19 +31,11 @@ public class JimpleAnalysisInputLocation Date: Mon, 8 Jan 2024 09:48:34 +0100 Subject: [PATCH 11/13] improve stringbuilder --- .../java/sootup/callgraph/GraphBasedCallGraph.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sootup.callgraph/src/main/java/sootup/callgraph/GraphBasedCallGraph.java b/sootup.callgraph/src/main/java/sootup/callgraph/GraphBasedCallGraph.java index d6672716bbe..822a6d00529 100644 --- a/sootup.callgraph/src/main/java/sootup/callgraph/GraphBasedCallGraph.java +++ b/sootup.callgraph/src/main/java/sootup/callgraph/GraphBasedCallGraph.java @@ -65,7 +65,7 @@ public GraphBasedCallGraph() { signatureToVertex = new HashMap<>(); } - public GraphBasedCallGraph( + protected GraphBasedCallGraph( @Nonnull DefaultDirectedGraph graph, @Nonnull Map signatureToVertex) { this.graph = graph; @@ -181,9 +181,13 @@ public String exportAsDot() { Vertex targetVertex = graph.getEdgeTarget(edge); dotFormatBuilder .append("\t") - .append("\"" + sourceVertex.methodSignature + "\"") + .append("\"") + .append(sourceVertex.methodSignature) + .append("\"") .append(" -> ") - .append("\"" + targetVertex.methodSignature + "\"") + .append("\"") + .append(targetVertex.methodSignature) + .append("\"") .append(";\n"); }); @@ -207,7 +211,8 @@ public MutableCallGraph copy() { @Nonnull protected Vertex vertexOf(@Nonnull MethodSignature method) { Vertex methodVertex = signatureToVertex.get(method); - Preconditions.checkNotNull(methodVertex, "Node for " + method + " has not been added yet"); + Preconditions.checkNotNull( + methodVertex, "Node for " + method + " does not exist in this CallGraph."); return methodVertex; } From c588372c72f591397a6e57e2b293483ed79f9bf3 Mon Sep 17 00:00:00 2001 From: "M.Schmidt" Date: Mon, 8 Jan 2024 09:48:59 +0100 Subject: [PATCH 12/13] add jimple to file util --- .../src/main/java/sootup/core/util/Utils.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sootup.core/src/main/java/sootup/core/util/Utils.java b/sootup.core/src/main/java/sootup/core/util/Utils.java index 0f02d4d6d2e..1662bfec6ea 100644 --- a/sootup.core/src/main/java/sootup/core/util/Utils.java +++ b/sootup.core/src/main/java/sootup/core/util/Utils.java @@ -23,6 +23,7 @@ */ import java.io.*; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -36,6 +37,8 @@ import javax.annotation.Nullable; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; + +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringEscapeUtils; import sootup.core.jimple.basic.EquivTo; import sootup.core.model.Body; @@ -46,6 +49,16 @@ /** @author Linghui Luo */ public class Utils { + void exportAsJimpleFile(@Nonnull SootClass c, @Nonnull String baseDir){ + try { + String printClass = c.print(); + File file = new File(baseDir + c.getName().replace('.', '/') + ".jimple"); + FileUtils.writeStringToFile(file, printClass, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + @Nullable Path compileJavaOTF(String className, String javaSourceContent) { File sourceFile; From 2d2a222b044a75c02b5ca42acaad07b9d928a221 Mon Sep 17 00:00:00 2001 From: "M.Schmidt" Date: Mon, 8 Jan 2024 11:57:38 +0100 Subject: [PATCH 13/13] cleanup modifierprint --- .../java/sootup/core/util/printer/JimplePrinter.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java index 618d06da60e..06755bb381d 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java @@ -134,10 +134,13 @@ public void printTo(SootClass cl, PrintWriter out, LabeledStmtPrinter printer */ EnumSet modifiers = EnumSet.copyOf(cl.getModifiers()); - // remove unwanted modifier combinations + // remove unwanted modifier combinations from print if (cl.isInterface() && ClassModifier.isAbstract(modifiers)) { modifiers.remove(ClassModifier.ABSTRACT); } + modifiers.remove(ClassModifier.SUPER); + modifiers.remove(ClassModifier.MODULE); + if (modifiers.size() != 0) { printer.modifier(ClassModifier.toString(modifiers)); printer.literal(" "); @@ -226,7 +229,7 @@ public void printTo(SootClass cl, PrintWriter out, LabeledStmtPrinter printer out.println(); } - out.println(printer.toString()); + out.println(printer); } private void printMethods(SootClass cl, LabeledStmtPrinter printer) { @@ -332,7 +335,6 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { final Map labels = printer.getLabels(); for (Stmt currentStmt : linearizedStmtGraph) { - if (currentStmt == null) continue; previousStmt = currentStmt; // Print appropriate header. @@ -346,7 +348,7 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { final boolean currentStmtHasLabel = labels.get(currentStmt) != null; if (previousStmt.branches() - || stmtGraph.predecessors(currentStmt).size() != 1 + || stmtGraph.predecessors(currentStmt).size() > 1 || previousStmt.getExpectedSuccessorCount() == 0 || currentStmtHasLabel) { printer.newline();