From 518515c8d7fc7b4e9aab79c1215ed3eef0ff5f9b Mon Sep 17 00:00:00 2001 From: emmanue1 Date: Mon, 29 Jul 2019 10:28:23 +0200 Subject: [PATCH] In unit tests, check that the decompiled source code can be recompiled. --- .../jd/core/v1/ClassFileToJavaSourceTest.java | 1705 +++++++++-------- .../org/jd/core/v1/ControlFlowGraphTest.java | 4 +- .../jd/core/v1/JavaFragmentToTokenTest.java | 4 +- .../core/v1/LayoutFragmentProcessorTest.java | 4 +- .../ControlFlowGraphPlantUMLWriter.java | 5 +- .../org/jd/core/v1/compiler/CompilerUtil.java | 44 + .../v1/compiler/JavaSourceFileObject.java | 34 + .../jd/core/v1/printer/PlainTextPrinter.java | 12 +- .../core/v1/{util => regex}/PatternMaker.java | 4 +- 9 files changed, 1034 insertions(+), 782 deletions(-) rename src/test/java/org/jd/core/v1/{util => cfg}/ControlFlowGraphPlantUMLWriter.java (99%) create mode 100644 src/test/java/org/jd/core/v1/compiler/CompilerUtil.java create mode 100644 src/test/java/org/jd/core/v1/compiler/JavaSourceFileObject.java rename src/test/java/org/jd/core/v1/{util => regex}/PatternMaker.java (94%) diff --git a/src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java b/src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java index eb2b5a48..319b0f2e 100644 --- a/src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java +++ b/src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2019 Emmanuel Dupuy. + * Copyright (c) 2008, 2019 Emmanuel Dupuy. * This project is distributed under the GPLv3 license. * This is a Copyleft license that gives the user the right to use, * copy and modify the code freely for non-commercial purposes. @@ -9,6 +9,8 @@ import junit.framework.TestCase; import org.jd.core.v1.api.loader.Loader; +import org.jd.core.v1.compiler.CompilerUtil; +import org.jd.core.v1.compiler.JavaSourceFileObject; import org.jd.core.v1.loader.ZipLoader; import org.jd.core.v1.model.message.Message; import org.jd.core.v1.printer.PlainTextPrinter; @@ -18,7 +20,7 @@ import org.jd.core.v1.service.layouter.LayoutFragmentProcessor; import org.jd.core.v1.service.tokenizer.javafragmenttotoken.JavaFragmentToTokenProcessor; import org.jd.core.v1.service.writer.WriteTokenProcessor; -import org.jd.core.v1.util.PatternMaker; +import org.jd.core.v1.regex.PatternMaker; import org.junit.Test; import java.io.InputStream; @@ -26,6 +28,7 @@ import java.util.HashMap; import java.util.Map; + public class ClassFileToJavaSourceTest extends TestCase { protected DeserializeClassFileProcessor deserializer = new DeserializeClassFileProcessor(); protected ClassFileToJavaSyntaxProcessor converter = new ClassFileToJavaSyntaxProcessor(); @@ -37,6 +40,7 @@ public class ClassFileToJavaSourceTest extends TestCase { @Test public void testJdk170Basic() throws Exception { + String internalClassName = "org/jd/core/test/Basic"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -44,7 +48,7 @@ public void testJdk170Basic() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Basic"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -60,75 +64,77 @@ public void testJdk170Basic() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("serialVersionUID = 9506606333927794L;") != -1); assertTrue(source.indexOf(".indexOf('B');") != -1); - assertTrue(source.matches(PatternMaker.make("[ 26: 26]", "System.out.println(\"hello\");"))); + assertTrue(source.matches(PatternMaker.make("/* 26: 26 */", "System.out.println(\"hello\");"))); assertTrue(source.indexOf("String str1 = \"3 == \" + (i + 1) + \" ?\";") != -1); - assertTrue(source.indexOf("String str2 = str1.valueOf(\"abc \\b \\f \\n \\r \\t \\\" \\007 def\");") != -1); + assertTrue(source.indexOf("String str2 = String.valueOf(\"abc \\b \\f \\n \\r \\t \\\" \\007 def\");") != -1); assertTrue(source.indexOf("char c2 = '€';") != -1); assertTrue(source.indexOf("char c3 = '\\'';") != -1); assertTrue(source.indexOf("char c4 = c3 = c2 = c1 = Character.toUpperCase('x');") != -1); - assertTrue(source.indexOf("Class class3 = String.class, class2 = class3, class1 = class2;") != -1); - assertTrue(source.matches(PatternMaker.make("Class class5 = doSomething(class6 = String.class, args1 = args2 = new String[], class4 = class5;"))); + assertTrue(source.indexOf("Class class3 = String.class, class2 = class3, class1 = class2;") != -1); + assertTrue(source.matches(PatternMaker.make("Class class5 = doSomething(class6 = String.class, args1 = args2 = new String[], class4 = class5;"))); assertTrue(source.matches(PatternMaker.make("int j = 1, k[] = {1, l[][] = {"))); assertTrue(source.matches(PatternMaker.make("String stringNull = null;"))); - assertTrue(source.matches(PatternMaker.make(": 58]", "return new String[] {s, s + '?'};"))); + assertTrue(source.matches(PatternMaker.make(": 58 */", "return new String[] {s, s + '?'};"))); assertTrue(source.indexOf("if (this instanceof Object)") != -1); assertTrue(source.indexOf("int k = 50 / (25 + (i = 789));") != -1); - assertTrue(source.matches(PatternMaker.make(": 80]", "k = i += 100;"))); - assertTrue(source.matches(PatternMaker.make(": 85]", "i = ++this.int78;"))); - assertTrue(source.matches(PatternMaker.make(": 86]", "i = this.int78++;"))); - assertTrue(source.matches(PatternMaker.make(": 87]", "i *= 10;"))); - assertTrue(source.matches(PatternMaker.make(": 89]", "this.int78 = ++i;"))); - assertTrue(source.matches(PatternMaker.make(": 90]", "this.int78 = i++;"))); - assertTrue(source.matches(PatternMaker.make(": 91]", "this.int78 *= 10;"))); - assertTrue(source.matches(PatternMaker.make(": 93]", "long34 = ++long12;"))); - assertTrue(source.matches(PatternMaker.make(": 94]", "long34 = long12++;"))); - assertTrue(source.matches(PatternMaker.make(": 95]", "long34 *= 10L;"))); - assertTrue(source.matches(PatternMaker.make(": 97]", "i = (int)long12 + this.int78;"))); - assertTrue(source.matches(PatternMaker.make(": 99]", "i = k ^ 0xFF;"))); - assertTrue(source.matches(PatternMaker.make(": 100]", "i |= 0x7;"))); + assertTrue(source.matches(PatternMaker.make(": 80 */", "k = i += 100;"))); + assertTrue(source.matches(PatternMaker.make(": 85 */", "i = ++this.int78;"))); + assertTrue(source.matches(PatternMaker.make(": 86 */", "i = this.int78++;"))); + assertTrue(source.matches(PatternMaker.make(": 87 */", "i *= 10;"))); + assertTrue(source.matches(PatternMaker.make(": 89 */", "this.int78 = ++i;"))); + assertTrue(source.matches(PatternMaker.make(": 90 */", "this.int78 = i++;"))); + assertTrue(source.matches(PatternMaker.make(": 91 */", "this.int78 *= 10;"))); + assertTrue(source.matches(PatternMaker.make(": 93 */", "long34 = ++long12;"))); + assertTrue(source.matches(PatternMaker.make(": 94 */", "long34 = long12++;"))); + assertTrue(source.matches(PatternMaker.make(": 95 */", "long34 *= 10L;"))); + assertTrue(source.matches(PatternMaker.make(": 97 */", "i = (int)long12 + this.int78;"))); + assertTrue(source.matches(PatternMaker.make(": 99 */", "i = k ^ 0xFF;"))); + assertTrue(source.matches(PatternMaker.make(": 100 */", "i |= 0x7;"))); assertTrue(source.indexOf("int result;") != -1); - assertTrue(source.matches(PatternMaker.make(": 112]", "result = 1;"))); - assertTrue(source.matches(PatternMaker.make(": 114]", "int k = i;"))); - assertTrue(source.matches(PatternMaker.make(": 115]", "result = k + 2;"))); - assertTrue(source.matches(PatternMaker.make(": 118]", "result = this.short56;"))); - assertTrue(source.matches(PatternMaker.make(": 122]", "return result;"))); - assertTrue(source.matches(PatternMaker.make(": 126]", "int int78 = getInt78(new Object[] { this }, (short)5);"))); - assertTrue(source.matches(PatternMaker.make(": 128]", "i = (int)(Basic.long12 + long12) + this.int78 + int78;"))); + assertTrue(source.matches(PatternMaker.make(": 112 */", "result = 1;"))); + assertTrue(source.matches(PatternMaker.make(": 114 */", "int k = i;"))); + assertTrue(source.matches(PatternMaker.make(": 115 */", "result = k + 2;"))); + assertTrue(source.matches(PatternMaker.make(": 118 */", "result = this.short56;"))); + assertTrue(source.matches(PatternMaker.make(": 122 */", "return result;"))); + assertTrue(source.matches(PatternMaker.make(": 126 */", "int int78 = getInt78(new Object[] { this }, (short)5);"))); + assertTrue(source.matches(PatternMaker.make(": 128 */", "i = (int)(Basic.long12 + long12) + this.int78 + int78;"))); assertTrue(source.indexOf("public static native int read();") != -1); - assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;"))); - assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;"))); + assertTrue(source.matches(PatternMaker.make("/* 171: 171 */", "return str + str;"))); + assertTrue(source.matches(PatternMaker.make("/* 174: 174 */", "return str;"))); - assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;"))); + assertTrue(source.matches(PatternMaker.make("/* 183: 183 */", "return ((Basic)objects[index]).int78;"))); - assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);"))); + assertTrue(source.matches(PatternMaker.make("/* 186: 186 */", "protected static final Integer INTEGER_255 = new Integer(255);"))); assertTrue(source.indexOf("()") == -1); - assertTrue(source.indexOf("null = ") == -1); assertTrue(source.indexOf("NaND") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170NoDebugBasic() throws Exception { + String internalClassName = "org/jd/core/test/Basic"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0-no-debug-info.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Basic"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -143,16 +149,17 @@ public void testJdk170NoDebugBasic() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.matches(PatternMaker.make("System.out.println(\"hello\");"))); assertTrue(source.matches(PatternMaker.make("String str1 = \"3 == \" + (paramInt + 1) + \" ?\";"))); - assertTrue(source.indexOf("String str2 = str1.valueOf(\"abc \\b \\f \\n \\r \\t \\\" \\007 def\");") != -1); + assertTrue(source.indexOf("String str2 = String.valueOf(\"abc \\b \\f \\n \\r \\t \\\" \\007 def\");") != -1); assertTrue(source.matches(PatternMaker.make("char c2 = '€';"))); assertTrue(source.matches(PatternMaker.make("char c4 = c3 = c2 = c1 = Character.toUpperCase('x');"))); - assertTrue(source.matches(PatternMaker.make("Class clazz3 = String.class;"))); - assertTrue(source.matches(PatternMaker.make("Class clazz2 = clazz3;"))); - assertTrue(source.matches(PatternMaker.make("Class clazz1 = clazz2;"))); - assertTrue(source.indexOf("Class clazz5 = doSomething(clazz6 = String.class, arrayOfString1 = arrayOfString2 = new String[]") != -1); + assertTrue(source.matches(PatternMaker.make("Class clazz3 = String.class;"))); + assertTrue(source.matches(PatternMaker.make("Class clazz2 = clazz3;"))); + assertTrue(source.matches(PatternMaker.make("Class clazz1 = clazz2;"))); + assertTrue(source.indexOf("Class clazz5 = doSomething(clazz6 = String.class, arrayOfString1 = arrayOfString2 = new String[]") != -1); assertTrue(source.matches(PatternMaker.make("if (this instanceof Object)"))); @@ -161,22 +168,23 @@ public void testJdk170NoDebugBasic() throws Exception { assertTrue(source.indexOf("protected static final Integer INTEGER_255 = new Integer(255);") != -1); assertTrue(source.indexOf("()") == -1); - assertTrue(source.indexOf("null = ") == -1); assertTrue(source.indexOf("NaND") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170Constructors() throws Exception { + String internalClassName = "org/jd/core/test/Constructors"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Constructors"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -191,22 +199,25 @@ public void testJdk170Constructors() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 28]", "this.short123 = 1;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 28 */", "this.short123 = 1;"))); - assertTrue(source.matches(PatternMaker.make(": 32]", "super(short56);"))); - assertTrue(source.matches(PatternMaker.make(": 33]", "this.short123 = 2;"))); + assertTrue(source.matches(PatternMaker.make(": 32 */", "super(short56);"))); + assertTrue(source.matches(PatternMaker.make(": 33 */", "this.short123 = 2;"))); - assertTrue(source.matches(PatternMaker.make(": 37]", "this(short56);"))); - assertTrue(source.matches(PatternMaker.make(": 38]", "this.int78 = int78;"))); - assertTrue(source.matches(PatternMaker.make(": 39]", "this.short123 = 3;"))); + assertTrue(source.matches(PatternMaker.make(": 37 */", "this(short56);"))); + assertTrue(source.matches(PatternMaker.make(": 38 */", "this.int78 = int78;"))); + assertTrue(source.matches(PatternMaker.make(": 39 */", "this.short123 = 3;"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170IfElse() throws Exception { + String internalClassName = "org/jd/core/test/IfElse"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -214,7 +225,7 @@ public void testJdk170IfElse() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/IfElse"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -230,48 +241,51 @@ public void testJdk170IfElse() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 12: 12]", "if (this == null)"))); - assertTrue(source.matches(PatternMaker.make("[ 22: 22]", "if (\"abc\".isEmpty() && \"abc\".isEmpty())"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 12: 12 */", "if (this == null)"))); + assertTrue(source.matches(PatternMaker.make("/* 22: 22 */", "if (\"abc\".isEmpty() && \"abc\".isEmpty())"))); - assertTrue(source.matches(PatternMaker.make("[ 32: 32]", "if (this == null)"))); - assertTrue(source.matches(PatternMaker.make("[ 34: 0]", "} else {"))); + assertTrue(source.matches(PatternMaker.make("/* 32: 32 */", "if (this == null)"))); + assertTrue(source.matches(PatternMaker.make("/* 34: 0 */", "} else {"))); - assertTrue(source.matches(PatternMaker.make("[ 44: 44]", "if (this == null)"))); - assertTrue(source.matches(PatternMaker.make("[ 46: 46]", "} else if (this == null) {"))); - assertTrue(source.matches(PatternMaker.make("[ 48: 0]", "} else {"))); + assertTrue(source.matches(PatternMaker.make("/* 44: 44 */", "if (this == null)"))); + assertTrue(source.matches(PatternMaker.make("/* 46: 46 */", "} else if (this == null) {"))); + assertTrue(source.matches(PatternMaker.make("/* 48: 0 */", "} else {"))); - assertTrue(source.matches(PatternMaker.make("[ 58: 58]", "if (i == 0)"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 60]", "if (i == 1)"))); + assertTrue(source.matches(PatternMaker.make("/* 58: 58 */", "if (i == 0)"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 60 */", "if (i == 1)"))); - assertTrue(source.matches(PatternMaker.make("[ 71: 71]", "if (i == System.currentTimeMillis())"))); - assertTrue(source.matches(PatternMaker.make("[ 73: 73]", "} else if (i != System.currentTimeMillis()) {"))); - assertTrue(source.matches(PatternMaker.make("[ 75: 75]", "} else if (i > System.currentTimeMillis()) {"))); + assertTrue(source.matches(PatternMaker.make("/* 71: 71 */", "if (i == System.currentTimeMillis())"))); + assertTrue(source.matches(PatternMaker.make("/* 73: 73 */", "} else if (i != System.currentTimeMillis()) {"))); + assertTrue(source.matches(PatternMaker.make("/* 75: 75 */", "} else if (i > System.currentTimeMillis()) {"))); - assertTrue(source.matches(PatternMaker.make("[ 123: 123]", "if (i == 4 && i == 5 && i == 6)"))); + assertTrue(source.matches(PatternMaker.make("/* 123: 123 */", "if (i == 4 && i == 5 && i == 6)"))); - assertTrue(source.matches(PatternMaker.make("[ 135: 135]", "if (i == 3 || i == 5 || i == 6)"))); - assertTrue(source.matches(PatternMaker.make("[ 137: 137]", "} else if (i != 4 && i > 7 && i > 8) {"))); - assertTrue(source.matches(PatternMaker.make("[ 139: 0]", "} else {"))); + assertTrue(source.matches(PatternMaker.make("/* 135: 135 */", "if (i == 3 || i == 5 || i == 6)"))); + assertTrue(source.matches(PatternMaker.make("/* 137: 137 */", "} else if (i != 4 && i > 7 && i > 8) {"))); + assertTrue(source.matches(PatternMaker.make("/* 139: 0 */", "} else {"))); - assertTrue(source.matches(PatternMaker.make("[ 148: 148]", "if ((i == 1 && i == 2 && i == 3) || (i == 4 && i == 5 && i == 6) || (i == 7 && i == 8 && i == 9))"))); - assertTrue(source.matches(PatternMaker.make("[ 160: 160]", "if ((i == 1 || i == 2 || i == 3) && (i == 4 || i == 5 || i == 6) && (i == 7 || i == 8 || i == 9))"))); + assertTrue(source.matches(PatternMaker.make("/* 148: 148 */", "if ((i == 1 && i == 2 && i == 3) || (i == 4 && i == 5 && i == 6) || (i == 7 && i == 8 && i == 9))"))); + assertTrue(source.matches(PatternMaker.make("/* 160: 160 */", "if ((i == 1 || i == 2 || i == 3) && (i == 4 || i == 5 || i == 6) && (i == 7 || i == 8 || i == 9))"))); - assertTrue(source.matches(PatternMaker.make("[ 172: 172]", "if ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4))"))); - assertTrue(source.matches(PatternMaker.make("[ 184: 184]", "if ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4))"))); + assertTrue(source.matches(PatternMaker.make("/* 172: 172 */", "if ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4))"))); + assertTrue(source.matches(PatternMaker.make("/* 184: 184 */", "if ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4))"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170Interface() throws Exception { + String internalClassName = "org/jd/core/test/Interface"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Interface"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -286,15 +300,18 @@ public void testJdk170Interface() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.matches(PatternMaker.make("public interface Interface", "extends Serializable"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170While() throws Exception { + String internalClassName = "org/jd/core/test/While"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -302,7 +319,7 @@ public void testJdk170While() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/While"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -318,28 +335,31 @@ public void testJdk170While() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 15]", "while (i-- > 0)"))); - assertTrue(source.matches(PatternMaker.make(": 23]", "while (i < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 42]", "while (i0 > 20)"))); - assertTrue(source.matches(PatternMaker.make("[ 113: 0]", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 128: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 158: 0]", "while (true)"))); - assertTrue(source.matches(PatternMaker.make(": 232]", "while (++i < 10)"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 15 */", "while (i-- > 0)"))); + assertTrue(source.matches(PatternMaker.make(": 23 */", "while (i < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 42 */", "while (i0 > 20)"))); + assertTrue(source.matches(PatternMaker.make("/* 113: 0 */", "continue;"))); + assertTrue(source.matches(PatternMaker.make("/* 128: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 158: 0 */", "while (true)"))); + assertTrue(source.matches(PatternMaker.make(": 232 */", "while (++i < 10)"))); assertTrue(source.indexOf("while (i == 4 && i == 5 && i == 6)") != -1); assertTrue(source.indexOf("while ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4))") != -1); assertTrue(source.indexOf("while ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4))") != -1); - assertFalse(source.matches(PatternMaker.make("[ 348: 0]", "default:"))); - assertFalse(source.matches(PatternMaker.make("[ 350: 348]", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 404: 404]", "System.out.println(\"a\");"))); - assertTrue(source.matches(PatternMaker.make("[ 431: 431]", "System.out.println(\"a\");"))); + assertFalse(source.matches(PatternMaker.make("[ 348: 0 */", "default:"))); + assertFalse(source.matches(PatternMaker.make("[ 350: 348 */", "continue;"))); + assertTrue(source.matches(PatternMaker.make("/* 404: 404 */", "System.out.println(\"a\");"))); + assertTrue(source.matches(PatternMaker.make("/* 431: 431 */", "System.out.println(\"a\");"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk901While() throws Exception { + String internalClassName = "org/jd/core/test/While"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-9.0.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -347,7 +367,7 @@ public void testJdk901While() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/While"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -363,28 +383,31 @@ public void testJdk901While() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 15]", "while (i-- > 0)"))); - assertTrue(source.matches(PatternMaker.make(": 23]", "while (i < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 42]", "while (i0 > 20)"))); - assertTrue(source.matches(PatternMaker.make("[ 113: 0]", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 128: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 158: 0]", "while (true)"))); - assertTrue(source.matches(PatternMaker.make(": 232]", "while (++i < 10)"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 15 */", "while (i-- > 0)"))); + assertTrue(source.matches(PatternMaker.make(": 23 */", "while (i < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 42 */", "while (i0 > 20)"))); + assertTrue(source.matches(PatternMaker.make("/* 113: 0 */", "continue;"))); + assertTrue(source.matches(PatternMaker.make("/* 128: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 158: 0 */", "while (true)"))); + assertTrue(source.matches(PatternMaker.make(": 232 */", "while (++i < 10)"))); assertTrue(source.indexOf("while (i == 4 && i == 5 && i == 6)") != -1); assertTrue(source.indexOf("while ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4))") != -1); assertTrue(source.indexOf("while ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4))") != -1); - assertFalse(source.matches(PatternMaker.make("[ 348: 0]", "default:"))); - assertFalse(source.matches(PatternMaker.make("[ 350: 348]", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 404: 404]", "System.out.println(\"a\");"))); - assertTrue(source.matches(PatternMaker.make("[ 431: 431]", "System.out.println(\"a\");"))); + assertFalse(source.matches(PatternMaker.make("[ 348: 0 */", "default:"))); + assertFalse(source.matches(PatternMaker.make("[ 350: 348 */", "continue;"))); + assertTrue(source.matches(PatternMaker.make("/* 404: 404 */", "System.out.println(\"a\");"))); + assertTrue(source.matches(PatternMaker.make("/* 431: 431 */", "System.out.println(\"a\");"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk1002While() throws Exception { + String internalClassName = "org/jd/core/test/While"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-10.0.2.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -392,7 +415,7 @@ public void testJdk1002While() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/While"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -408,35 +431,38 @@ public void testJdk1002While() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 15]", "while (i-- > 0)"))); - assertTrue(source.matches(PatternMaker.make(": 23]", "while (i < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 42]", "while (i0 > 20)"))); - assertTrue(source.matches(PatternMaker.make("[ 113: 0]", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 128: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 158: 0]", "while (true)"))); - assertTrue(source.matches(PatternMaker.make(": 232]", "while (++i < 10)"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 15 */", "while (i-- > 0)"))); + assertTrue(source.matches(PatternMaker.make(": 23 */", "while (i < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 42 */", "while (i0 > 20)"))); + assertTrue(source.matches(PatternMaker.make("/* 113: 0 */", "continue;"))); + assertTrue(source.matches(PatternMaker.make("/* 128: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 158: 0 */", "while (true)"))); + assertTrue(source.matches(PatternMaker.make(": 232 */", "while (++i < 10)"))); assertTrue(source.indexOf("while (i == 4 && i == 5 && i == 6)") != -1); assertTrue(source.indexOf("while ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4))") != -1); assertTrue(source.indexOf("while ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4))") != -1); - assertFalse(source.matches(PatternMaker.make("[ 348: 0]", "default:"))); - assertFalse(source.matches(PatternMaker.make("[ 350: 348]", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 404: 404]", "System.out.println(\"a\");"))); - assertTrue(source.matches(PatternMaker.make("[ 431: 431]", "System.out.println(\"a\");"))); + assertFalse(source.matches(PatternMaker.make("[ 348: 0 */", "default:"))); + assertFalse(source.matches(PatternMaker.make("[ 350: 348 */", "continue;"))); + assertTrue(source.matches(PatternMaker.make("/* 404: 404 */", "System.out.println(\"a\");"))); + assertTrue(source.matches(PatternMaker.make("/* 431: 431 */", "System.out.println(\"a\");"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170DoWhile() throws Exception { + String internalClassName = "org/jd/core/test/DoWhile"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/DoWhile"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -451,32 +477,35 @@ public void testJdk170DoWhile() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 24]", "} while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 32]", "} while (this == null);"))); - assertTrue(source.matches(PatternMaker.make(": 44]", "++i;"))); - assertTrue(source.matches(PatternMaker.make(": 46]", "while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 72]", "while (i0 < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 77]", "i1--;"))); - assertTrue(source.matches(PatternMaker.make(": 79]", "while (i1 > 0);"))); - assertTrue(source.matches(PatternMaker.make(": 98]", "while (--i > 0.0F);"))); - assertTrue(source.matches(PatternMaker.make(": 108]", "while (i-- > 0.0F);"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 24 */", "} while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 32 */", "} while (this == null);"))); + assertTrue(source.matches(PatternMaker.make(": 44 */", "++i;"))); + assertTrue(source.matches(PatternMaker.make(": 46 */", "while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 72 */", "while (i0 < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 77 */", "i1--;"))); + assertTrue(source.matches(PatternMaker.make(": 79 */", "while (i1 > 0);"))); + assertTrue(source.matches(PatternMaker.make(": 98 */", "while (--i > 0.0F);"))); + assertTrue(source.matches(PatternMaker.make(": 108 */", "while (i-- > 0.0F);"))); assertTrue(source.indexOf("while ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4));") != -1); assertTrue(source.indexOf("while ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4));") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk901DoWhile() throws Exception { + String internalClassName = "org/jd/core/test/DoWhile"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-9.0.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/DoWhile"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -491,32 +520,35 @@ public void testJdk901DoWhile() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 24]", "} while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 32]", "} while (this == null);"))); - assertTrue(source.matches(PatternMaker.make(": 44]", "++i;"))); - assertTrue(source.matches(PatternMaker.make(": 46]", "while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 72]", "while (i0 < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 77]", "i1--;"))); - assertTrue(source.matches(PatternMaker.make(": 79]", "while (i1 > 0);"))); - assertTrue(source.matches(PatternMaker.make(": 98]", "while (--i > 0.0F);"))); - assertTrue(source.matches(PatternMaker.make(": 108]", "while (i-- > 0.0F);"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 24 */", "} while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 32 */", "} while (this == null);"))); + assertTrue(source.matches(PatternMaker.make(": 44 */", "++i;"))); + assertTrue(source.matches(PatternMaker.make(": 46 */", "while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 72 */", "while (i0 < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 77 */", "i1--;"))); + assertTrue(source.matches(PatternMaker.make(": 79 */", "while (i1 > 0);"))); + assertTrue(source.matches(PatternMaker.make(": 98 */", "while (--i > 0.0F);"))); + assertTrue(source.matches(PatternMaker.make(": 108 */", "while (i-- > 0.0F);"))); assertTrue(source.indexOf("while ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4));") != -1); assertTrue(source.indexOf("while ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4));") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk1002DoWhile() throws Exception { + String internalClassName = "org/jd/core/test/DoWhile"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-10.0.2.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/DoWhile"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -531,25 +563,28 @@ public void testJdk1002DoWhile() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 24]", "} while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 32]", "} while (this == null);"))); - assertTrue(source.matches(PatternMaker.make(": 44]", "++i;"))); - assertTrue(source.matches(PatternMaker.make(": 46]", "while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 72]", "while (i0 < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 77]", "i1--;"))); - assertTrue(source.matches(PatternMaker.make(": 79]", "while (i1 > 0);"))); - assertTrue(source.matches(PatternMaker.make(": 98]", "while (--i > 0.0F);"))); - assertTrue(source.matches(PatternMaker.make(": 108]", "while (i-- > 0.0F);"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 24 */", "} while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 32 */", "} while (this == null);"))); + assertTrue(source.matches(PatternMaker.make(": 44 */", "++i;"))); + assertTrue(source.matches(PatternMaker.make(": 46 */", "while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 72 */", "while (i0 < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 77 */", "i1--;"))); + assertTrue(source.matches(PatternMaker.make(": 79 */", "while (i1 > 0);"))); + assertTrue(source.matches(PatternMaker.make(": 98 */", "while (--i > 0.0F);"))); + assertTrue(source.matches(PatternMaker.make(": 108 */", "while (i-- > 0.0F);"))); assertTrue(source.indexOf("while ((i == 1 || (i == 5 && i == 6 && i == 7) || i == 8 || (i == 9 && i == 10 && i == 11)) && (i == 4 || i % 200 > 50) && (i > 3 || i > 4));") != -1); assertTrue(source.indexOf("while ((i == 1 && (i == 5 || i == 6 || i == 7) && i == 8 && (i == 9 || i == 10 || i == 11)) || (i == 4 && i % 200 > 50) || (i > 3 && i > 4));") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170BreakContinue() throws Exception { + String internalClassName = "org/jd/core/test/BreakContinue"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -557,7 +592,7 @@ public void testJdk170BreakContinue() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/BreakContinue"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -573,42 +608,45 @@ public void testJdk170BreakContinue() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 15: 15]", "if (i == 1)"))); - assertTrue(source.matches(PatternMaker.make("[ 16: 0]", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 18: 18]", "if (i == 2)"))); - assertTrue(source.matches(PatternMaker.make("[ 19: 0]", "continue;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 15: 15 */", "if (i == 1)"))); + assertTrue(source.matches(PatternMaker.make("/* 16: 0 */", "continue;"))); + assertTrue(source.matches(PatternMaker.make("/* 18: 18 */", "if (i == 2)"))); + assertTrue(source.matches(PatternMaker.make("/* 19: 0 */", "continue;"))); - assertTrue(source.matches(PatternMaker.make("[ 31: 31]", "label18: while (i > 1)"))); - assertTrue(source.matches(PatternMaker.make("[ 37: 0]", "continue label18;"))); - assertTrue(source.matches(PatternMaker.make("[ 40: 0]", "break label18;"))); + assertTrue(source.matches(PatternMaker.make("/* 31: 31 */", "label18: while (i > 1)"))); + assertTrue(source.matches(PatternMaker.make("/* 37: 0 */", "continue label18;"))); + assertTrue(source.matches(PatternMaker.make("/* 40: 0 */", "break label18;"))); - assertTrue(source.matches(PatternMaker.make("[ 54: 54]", "label17: while (i > 1)"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 63: 0]", "continue label17;"))); + assertTrue(source.matches(PatternMaker.make("/* 54: 54 */", "label17: while (i > 1)"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 63: 0 */", "continue label17;"))); - assertTrue(source.matches(PatternMaker.make("[ 78: 0]", "label13:"))); - assertTrue(source.matches(PatternMaker.make("[ 83: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 86: 0]", "break label13;"))); + assertTrue(source.matches(PatternMaker.make("/* 78: 0 */", "label13:"))); + assertTrue(source.matches(PatternMaker.make("/* 83: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 86: 0 */", "break label13;"))); - assertTrue(source.matches(PatternMaker.make("[ 101: 0]", "label15:", "do {"))); - assertTrue(source.matches(PatternMaker.make("[ 106: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 109: 0]", "break label15;"))); + assertTrue(source.matches(PatternMaker.make("/* 101: 0 */", "label15:", "do {"))); + assertTrue(source.matches(PatternMaker.make("/* 106: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 109: 0 */", "break label15;"))); - assertTrue(source.matches(PatternMaker.make("[ 123: 0]", "label24:", "do {"))); - assertTrue(source.matches(PatternMaker.make("[ 133: 0]", "continue label24;"))); - assertTrue(source.matches(PatternMaker.make("[ 135: 0]", "break label24;"))); - assertTrue(source.matches(PatternMaker.make("[ 138: 0]", "break label23;"))); + assertTrue(source.matches(PatternMaker.make("/* 123: 0 */", "label24:", "do {"))); + assertTrue(source.matches(PatternMaker.make("/* 133: 0 */", "continue label24;"))); + assertTrue(source.matches(PatternMaker.make("/* 135: 0 */", "break label24;"))); + assertTrue(source.matches(PatternMaker.make("/* 138: 0 */", "break label23;"))); - assertTrue(source.matches(PatternMaker.make("[ 155: 0]", "label16:", "do {"))); - assertTrue(source.matches(PatternMaker.make("[ 162: 0]", "break label16;"))); + assertTrue(source.matches(PatternMaker.make("/* 155: 0 */", "label16:", "do {"))); + assertTrue(source.matches(PatternMaker.make("/* 162: 0 */", "break label16;"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170For() throws Exception { + String internalClassName = "org/jd/core/test/For"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -616,7 +654,7 @@ public void testJdk170For() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/For"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -632,61 +670,64 @@ public void testJdk170For() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 20]", "for (int i = 0; i < 10; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 42]", "int i = 0;"))); - assertTrue(source.matches(PatternMaker.make(": 44]", "for (; i < 10; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 54]", "for (; i < 10; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 64]", "for (int i = 0;; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 72]", "for (;; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 80]", "for (int i = 0; i < 10;)"))); - assertTrue(source.matches(PatternMaker.make(": 88]", "while (i < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 96]", "int i = 0;"))); - assertTrue(source.matches(PatternMaker.make("[ 104: 0]", "while (true)"))); - assertTrue(source.matches(PatternMaker.make(": 112]", "for (int i = 0, j = i, size = 10; i < size; j += ++i)"))); - assertTrue(source.matches(PatternMaker.make(": 122]", "int i = 0, j = i, size = 10;"))); - assertTrue(source.matches(PatternMaker.make(": 123]", "for (; i < size;"))); - assertTrue(source.matches(PatternMaker.make(": 124]", "j += ++i)"))); - assertTrue(source.matches(PatternMaker.make(": 134]", "int i = 0;"))); - assertTrue(source.matches(PatternMaker.make(": 135]", "int j = i;"))); - assertTrue(source.matches(PatternMaker.make(": 136]", "int size = 10;"))); - assertTrue(source.matches(PatternMaker.make(": 137]", "for (; i < size;"))); - assertTrue(source.matches(PatternMaker.make(": 138]", "i++,"))); - assertTrue(source.matches(PatternMaker.make(": 139]", "j += i)"))); - assertTrue(source.matches(PatternMaker.make(": 149]", "int i = 0;"))); - assertTrue(source.matches(PatternMaker.make(": 151]", "int j = i;"))); - assertTrue(source.matches(PatternMaker.make(": 153]", "int size = 10;"))); - assertTrue(source.matches(PatternMaker.make(": 155]", "for (; i < size;"))); - assertTrue(source.matches(PatternMaker.make(": 157]", "i++,"))); - assertTrue(source.matches(PatternMaker.make(": 159]", "j += i)"))); - assertTrue(source.matches(PatternMaker.make(": 169]", "for (int i = 0; i < 10; i++);"))); - assertTrue(source.matches(PatternMaker.make(": 177]", "for (; i < 10; i++);"))); - assertTrue(source.matches(PatternMaker.make(": 185]", "for (int i = 0;; i++);"))); - assertTrue(source.matches(PatternMaker.make("[ 190: 0]", "while (true)"))); - assertTrue(source.matches(PatternMaker.make(": 191]", "i++;"))); - assertTrue(source.matches(PatternMaker.make(": 197]", "for (int i = 0; i < 10;);"))); - assertTrue(source.matches(PatternMaker.make(": 203]", "for (int[] i = { 0 }; i.length < 10;);"))); - assertTrue(source.matches(PatternMaker.make(": 209]", "for (int i = 0, j = i, k = i; i < 10;);"))); - assertTrue(source.matches(PatternMaker.make(": 215]", "for (int[] i = { 0 }, j = i, k = j; i.length < 10;);"))); - assertTrue(source.matches(PatternMaker.make(": 221]", "for (int i = 0, j[] = { 1 }; i < 10;);"))); - assertTrue(source.matches(PatternMaker.make(": 227]", "while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 233]", "int i = 0;"))); - assertTrue(source.matches(PatternMaker.make("[ 234: 0]", "while (true);"))); - assertTrue(source.matches(PatternMaker.make(": 245]", "for (int i = 0, j = i, size = 10; i < size; j += ++i);"))); - assertTrue(source.matches(PatternMaker.make("[ 253: 0]", "while (true) {"))); - assertTrue(source.matches(PatternMaker.make(": 264]", "for (String s : list)"))); - assertTrue(source.matches(PatternMaker.make(": 310]", "for (int i : new int[] { 4 })"))); - assertTrue(source.matches(PatternMaker.make(": 389]", "for (String s : array)"))); - assertTrue(source.matches(PatternMaker.make(": 403]", "for (String s : list)"))); - assertTrue(source.matches(PatternMaker.make(": 411]", "Iterator iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); - - assertTrue(source.indexOf("[ 489: 489]") != -1); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 20 */", "for (int i = 0; i < 10; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 42 */", "int i = 0;"))); + assertTrue(source.matches(PatternMaker.make(": 44 */", "for (; i < 10; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 54 */", "for (; i < 10; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 64 */", "for (int i = 0;; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 72 */", "for (;; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 80 */", "for (int i = 0; i < 10;)"))); + assertTrue(source.matches(PatternMaker.make(": 88 */", "while (i < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 96 */", "int i = 0;"))); + assertTrue(source.matches(PatternMaker.make("/* 104: 0 */", "while (true)"))); + assertTrue(source.matches(PatternMaker.make(": 112 */", "for (int i = 0, j = i, size = 10; i < size; j += ++i)"))); + assertTrue(source.matches(PatternMaker.make(": 122 */", "int i = 0, j = i, size = 10;"))); + assertTrue(source.matches(PatternMaker.make(": 123 */", "for (; i < size;"))); + assertTrue(source.matches(PatternMaker.make(": 124 */", "j += ++i)"))); + assertTrue(source.matches(PatternMaker.make(": 134 */", "int i = 0;"))); + assertTrue(source.matches(PatternMaker.make(": 135 */", "int j = i;"))); + assertTrue(source.matches(PatternMaker.make(": 136 */", "int size = 10;"))); + assertTrue(source.matches(PatternMaker.make(": 137 */", "for (; i < size;"))); + assertTrue(source.matches(PatternMaker.make(": 138 */", "i++,"))); + assertTrue(source.matches(PatternMaker.make(": 139 */", "j += i)"))); + assertTrue(source.matches(PatternMaker.make(": 149 */", "int i = 0;"))); + assertTrue(source.matches(PatternMaker.make(": 151 */", "int j = i;"))); + assertTrue(source.matches(PatternMaker.make(": 153 */", "int size = 10;"))); + assertTrue(source.matches(PatternMaker.make(": 155 */", "for (; i < size;"))); + assertTrue(source.matches(PatternMaker.make(": 157 */", "i++,"))); + assertTrue(source.matches(PatternMaker.make(": 159 */", "j += i)"))); + assertTrue(source.matches(PatternMaker.make(": 169 */", "for (int i = 0; i < 10; i++);"))); + assertTrue(source.matches(PatternMaker.make(": 177 */", "for (; i < 10; i++);"))); + assertTrue(source.matches(PatternMaker.make(": 185 */", "for (int i = 0;; i++);"))); + assertTrue(source.matches(PatternMaker.make("/* 190: 0 */", "while (true)"))); + assertTrue(source.matches(PatternMaker.make(": 191 */", "i++;"))); + assertTrue(source.matches(PatternMaker.make(": 197 */", "for (int i = 0; i < 10;);"))); + assertTrue(source.matches(PatternMaker.make(": 203 */", "for (int[] i = { 0 }; i.length < 10;);"))); + assertTrue(source.matches(PatternMaker.make(": 209 */", "for (int i = 0, j = i, k = i; i < 10;);"))); + assertTrue(source.matches(PatternMaker.make(": 215 */", "for (int[] i = { 0 }, j = i, k = j; i.length < 10;);"))); + assertTrue(source.matches(PatternMaker.make(": 221 */", "for (int i = 0, j[] = { 1 }; i < 10;);"))); + assertTrue(source.matches(PatternMaker.make(": 227 */", "while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 233 */", "int i = 0;"))); + assertTrue(source.matches(PatternMaker.make("/* 234: 0 */", "while (true);"))); + assertTrue(source.matches(PatternMaker.make(": 245 */", "for (int i = 0, j = i, size = 10; i < size; j += ++i);"))); + assertTrue(source.matches(PatternMaker.make("/* 253: 0 */", "while (true) {"))); + assertTrue(source.matches(PatternMaker.make(": 264 */", "for (String s : list)"))); + assertTrue(source.matches(PatternMaker.make(": 310 */", "for (int i : new int[] { 4 })"))); + assertTrue(source.matches(PatternMaker.make(": 389 */", "for (String s : array)"))); + assertTrue(source.matches(PatternMaker.make(": 403 */", "for (String s : list)"))); + assertTrue(source.matches(PatternMaker.make(": 411 */", "Iterator> iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); + + assertTrue(source.indexOf("/* 489: 489 */") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170NoDebugFor() throws Exception { + String internalClassName = "org/jd/core/test/For"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0-no-debug-info.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -708,6 +749,7 @@ public void testJdk170NoDebugFor() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.matches(PatternMaker.make("for (byte b = 0; b < 10; b++)"))); assertTrue(source.matches(PatternMaker.make("for (byte b = 0;; b++)"))); assertTrue(source.matches(PatternMaker.make("for (byte b = 0; b < 10; b++)"))); @@ -718,12 +760,14 @@ public void testJdk170NoDebugFor() throws Exception { assertTrue(source.matches(PatternMaker.make("for (String str : paramList)"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + // TODO: fix error on line 168. assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk150For() throws Exception { + String internalClassName = "org/jd/core/test/For"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.5.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -731,7 +775,7 @@ public void testJdk150For() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/For"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -747,24 +791,27 @@ public void testJdk150For() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 16]", "for (byte b = 0; b < 10; b++)"))); - assertTrue(source.matches(PatternMaker.make(": 84]", "while (paramInt < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 269]", "for (paramInt = 0; paramInt < 10; paramInt++)"))); - assertTrue(source.matches(PatternMaker.make(": 306]", "for (int i : new int[] { 4 })"))); - assertTrue(source.matches(PatternMaker.make("[ 343: 0]", "do {"))); - assertTrue(source.matches(PatternMaker.make(": 345]", "while (b < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 381]", "for (String str : paramArrayOfString)"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "for (String str : paramList)"))); - assertTrue(source.matches(PatternMaker.make(": 407]", "Iterator iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); - assertTrue(source.matches(PatternMaker.make(": 423]", "for (byte b = 0; b < 3; b++)"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 16 */", "for (byte b = 0; b < 10; b++)"))); + assertTrue(source.matches(PatternMaker.make(": 84 */", "while (paramInt < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 269 */", "for (paramInt = 0; paramInt < 10; paramInt++)"))); + assertTrue(source.matches(PatternMaker.make(": 306 */", "for (int i : new int[] { 4 })"))); + assertTrue(source.matches(PatternMaker.make("/* 343: 0 */", "do {"))); + assertTrue(source.matches(PatternMaker.make(": 345 */", "while (b < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 381 */", "for (String str : paramArrayOfString)"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "for (String str : paramList)"))); + assertTrue(source.matches(PatternMaker.make(": 407 */", "Iterator iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); + assertTrue(source.matches(PatternMaker.make(": 423 */", "for (byte b = 0; b < 3; b++)"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + // TODO: fix error on line 205. assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk160For() throws Exception { + String internalClassName = "org/jd/core/test/For"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.6.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -772,7 +819,7 @@ public void testJdk160For() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/For"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -788,24 +835,27 @@ public void testJdk160For() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 16]", "for (int i = 0; i < 10; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 84]", "while (i < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 269]", "for (i = 0; i < 10; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 306]", "for (int i : new int[] { 4 })"))); - assertTrue(source.matches(PatternMaker.make("[ 343: 0]", "do {"))); - assertTrue(source.matches(PatternMaker.make(": 345]", "while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 381]", "for (String s : array)"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "for (String s : list)"))); - assertTrue(source.matches(PatternMaker.make(": 407]", "Iterator iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); - assertTrue(source.matches(PatternMaker.make(": 423]", "for (int i = 0; i < 3; i++)"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 16 */", "for (int i = 0; i < 10; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 84 */", "while (i < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 269 */", "for (i = 0; i < 10; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 306 */", "for (int i : new int[] { 4 })"))); + assertTrue(source.matches(PatternMaker.make("/* 343: 0 */", "do {"))); + assertTrue(source.matches(PatternMaker.make(": 345 */", "while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 381 */", "for (String s : array)"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "for (String s : list)"))); + assertTrue(source.matches(PatternMaker.make(": 407 */", "Iterator> iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); + assertTrue(source.matches(PatternMaker.make(": 423 */", "for (int i = 0; i < 3; i++)"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.6", new JavaSourceFileObject(internalClassName, source))); } @Test public void testIbmJ9For() throws Exception { + String internalClassName = "org/jd/core/test/For"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-ibm-j9_vm.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -813,7 +863,7 @@ public void testIbmJ9For() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/For"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -829,23 +879,26 @@ public void testIbmJ9For() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 84]", "while (i < 10)"))); - assertTrue(source.matches(PatternMaker.make(": 269]", "for (i = 0; i < 10; i++)"))); - assertTrue(source.matches(PatternMaker.make(": 306]", "for (int i : new int[] { 4 })"))); - assertTrue(source.matches(PatternMaker.make("[ 343: 0]", "do"))); - assertTrue(source.matches(PatternMaker.make(": 345]", "while (i < 10);"))); - assertTrue(source.matches(PatternMaker.make(": 381]", "for (String s : array)"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "for (String s : list)"))); - assertTrue(source.matches(PatternMaker.make(": 407]", "Iterator iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); - assertTrue(source.matches(PatternMaker.make(": 423]", "for (int i = 0; i < 3; i++)"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 84 */", "while (i < 10)"))); + assertTrue(source.matches(PatternMaker.make(": 269 */", "for (i = 0; i < 10; i++)"))); + assertTrue(source.matches(PatternMaker.make(": 306 */", "for (int i : new int[] { 4 })"))); + assertTrue(source.matches(PatternMaker.make("/* 343: 0 */", "do"))); + assertTrue(source.matches(PatternMaker.make(": 345 */", "while (i < 10);"))); + assertTrue(source.matches(PatternMaker.make(": 381 */", "for (String s : array)"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "for (String s : list)"))); + assertTrue(source.matches(PatternMaker.make(": 407 */", "Iterator> iterator = Arrays.asList(getClass().getInterfaces()).iterator()"))); + assertTrue(source.matches(PatternMaker.make(": 423 */", "for (int i = 0; i < 3; i++)"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170Array() throws Exception { + String internalClassName = "org/jd/core/test/Array"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -855,7 +908,7 @@ public void testJdk170Array() throws Exception { configuration.put("realignLineNumbers", Boolean.FALSE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Array"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -871,27 +924,30 @@ public void testJdk170Array() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 12]", "int[] i1 = new int[1];"))); - assertTrue(source.matches(PatternMaker.make(": 13]", "int[][] i2 = new int[1][];"))); - assertTrue(source.matches(PatternMaker.make(": 14]", "int[][][] i3 = new int[1][][];"))); - assertTrue(source.matches(PatternMaker.make(": 15]", "int[][][] i4 = new int[1][2][];"))); - assertTrue(source.matches(PatternMaker.make(": 22]", "String[][][][] s5 = new String[1][2][][];"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 12 */", "int[] i1 = new int[1];"))); + assertTrue(source.matches(PatternMaker.make(": 13 */", "int[][] i2 = new int[1][];"))); + assertTrue(source.matches(PatternMaker.make(": 14 */", "int[][][] i3 = new int[1][][];"))); + assertTrue(source.matches(PatternMaker.make(": 15 */", "int[][][] i4 = new int[1][2][];"))); + assertTrue(source.matches(PatternMaker.make(": 22 */", "String[][][][] s5 = new String[1][2][][];"))); - assertTrue(source.matches(PatternMaker.make(": 26]", "byte[] b1 = { 1, 2 } ;"))); - assertTrue(source.matches(PatternMaker.make(": 27]", "byte[][] b2 = { { 1, 2 } } ;"))); - assertTrue(source.matches(PatternMaker.make(": 28]", "byte[][][][] b3 = { { { 3, 4 } } } ;"))); + assertTrue(source.matches(PatternMaker.make(": 26 */", "byte[] b1 = { 1, 2 } ;"))); + assertTrue(source.matches(PatternMaker.make(": 27 */", "byte[][] b2 = { { 1, 2 } } ;"))); + assertTrue(source.matches(PatternMaker.make(": 28 */", "byte[][][][] b3 = { { { 3, 4 } } } ;"))); - assertTrue(source.matches(PatternMaker.make(": 48]", "testException1(new Exception[]", "{ new Exception(\"1\") } );"))); + assertTrue(source.matches(PatternMaker.make(": 48 */", "testException1(new Exception[]", "{ new Exception(\"1\") } );"))); - assertTrue(source.matches(PatternMaker.make(": 73]", "testInt2(new int[][]", "{ { 1 } ,"))); + assertTrue(source.matches(PatternMaker.make(": 73 */", "testInt2(new int[][]", "{ { 1 } ,"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk150Array() throws Exception { + String internalClassName = "org/jd/core/test/Array"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.5.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -901,7 +957,7 @@ public void testJdk150Array() throws Exception { configuration.put("realignLineNumbers", Boolean.FALSE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Array"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -917,23 +973,26 @@ public void testJdk150Array() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 13]", "int[][] arrayOfInt1 = new int[1][];"))); - assertTrue(source.matches(PatternMaker.make(": 30]", "int[][] arrayOfInt1 = { { 0, 1, 2"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 13 */", "int[][] arrayOfInt1 = new int[1][];"))); + assertTrue(source.matches(PatternMaker.make(": 30 */", "int[][] arrayOfInt1 = { { 0, 1, 2"))); - assertTrue(source.matches(PatternMaker.make(": 52]", "testException2(new Exception[][]", "{ { new Exception(\"1\")"))); + assertTrue(source.matches(PatternMaker.make(": 52 */", "testException2(new Exception[][]", "{ { new Exception(\"1\")"))); - assertTrue(source.matches(PatternMaker.make(": 73]", "testInt2(new int[][] { { 1,"))); + assertTrue(source.matches(PatternMaker.make(": 73 */", "testInt2(new int[][] { { 1,"))); - assertTrue(source.matches(PatternMaker.make(": 73]", "testInt2(new int[][] { { 1,"))); - assertTrue(source.matches(PatternMaker.make(": 75]", "testInt3(new int[][][] { { { 0, 1"))); + assertTrue(source.matches(PatternMaker.make(": 73 */", "testInt2(new int[][] { { 1,"))); + assertTrue(source.matches(PatternMaker.make(": 75 */", "testInt3(new int[][][] { { { 0, 1"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170Assert() throws Exception { + String internalClassName = "org/jd/core/test/Assert"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -941,7 +1000,7 @@ public void testJdk170Assert() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Assert"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -957,21 +1016,24 @@ public void testJdk170Assert() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 16: 16]", "assert false : \"false\";"))); - assertTrue(source.matches(PatternMaker.make("[ 17: 17]", "assert i == 0 || i == 1;"))); - assertTrue(source.matches(PatternMaker.make("[ 18: 18]", "assert i == 2 && i < 3;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 16: 16 */", "assert false : \"false\";"))); + assertTrue(source.matches(PatternMaker.make("/* 17: 17 */", "assert i == 0 || i == 1;"))); + assertTrue(source.matches(PatternMaker.make("/* 18: 18 */", "assert i == 2 && i < 3;"))); - assertTrue(source.matches(PatternMaker.make("[ 34: 34]", "assert new BigDecimal(i) == BigDecimal.ONE;"))); + assertTrue(source.matches(PatternMaker.make("/* 34: 34 */", "assert new BigDecimal(i) == BigDecimal.ONE;"))); - assertTrue(source.matches(PatternMaker.make("[ 41: 41]", "assert check() : \"boom\";"))); + assertTrue(source.matches(PatternMaker.make("/* 41: 41 */", "assert check() : \"boom\";"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk150Assert() throws Exception { + String internalClassName = "org/jd/core/test/Assert"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.5.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -979,7 +1041,7 @@ public void testJdk150Assert() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Assert"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -995,28 +1057,31 @@ public void testJdk150Assert() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 16: 16]", "assert false : \"false\";"))); - assertTrue(source.matches(PatternMaker.make("[ 17: 17]", "assert paramInt == 0 || paramInt == 1;"))); - assertTrue(source.matches(PatternMaker.make("[ 18: 18]", "assert paramInt == 2 && paramInt < 3;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 16: 16 */", "assert false : \"false\";"))); + assertTrue(source.matches(PatternMaker.make("/* 17: 17 */", "assert paramInt == 0 || paramInt == 1;"))); + assertTrue(source.matches(PatternMaker.make("/* 18: 18 */", "assert paramInt == 2 && paramInt < 3;"))); - assertTrue(source.matches(PatternMaker.make("[ 34: 34]", "assert new BigDecimal(paramInt) == BigDecimal.ONE;"))); + assertTrue(source.matches(PatternMaker.make("/* 34: 34 */", "assert new BigDecimal(paramInt) == BigDecimal.ONE;"))); - assertTrue(source.matches(PatternMaker.make("[ 41: 41]", "assert check() : \"boom\";"))); + assertTrue(source.matches(PatternMaker.make("/* 41: 41 */", "assert check() : \"boom\";"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk150AnonymousClass() throws Exception { + String internalClassName = "org/jd/core/test/AnonymousClass"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.5.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/AnonymousClass"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -1031,30 +1096,34 @@ public void testJdk150AnonymousClass() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 21]", "Object object = new Object()"))); - assertTrue(source.matches(PatternMaker.make(": 23]", "return \"toString() return \" + super.toString() + \" at \" + AnonymousClass.this.time;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 21 */", "Object object = new Object()"))); + assertTrue(source.matches(PatternMaker.make(": 23 */", "return \"toString() return \" + super.toString() + \" at \" + AnonymousClass.this.time;"))); - assertTrue(source.matches(PatternMaker.make(": 37]", "final long l1 = System.currentTimeMillis();"))); - assertTrue(source.matches(PatternMaker.make(": 39]", "Enumeration enumeration = new Enumeration()"))); - assertTrue(source.matches(PatternMaker.make(": 40]", "Iterator i = AnonymousClass.this.list.iterator();"))); - assertTrue(source.matches(PatternMaker.make(": 44]", "return (this.i.hasNext() && s1 == s2 && i1 > l1);"))); + assertTrue(source.matches(PatternMaker.make(": 37 */", "final long l1 = System.currentTimeMillis();"))); + assertTrue(source.matches(PatternMaker.make(": 39 */", "Enumeration enumeration = new Enumeration()"))); + assertTrue(source.matches(PatternMaker.make(": 40 */", "Iterator i = AnonymousClass.this.list.iterator();"))); + assertTrue(source.matches(PatternMaker.make(": 44 */", "return (this.i.hasNext() && s1 == s2 && i1 > l1);"))); assertTrue(source.indexOf("return this.i.next();") != -1); - assertTrue(source.matches(PatternMaker.make(": 52]", "test(enumeration, \"test\");"))); - assertTrue(source.matches(PatternMaker.make(": 55]", "System.out.println(\"end\");"))); + assertTrue(source.matches(PatternMaker.make(": 52 */", "test(enumeration, \"test\");"))); + assertTrue(source.matches(PatternMaker.make(": 55 */", "System.out.println(\"end\");"))); - assertTrue(source.matches(PatternMaker.make(": 67]", "if (s1 == s2 && i == 5)"))); + assertTrue(source.matches(PatternMaker.make(": 67 */", "if (s1 == s2 && i == 5)"))); - assertTrue(source.matches(PatternMaker.make(": 90]", "Serializable serializable = new Serializable()"))); - assertTrue(source.matches(PatternMaker.make(": 96]", "return (abc.equals(param2Object) || def.equals(param2Object) || str1.equals(param2Object) || str2.equals(param2Object));"))); - assertTrue(source.matches(PatternMaker.make(": 104]", "System.out.println(\"end\");"))); + assertTrue(source.matches(PatternMaker.make(": 90 */", "Serializable serializable = new Serializable()"))); + assertTrue(source.matches(PatternMaker.make(": 96 */", "return (abc.equals(param2Object) || def.equals(param2Object) || str1.equals(param2Object) || str2.equals(param2Object));"))); + assertTrue(source.matches(PatternMaker.make(": 104 */", "System.out.println(\"end\");"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source), + new JavaSourceFileObject("org/jd/core/test/annotation/Name", "package org.jd.core.test.annotation; public @interface Name {String value();}"))); } @Test public void testJdk170Switch() throws Exception { + String internalClassName = "org/jd/core/test/Switch"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1062,7 +1131,7 @@ public void testJdk170Switch() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Switch"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1078,46 +1147,49 @@ public void testJdk170Switch() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 15: 15]", "switch (i)"))); - assertTrue(source.matches(PatternMaker.make("[ 16: 0]", "case 0:"))); - assertTrue(source.matches(PatternMaker.make("[ 17: 17]", "System.out.println(\"0\");"))); - assertTrue(source.matches(PatternMaker.make("[ 18: 0]", "break;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 15: 15 */", "switch (i)"))); + assertTrue(source.matches(PatternMaker.make("/* 16: 0 */", "case 0:"))); + assertTrue(source.matches(PatternMaker.make("/* 17: 17 */", "System.out.println(\"0\");"))); + assertTrue(source.matches(PatternMaker.make("/* 18: 0 */", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 34: 0]", "case 0:"))); - assertTrue(source.matches(PatternMaker.make("[ 35: 35]", "System.out.println(\"0\");"))); - assertTrue(source.matches(PatternMaker.make("[ 36: 0]", "case 1:"))); + assertTrue(source.matches(PatternMaker.make("/* 34: 0 */", "case 0:"))); + assertTrue(source.matches(PatternMaker.make("/* 35: 35 */", "System.out.println(\"0\");"))); + assertTrue(source.matches(PatternMaker.make("/* 36: 0 */", "case 1:"))); - assertTrue(source.matches(PatternMaker.make("[ 56: 0]", "default:"))); + assertTrue(source.matches(PatternMaker.make("/* 56: 0 */", "default:"))); - assertTrue(source.matches(PatternMaker.make("[ 110: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 111: 0]", "case 1:"))); - assertTrue(source.matches(PatternMaker.make("[ 112: 112]", "System.out.println(\"1\");"))); - assertTrue(source.matches(PatternMaker.make("[ 113: 113]", "throw new RuntimeException(\"boom\");"))); + assertTrue(source.matches(PatternMaker.make("/* 110: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 111: 0 */", "case 1:"))); + assertTrue(source.matches(PatternMaker.make("/* 112: 112 */", "System.out.println(\"1\");"))); + assertTrue(source.matches(PatternMaker.make("/* 113: 113 */", "throw new RuntimeException(\"boom\");"))); - assertTrue(source.matches(PatternMaker.make("[ 134: 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make("/* 134: 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make("[ 171: 0]", "case 3:"))); - assertTrue(source.matches(PatternMaker.make("[ 172: 0]", "case 4:"))); - assertTrue(source.matches(PatternMaker.make("[ 173: 173]", "System.out.println(\"3 or 4\");"))); - assertTrue(source.matches(PatternMaker.make("[ 174: 0]", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 171: 0 */", "case 3:"))); + assertTrue(source.matches(PatternMaker.make("/* 172: 0 */", "case 4:"))); + assertTrue(source.matches(PatternMaker.make("/* 173: 173 */", "System.out.println(\"3 or 4\");"))); + assertTrue(source.matches(PatternMaker.make("/* 174: 0 */", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 265: 0]", "case 1:"))); - assertTrue(source.matches(PatternMaker.make("[ 266: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 267: 0]", "default:"))); + assertTrue(source.matches(PatternMaker.make("/* 265: 0 */", "case 1:"))); + assertTrue(source.matches(PatternMaker.make("/* 266: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 267: 0 */", "default:"))); - assertTrue(source.matches(PatternMaker.make("[ 283: 0]", "case 1:"))); - assertTrue(source.matches(PatternMaker.make("[ 284: 0]", "case 2:"))); - assertTrue(source.matches(PatternMaker.make("[ 285: 0]", "case 3:"))); - assertTrue(source.matches(PatternMaker.make("[ 286: 0]", "break;"))); - assertTrue(source.matches(PatternMaker.make("[ 288: 0]", "default:"))); + assertTrue(source.matches(PatternMaker.make("/* 283: 0 */", "case 1:"))); + assertTrue(source.matches(PatternMaker.make("/* 284: 0 */", "case 2:"))); + assertTrue(source.matches(PatternMaker.make("/* 285: 0 */", "case 3:"))); + assertTrue(source.matches(PatternMaker.make("/* 286: 0 */", "break;"))); + assertTrue(source.matches(PatternMaker.make("/* 288: 0 */", "default:"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170AdvancedSwitch() throws Exception { + String internalClassName = "org/jd/core/test/AdvancedSwitch"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1125,7 +1197,7 @@ public void testJdk170AdvancedSwitch() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/AdvancedSwitch"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1141,32 +1213,35 @@ public void testJdk170AdvancedSwitch() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 13: 13]", "A,", "B,", "C;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 13: 13 */", "A,", "B,", "C;"))); - assertTrue(source.matches(PatternMaker.make("[ 19: 19]", "switch (te)"))); - assertTrue(source.matches(PatternMaker.make("[ 20: 0]", "case A:"))); - assertTrue(source.matches(PatternMaker.make("[ 22: 0]", "case B:"))); - assertTrue(source.matches(PatternMaker.make("[ 25: 0]", "case C:"))); + assertTrue(source.matches(PatternMaker.make("/* 19: 19 */", "switch (te)"))); + assertTrue(source.matches(PatternMaker.make("/* 20: 0 */", "case A:"))); + assertTrue(source.matches(PatternMaker.make("/* 22: 0 */", "case B:"))); + assertTrue(source.matches(PatternMaker.make("/* 25: 0 */", "case C:"))); - assertTrue(source.matches(PatternMaker.make("[ 39: 0]", "case A:"))); - assertTrue(source.matches(PatternMaker.make("[ 40: 0]", "case B:"))); - assertTrue(source.matches(PatternMaker.make("[ 41: 41]", "System.out.println(\"A or B\");"))); + assertTrue(source.matches(PatternMaker.make("/* 39: 0 */", "case A:"))); + assertTrue(source.matches(PatternMaker.make("/* 40: 0 */", "case B:"))); + assertTrue(source.matches(PatternMaker.make("/* 41: 41 */", "System.out.println(\"A or B\");"))); - assertTrue(source.matches(PatternMaker.make("[ 56: 56]", "switch (str)"))); - assertTrue(source.matches(PatternMaker.make("[ 57: 0]", "case \"One\":"))); - assertTrue(source.matches(PatternMaker.make("[ 58: 58]", "System.out.println(1);"))); + assertTrue(source.matches(PatternMaker.make("/* 56: 56 */", "switch (str)"))); + assertTrue(source.matches(PatternMaker.make("/* 57: 0 */", "case \"One\":"))); + assertTrue(source.matches(PatternMaker.make("/* 58: 58 */", "System.out.println(1);"))); - assertTrue(source.matches(PatternMaker.make("[ 78: 0]", "case \"One\":"))); - assertTrue(source.matches(PatternMaker.make("[ 79: 0]", "case \"POe\":"))); - assertTrue(source.matches(PatternMaker.make("[ 80: 80]", "System.out.println(\"'One' or 'POe'\");"))); + assertTrue(source.matches(PatternMaker.make("/* 78: 0 */", "case \"One\":"))); + assertTrue(source.matches(PatternMaker.make("/* 79: 0 */", "case \"POe\":"))); + assertTrue(source.matches(PatternMaker.make("/* 80: 80 */", "System.out.println(\"'One' or 'POe'\");"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testEclipseJavaCompiler321Switch() throws Exception { + String internalClassName = "org/jd/core/test/Switch"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-eclipse-java-compiler-3.2.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1174,7 +1249,7 @@ public void testEclipseJavaCompiler321Switch() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Switch"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1190,14 +1265,17 @@ public void testEclipseJavaCompiler321Switch() throws Exception { printSource(source); - assertTrue(source.indexOf("[ 239: 239]") != -1); + // Check decompiled source code + assertTrue(source.indexOf("/* 239: 239 */") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testEclipseJavaCompiler3130Switch() throws Exception { + String internalClassName = "org/jd/core/test/Switch"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-eclipse-java-compiler-3.13.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1205,7 +1283,7 @@ public void testEclipseJavaCompiler3130Switch() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Switch"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1221,21 +1299,24 @@ public void testEclipseJavaCompiler3130Switch() throws Exception { printSource(source); - assertTrue(source.indexOf("[ 239: 239]") != -1); + // Check decompiled source code + assertTrue(source.indexOf("/* 239: 239 */") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk118TernaryOperator() throws Exception { + String internalClassName = "org/jd/core/test/TernaryOperator"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.1.8.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TernaryOperator"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -1250,35 +1331,38 @@ public void testJdk118TernaryOperator() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 13]", "this.str ="))); - assertTrue(source.matches(PatternMaker.make(": 14]", "(s == null) ?"))); - assertTrue(source.matches(PatternMaker.make(": 15]", "\"1\""))); - assertTrue(source.matches(PatternMaker.make(": 16]", "\"2\";"))); - assertTrue(source.matches(PatternMaker.make(": 24]", "s = (s == null) ? ((s == null) ? \"1\" : \"2\") : ((s == null) ? \"3\" : \"4\");"))); - assertTrue(source.matches(PatternMaker.make(": 34]", "return !(s != s || time < time);"))); - assertTrue(source.matches(PatternMaker.make(": 40]", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); - assertTrue(source.matches(PatternMaker.make(": 60]", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); - assertTrue(source.matches(PatternMaker.make(": 71]", "if ((s1 == null) ? false : (s1.length() > 0))"))); - assertTrue(source.matches(PatternMaker.make(": 82]", "if (s1 != null && s1.length() > 0)"))); - assertTrue(source.matches(PatternMaker.make(": 126]", "if (s1 == null && false)"))); - assertTrue(source.matches(PatternMaker.make(": 137]", "if (s1 == s2 && ((s1 == null) ? (s2 == null) : s1.equals(s2)) && s1 == s2)"))); - assertTrue(source.matches(PatternMaker.make(": 148]", "if (s1 == s2 || ((s1 == null) ? (s2 == null) : s1.equals(s2)) || s1 == s2)"))); - assertTrue(source.matches(PatternMaker.make(": 157]", "return Short.toString((short)((this == null) ? 1 : 2));"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 13 */", "this.str ="))); + assertTrue(source.matches(PatternMaker.make(": 14 */", "(s == null) ?"))); + assertTrue(source.matches(PatternMaker.make(": 15 */", "\"1\""))); + assertTrue(source.matches(PatternMaker.make(": 16 */", "\"2\";"))); + assertTrue(source.matches(PatternMaker.make(": 24 */", "s = (s == null) ? ((s == null) ? \"1\" : \"2\") : ((s == null) ? \"3\" : \"4\");"))); + assertTrue(source.matches(PatternMaker.make(": 34 */", "return !(s != s || time < time);"))); + assertTrue(source.matches(PatternMaker.make(": 40 */", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); + assertTrue(source.matches(PatternMaker.make(": 60 */", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); + assertTrue(source.matches(PatternMaker.make(": 71 */", "if ((s1 == null) ? false : (s1.length() > 0))"))); + assertTrue(source.matches(PatternMaker.make(": 82 */", "if (s1 != null && s1.length() > 0)"))); + assertTrue(source.matches(PatternMaker.make(": 126 */", "if (s1 == null && false)"))); + assertTrue(source.matches(PatternMaker.make(": 137 */", "if (s1 == s2 && ((s1 == null) ? (s2 == null) : s1.equals(s2)) && s1 == s2)"))); + assertTrue(source.matches(PatternMaker.make(": 148 */", "if (s1 == s2 || ((s1 == null) ? (s2 == null) : s1.equals(s2)) || s1 == s2)"))); + assertTrue(source.matches(PatternMaker.make(": 157 */", "return Short.toString((short)((this == null) ? 1 : 2));"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.3", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170TernaryOperator() throws Exception { + String internalClassName = "org/jd/core/test/TernaryOperator"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TernaryOperator"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -1293,25 +1377,28 @@ public void testJdk170TernaryOperator() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 13]", "this.str = (s == null) ? \"1\" : \"2\";"))); - assertTrue(source.matches(PatternMaker.make(": 24]", "s = (s == null) ? ((s == null) ? \"1\" : \"2\") : ((s == null) ? \"3\" : \"4\");"))); - assertTrue(source.matches(PatternMaker.make(": 34]", "return (s == s && time >= time);"))); - assertTrue(source.matches(PatternMaker.make(": 40]", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); - assertTrue(source.matches(PatternMaker.make(": 60]", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); - assertTrue(source.matches(PatternMaker.make(": 71]", "if (s1 != null && s1.length() > 0)"))); - assertTrue(source.matches(PatternMaker.make(": 82]", "if (s1 != null && s1.length() > 0)"))); - assertTrue(source.matches(PatternMaker.make(": 126]", "if (s1 == null);"))); - assertTrue(source.matches(PatternMaker.make(": 137]", "if (s1 == s2 && ((s1 == null) ? (s2 == null) : s1.equals(s2)) && s1 == s2)"))); - assertTrue(source.matches(PatternMaker.make(": 148]", "if (s1 == s2 || ((s1 == null) ? (s2 == null) : s1.equals(s2)) || s1 == s2)"))); - assertTrue(source.matches(PatternMaker.make(": 157]", "return Short.toString((short)((this == null) ? 1 : 2));"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 13 */", "this.str = (s == null) ? \"1\" : \"2\";"))); + assertTrue(source.matches(PatternMaker.make(": 24 */", "s = (s == null) ? ((s == null) ? \"1\" : \"2\") : ((s == null) ? \"3\" : \"4\");"))); + assertTrue(source.matches(PatternMaker.make(": 34 */", "return (s == s && time >= time);"))); + assertTrue(source.matches(PatternMaker.make(": 40 */", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); + assertTrue(source.matches(PatternMaker.make(": 60 */", "if ((s1 == null) ? (s2 == null) : s1.equals(s2))"))); + assertTrue(source.matches(PatternMaker.make(": 71 */", "if (s1 != null && s1.length() > 0)"))); + assertTrue(source.matches(PatternMaker.make(": 82 */", "if (s1 != null && s1.length() > 0)"))); + assertTrue(source.matches(PatternMaker.make(": 126 */", "if (s1 == null);"))); + assertTrue(source.matches(PatternMaker.make(": 137 */", "if (s1 == s2 && ((s1 == null) ? (s2 == null) : s1.equals(s2)) && s1 == s2)"))); + assertTrue(source.matches(PatternMaker.make(": 148 */", "if (s1 == s2 || ((s1 == null) ? (s2 == null) : s1.equals(s2)) || s1 == s2)"))); + assertTrue(source.matches(PatternMaker.make(": 157 */", "return Short.toString((short)((this == null) ? 1 : 2));"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170TryWithResources() throws Exception { + String internalClassName = "org/jd/core/test/TryWithResources"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1319,7 +1406,7 @@ public void testJdk170TryWithResources() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryWithResources"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1335,34 +1422,37 @@ public void testJdk170TryWithResources() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 12]", "try (FileInputStream input = new FileInputStream(path))"))); - - assertTrue(source.matches(PatternMaker.make(": 49]", "try (FileInputStream input = new FileInputStream(path))"))); - assertTrue(source.matches(PatternMaker.make(": 57]", "e.printStackTrace();"))); - assertTrue(source.matches(PatternMaker.make(": 59]", "System.out.println(\"finally\");"))); - - assertTrue(source.matches(PatternMaker.make(": 121]", "try(FileInputStream input = new FileInputStream(pathIn);"))); - assertTrue(source.matches(PatternMaker.make(": 122]", "BufferedInputStream bufferedInput = new BufferedInputStream(input);"))); - assertTrue(source.matches(PatternMaker.make(": 123]", "FileOutputStream output = new FileOutputStream(pathOut);"))); - assertTrue(source.matches(PatternMaker.make(": 124]", "BufferedOutputStream bufferedOutput = new BufferedOutputStream(output))"))); - assertTrue(source.matches(PatternMaker.make(": 132]", "if (data == -7)"))); - assertTrue(source.matches(PatternMaker.make(": 133]", "return 1;"))); - assertTrue(source.matches(PatternMaker.make(": 142]", "return 2;"))); - assertTrue(source.matches(PatternMaker.make(": 144]", "e.printStackTrace();"))); - assertTrue(source.matches(PatternMaker.make(": 150]", "e.printStackTrace();"))); - assertTrue(source.matches(PatternMaker.make(": 152]", "System.out.println(\"finally, before loop\");"))); - assertTrue(source.matches(PatternMaker.make(": 156]", "System.out.println(\"finally, after loop\");"))); - assertTrue(source.matches(PatternMaker.make(": 159]", "System.out.println(\"finally\");"))); - assertTrue(source.matches(PatternMaker.make(": 162]", "return 3;"))); - - assertTrue(source.indexOf("[ 162: 162]") != -1); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 12 */", "try (FileInputStream input = new FileInputStream(path))"))); + + assertTrue(source.matches(PatternMaker.make(": 49 */", "try (FileInputStream input = new FileInputStream(path))"))); + assertTrue(source.matches(PatternMaker.make(": 57 */", "e.printStackTrace();"))); + assertTrue(source.matches(PatternMaker.make(": 59 */", "System.out.println(\"finally\");"))); + + assertTrue(source.matches(PatternMaker.make(": 121 */", "try(FileInputStream input = new FileInputStream(pathIn);"))); + assertTrue(source.matches(PatternMaker.make(": 122 */", "BufferedInputStream bufferedInput = new BufferedInputStream(input);"))); + assertTrue(source.matches(PatternMaker.make(": 123 */", "FileOutputStream output = new FileOutputStream(pathOut);"))); + assertTrue(source.matches(PatternMaker.make(": 124 */", "BufferedOutputStream bufferedOutput = new BufferedOutputStream(output))"))); + assertTrue(source.matches(PatternMaker.make(": 132 */", "if (data == -7)"))); + assertTrue(source.matches(PatternMaker.make(": 133 */", "return 1;"))); + assertTrue(source.matches(PatternMaker.make(": 142 */", "return 2;"))); + assertTrue(source.matches(PatternMaker.make(": 144 */", "e.printStackTrace();"))); + assertTrue(source.matches(PatternMaker.make(": 150 */", "e.printStackTrace();"))); + assertTrue(source.matches(PatternMaker.make(": 152 */", "System.out.println(\"finally, before loop\");"))); + assertTrue(source.matches(PatternMaker.make(": 156 */", "System.out.println(\"finally, after loop\");"))); + assertTrue(source.matches(PatternMaker.make(": 159 */", "System.out.println(\"finally\");"))); + assertTrue(source.matches(PatternMaker.make(": 162 */", "return 3;"))); + + assertTrue(source.indexOf("/* 162: 162 */") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk180TryWithResources() throws Exception { + String internalClassName = "org/jd/core/test/TryWithResources"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.8.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1370,7 +1460,7 @@ public void testJdk180TryWithResources() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryWithResources"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1386,41 +1476,44 @@ public void testJdk180TryWithResources() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 12]", "try (FileInputStream input = new FileInputStream(path))"))); - - assertTrue(source.matches(PatternMaker.make(": 49]", "try (FileInputStream input = new FileInputStream(path))"))); - assertTrue(source.matches(PatternMaker.make(": 57]", "e.printStackTrace();"))); - assertTrue(source.matches(PatternMaker.make(": 59]", "System.out.println(\"finally\");"))); - - assertTrue(source.matches(PatternMaker.make(": 121]", "try(FileInputStream input = new FileInputStream(pathIn);"))); - assertTrue(source.matches(PatternMaker.make(": 122]", "BufferedInputStream bufferedInput = new BufferedInputStream(input);"))); - assertTrue(source.matches(PatternMaker.make(": 123]", "FileOutputStream output = new FileOutputStream(pathOut);"))); - assertTrue(source.matches(PatternMaker.make(": 124]", "BufferedOutputStream bufferedOutput = new BufferedOutputStream(output))"))); - assertTrue(source.matches(PatternMaker.make(": 132]", "if (data == -7)"))); - assertTrue(source.matches(PatternMaker.make(": 133]", "return 1;"))); - assertTrue(source.matches(PatternMaker.make(": 142]", "return 2;"))); - assertTrue(source.matches(PatternMaker.make(": 144]", "e.printStackTrace();"))); - assertTrue(source.matches(PatternMaker.make(": 150]", "e.printStackTrace();"))); - assertTrue(source.matches(PatternMaker.make(": 152]", "System.out.println(\"finally, before loop\");"))); - assertTrue(source.matches(PatternMaker.make(": 156]", "System.out.println(\"finally, after loop\");"))); - assertTrue(source.matches(PatternMaker.make(": 159]", "System.out.println(\"finally\");"))); - assertTrue(source.matches(PatternMaker.make(": 162]", "return 3;"))); - - assertTrue(source.indexOf("[ 162: 162]") != -1); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 12 */", "try (FileInputStream input = new FileInputStream(path))"))); + + assertTrue(source.matches(PatternMaker.make(": 49 */", "try (FileInputStream input = new FileInputStream(path))"))); + assertTrue(source.matches(PatternMaker.make(": 57 */", "e.printStackTrace();"))); + assertTrue(source.matches(PatternMaker.make(": 59 */", "System.out.println(\"finally\");"))); + + assertTrue(source.matches(PatternMaker.make(": 121 */", "try(FileInputStream input = new FileInputStream(pathIn);"))); + assertTrue(source.matches(PatternMaker.make(": 122 */", "BufferedInputStream bufferedInput = new BufferedInputStream(input);"))); + assertTrue(source.matches(PatternMaker.make(": 123 */", "FileOutputStream output = new FileOutputStream(pathOut);"))); + assertTrue(source.matches(PatternMaker.make(": 124 */", "BufferedOutputStream bufferedOutput = new BufferedOutputStream(output))"))); + assertTrue(source.matches(PatternMaker.make(": 132 */", "if (data == -7)"))); + assertTrue(source.matches(PatternMaker.make(": 133 */", "return 1;"))); + assertTrue(source.matches(PatternMaker.make(": 142 */", "return 2;"))); + assertTrue(source.matches(PatternMaker.make(": 144 */", "e.printStackTrace();"))); + assertTrue(source.matches(PatternMaker.make(": 150 */", "e.printStackTrace();"))); + assertTrue(source.matches(PatternMaker.make(": 152 */", "System.out.println(\"finally, before loop\");"))); + assertTrue(source.matches(PatternMaker.make(": 156 */", "System.out.println(\"finally, after loop\");"))); + assertTrue(source.matches(PatternMaker.make(": 159 */", "System.out.println(\"finally\");"))); + assertTrue(source.matches(PatternMaker.make(": 162 */", "return 3;"))); + + assertTrue(source.indexOf("/* 162: 162 */") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170Synchronised() throws Exception { + String internalClassName = "org/jd/core/test/Synchronized"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Synchronized"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -1435,32 +1528,35 @@ public void testJdk170Synchronised() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 11]", "synchronized (paramStringBuilder)"))); - assertTrue(source.matches(PatternMaker.make(": 13]", "inSynchronized();"))); - assertTrue(source.matches(PatternMaker.make(": 15]", "return 2;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 11 */", "synchronized (paramStringBuilder)"))); + assertTrue(source.matches(PatternMaker.make(": 13 */", "inSynchronized();"))); + assertTrue(source.matches(PatternMaker.make(": 15 */", "return 2;"))); - assertTrue(source.matches(PatternMaker.make(": 20]", "synchronized (paramStringBuilder)"))); - assertTrue(source.matches(PatternMaker.make(": 22]", "inSynchronized();"))); - assertTrue(source.matches(PatternMaker.make(": 23]", "return 2;"))); + assertTrue(source.matches(PatternMaker.make(": 20 */", "synchronized (paramStringBuilder)"))); + assertTrue(source.matches(PatternMaker.make(": 22 */", "inSynchronized();"))); + assertTrue(source.matches(PatternMaker.make(": 23 */", "return 2;"))); - assertTrue(source.matches(PatternMaker.make(": 29]", "synchronized (paramStringBuilder)"))); - assertTrue(source.matches(PatternMaker.make(": 31]", "inSynchronized();"))); - assertTrue(source.matches(PatternMaker.make(": 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make(": 29 */", "synchronized (paramStringBuilder)"))); + assertTrue(source.matches(PatternMaker.make(": 31 */", "inSynchronized();"))); + assertTrue(source.matches(PatternMaker.make(": 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make(": 73]", "synchronized (paramStringBuilder)"))); - assertTrue(source.matches(PatternMaker.make(": 75]", "inSynchronized();"))); - assertTrue(source.matches(PatternMaker.make(": 76]", "throw new RuntimeException();"))); + assertTrue(source.matches(PatternMaker.make(": 73 */", "synchronized (paramStringBuilder)"))); + assertTrue(source.matches(PatternMaker.make(": 75 */", "inSynchronized();"))); + assertTrue(source.matches(PatternMaker.make(": 76 */", "throw new RuntimeException();"))); - assertTrue(source.matches(PatternMaker.make(": 95]", "synchronized (s)"))); - assertTrue(source.matches(PatternMaker.make(": 97]", "return subContentEquals(s);"))); + assertTrue(source.matches(PatternMaker.make(": 95 */", "synchronized (s)"))); + assertTrue(source.matches(PatternMaker.make(": 97 */", "return subContentEquals(s);"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testEclipseJavaCompiler321TryCatchFinally() throws Exception { + String internalClassName = "org/jd/core/test/TryCatchFinally"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-eclipse-java-compiler-3.2.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1468,7 +1564,7 @@ public void testEclipseJavaCompiler321TryCatchFinally() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryCatchFinally"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1484,13 +1580,14 @@ public void testEclipseJavaCompiler321TryCatchFinally() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("catch (RuntimeException runtimeexception)") != -1); - assertTrue(source.matches(PatternMaker.make("[ 45: 45]", "inCatch1();"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make("/* 45: 45 */", "inCatch1();"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make(": 166]", "return System.currentTimeMillis();"))); + assertTrue(source.matches(PatternMaker.make(": 166 */", "return System.currentTimeMillis();"))); - assertTrue(source.indexOf("[ 888: 888]") != -1); + assertTrue(source.indexOf("/* 888: 888 */") != -1); assertTrue(source.indexOf("long l = System.currentTimeMillis(); return l;") == -1); assertTrue(source.indexOf("catch (RuntimeException null)") == -1); @@ -1499,12 +1596,14 @@ public void testEclipseJavaCompiler321TryCatchFinally() throws Exception { assertTrue(source.indexOf("Exception exception8;") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testEclipseJavaCompiler370TryCatchFinally() throws Exception { + String internalClassName = "org/jd/core/test/TryCatchFinally"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-eclipse-java-compiler-3.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1512,7 +1611,7 @@ public void testEclipseJavaCompiler370TryCatchFinally() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryCatchFinally"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1528,27 +1627,28 @@ public void testEclipseJavaCompiler370TryCatchFinally() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("catch (RuntimeException runtimeException)") != -1); - assertTrue(source.matches(PatternMaker.make("[ 48: 48]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make("/* 48: 48 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make(": 166]", "return System.currentTimeMillis();"))); + assertTrue(source.matches(PatternMaker.make(": 166 */", "return System.currentTimeMillis();"))); - assertTrue(source.matches(PatternMaker.make(": 393]", "inCatch1();"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make(": 397]", "inCatch3();"))); - assertTrue(source.matches(PatternMaker.make(": 399]", "inFinally();"))); - assertTrue(source.indexOf("[ 400: 0] inFinally();") == -1); + assertTrue(source.matches(PatternMaker.make(": 393 */", "inCatch1();"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make(": 397 */", "inCatch3();"))); + assertTrue(source.matches(PatternMaker.make(": 399 */", "inFinally();"))); + assertTrue(source.indexOf("/* 400: 0] inFinally();") == -1); - assertTrue(source.matches(PatternMaker.make(": 424]", "inTry();"))); - assertTrue(source.matches(PatternMaker.make(": 427]", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 431]", "inTryA();"))); - assertTrue(source.matches(PatternMaker.make(": 434]", "inFinallyA();"))); - assertTrue(source.matches(PatternMaker.make(": 439]", "inTryC();"))); - assertTrue(source.matches(PatternMaker.make(": 442]", "inFinallyC();"))); - assertTrue(source.matches(PatternMaker.make(": 445]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 424 */", "inTry();"))); + assertTrue(source.matches(PatternMaker.make(": 427 */", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 431 */", "inTryA();"))); + assertTrue(source.matches(PatternMaker.make(": 434 */", "inFinallyA();"))); + assertTrue(source.matches(PatternMaker.make(": 439 */", "inTryC();"))); + assertTrue(source.matches(PatternMaker.make(": 442 */", "inFinallyC();"))); + assertTrue(source.matches(PatternMaker.make(": 445 */", "inFinally();"))); - assertTrue(source.indexOf("[ 888: 888]") != -1); + assertTrue(source.indexOf("/* 888: 888 */") != -1); assertTrue(source.indexOf("long l = System.currentTimeMillis(); return l;") == -1); assertTrue(source.indexOf("catch (RuntimeException null)") == -1); @@ -1557,12 +1657,14 @@ public void testEclipseJavaCompiler370TryCatchFinally() throws Exception { assertTrue(source.indexOf("Exception exception8;") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testEclipseJavaCompiler3130TryCatchFinally() throws Exception { + String internalClassName = "org/jd/core/test/TryCatchFinally"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-eclipse-java-compiler-3.13.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1570,7 +1672,7 @@ public void testEclipseJavaCompiler3130TryCatchFinally() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryCatchFinally"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1586,26 +1688,27 @@ public void testEclipseJavaCompiler3130TryCatchFinally() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("catch (RuntimeException runtimeException)") != -1); - assertTrue(source.matches(PatternMaker.make("[ 48: 48]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make("/* 48: 48 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make(": 166]", "return System.currentTimeMillis();"))); + assertTrue(source.matches(PatternMaker.make(": 166 */", "return System.currentTimeMillis();"))); - assertTrue(source.matches(PatternMaker.make(": 393]", "inCatch1();"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make(": 397]", "inCatch3();"))); - assertTrue(source.matches(PatternMaker.make(": 399]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 393 */", "inCatch1();"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make(": 397 */", "inCatch3();"))); + assertTrue(source.matches(PatternMaker.make(": 399 */", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 424]", "inTry();"))); - assertTrue(source.matches(PatternMaker.make(": 427]", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 431]", "inTryA();"))); - assertTrue(source.matches(PatternMaker.make(": 434]", "inFinallyA();"))); - assertTrue(source.matches(PatternMaker.make(": 439]", "inTryC();"))); - assertTrue(source.matches(PatternMaker.make(": 442]", "inFinallyC();"))); - assertTrue(source.matches(PatternMaker.make(": 445]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 424 */", "inTry();"))); + assertTrue(source.matches(PatternMaker.make(": 427 */", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 431 */", "inTryA();"))); + assertTrue(source.matches(PatternMaker.make(": 434 */", "inFinallyA();"))); + assertTrue(source.matches(PatternMaker.make(": 439 */", "inTryC();"))); + assertTrue(source.matches(PatternMaker.make(": 442 */", "inFinallyC();"))); + assertTrue(source.matches(PatternMaker.make(": 445 */", "inFinally();"))); - assertTrue(source.indexOf("[ 888: 888]") != -1); + assertTrue(source.indexOf("/* 888: 888 */") != -1); assertTrue(source.indexOf("long l = System.currentTimeMillis(); return l;") == -1); assertTrue(source.indexOf("catch (RuntimeException null)") == -1); @@ -1614,12 +1717,14 @@ public void testEclipseJavaCompiler3130TryCatchFinally() throws Exception { assertTrue(source.indexOf("Exception exception8;") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.5", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk118TryCatchFinally() throws Exception { + String internalClassName = "org/jd/core/test/TryCatchFinally"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.1.8.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1627,7 +1732,7 @@ public void testJdk118TryCatchFinally() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryCatchFinally"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1643,26 +1748,27 @@ public void testJdk118TryCatchFinally() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("catch (RuntimeException runtimeexception)") != -1); - assertTrue(source.matches(PatternMaker.make("[ 48: 48]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make("/* 48: 48 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make(": 166]", "return System.currentTimeMillis();"))); + assertTrue(source.matches(PatternMaker.make(": 166 */", "return System.currentTimeMillis();"))); - assertTrue(source.matches(PatternMaker.make(": 393]", "inCatch1();"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make(": 397]", "inCatch3();"))); - assertTrue(source.matches(PatternMaker.make(": 399]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 393 */", "inCatch1();"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make(": 397 */", "inCatch3();"))); + assertTrue(source.matches(PatternMaker.make(": 399 */", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 424]", "inTry();"))); - assertTrue(source.matches(PatternMaker.make(": 427]", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 431]", "inTryA();"))); - assertTrue(source.matches(PatternMaker.make(": 434]", "inFinallyA();"))); - assertTrue(source.matches(PatternMaker.make(": 439]", "inTryC();"))); - assertTrue(source.matches(PatternMaker.make(": 442]", "inFinallyC();"))); - assertTrue(source.matches(PatternMaker.make(": 445]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 424 */", "inTry();"))); + assertTrue(source.matches(PatternMaker.make(": 427 */", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 431 */", "inTryA();"))); + assertTrue(source.matches(PatternMaker.make(": 434 */", "inFinallyA();"))); + assertTrue(source.matches(PatternMaker.make(": 439 */", "inTryC();"))); + assertTrue(source.matches(PatternMaker.make(": 442 */", "inFinallyC();"))); + assertTrue(source.matches(PatternMaker.make(": 445 */", "inFinally();"))); - assertTrue(source.indexOf("[ 902: 902]") != -1); + assertTrue(source.indexOf("/* 902: 902 */") != -1); assertTrue(source.indexOf("long l = System.currentTimeMillis(); return l;") == -1); assertTrue(source.indexOf("catch (RuntimeException null)") == -1); @@ -1671,12 +1777,14 @@ public void testJdk118TryCatchFinally() throws Exception { assertTrue(source.indexOf("Exception exception8;") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.3", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk131TryCatchFinally() throws Exception { + String internalClassName = "org/jd/core/test/TryCatchFinally"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.3.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1684,7 +1792,7 @@ public void testJdk131TryCatchFinally() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryCatchFinally"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1700,22 +1808,23 @@ public void testJdk131TryCatchFinally() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("catch (RuntimeException runtimeexception)") != -1); - assertTrue(source.matches(PatternMaker.make("[ 48: 48]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make("/* 48: 48 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make(": 166]", "return System.currentTimeMillis();"))); + assertTrue(source.matches(PatternMaker.make(": 166 */", "return System.currentTimeMillis();"))); - assertTrue(source.matches(PatternMaker.make(": 393]", "inCatch1();"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make(": 397]", "inCatch3();"))); - assertTrue(source.matches(PatternMaker.make(": 399]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 393 */", "inCatch1();"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make(": 397 */", "inCatch3();"))); + assertTrue(source.matches(PatternMaker.make(": 399 */", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 424]", "inTry();"))); - assertTrue(source.matches(PatternMaker.make(": 427]", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 445]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 424 */", "inTry();"))); + assertTrue(source.matches(PatternMaker.make(": 427 */", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 445 */", "inFinally();"))); - assertTrue(source.indexOf("[ 902: 902]") != -1); + assertTrue(source.indexOf("/* 902: 902 */") != -1); assertTrue(source.indexOf("long l = System.currentTimeMillis(); return l;") == -1); assertTrue(source.indexOf("catch (RuntimeException null)") == -1); @@ -1724,12 +1833,14 @@ public void testJdk131TryCatchFinally() throws Exception { assertTrue(source.indexOf("Exception exception8;") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.3", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170TryCatchFinally() throws Exception { + String internalClassName = "org/jd/core/test/TryCatchFinally"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1737,7 +1848,7 @@ public void testJdk170TryCatchFinally() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/TryCatchFinally"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1753,29 +1864,30 @@ public void testJdk170TryCatchFinally() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("catch (RuntimeException runtimeexception)") != -1); - assertTrue(source.matches(PatternMaker.make("[ 48: 48]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make("[ 60: 0]", "return;"))); + assertTrue(source.matches(PatternMaker.make("/* 48: 48 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make("/* 60: 0 */", "return;"))); - assertTrue(source.matches(PatternMaker.make(": 166]", "return System.currentTimeMillis();"))); + assertTrue(source.matches(PatternMaker.make(": 166 */", "return System.currentTimeMillis();"))); - assertTrue(source.matches(PatternMaker.make(": 192]", "catch (RuntimeException e) {}"))); - assertTrue(source.matches(PatternMaker.make("[ 204: 0]", "finally {}"))); + assertTrue(source.matches(PatternMaker.make(": 192 */", "catch (RuntimeException e) {}"))); + assertTrue(source.matches(PatternMaker.make("/* 204: 0 */", "finally {}"))); - assertTrue(source.matches(PatternMaker.make(": 393]", "inCatch1();"))); - assertTrue(source.matches(PatternMaker.make(": 395]", "inCatch2();"))); - assertTrue(source.matches(PatternMaker.make(": 397]", "inCatch3();"))); - assertTrue(source.matches(PatternMaker.make(": 399]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 393 */", "inCatch1();"))); + assertTrue(source.matches(PatternMaker.make(": 395 */", "inCatch2();"))); + assertTrue(source.matches(PatternMaker.make(": 397 */", "inCatch3();"))); + assertTrue(source.matches(PatternMaker.make(": 399 */", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 424]", "inTry();"))); - assertTrue(source.matches(PatternMaker.make(": 427]", "inFinally();"))); - assertTrue(source.matches(PatternMaker.make(": 431]", "inTryA();"))); - assertTrue(source.matches(PatternMaker.make(": 434]", "inFinallyA();"))); - assertTrue(source.matches(PatternMaker.make(": 439]", "inTryC();"))); - assertTrue(source.matches(PatternMaker.make(": 442]", "inFinallyC();"))); - assertTrue(source.matches(PatternMaker.make(": 445]", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 424 */", "inTry();"))); + assertTrue(source.matches(PatternMaker.make(": 427 */", "inFinally();"))); + assertTrue(source.matches(PatternMaker.make(": 431 */", "inTryA();"))); + assertTrue(source.matches(PatternMaker.make(": 434 */", "inFinallyA();"))); + assertTrue(source.matches(PatternMaker.make(": 439 */", "inTryC();"))); + assertTrue(source.matches(PatternMaker.make(": 442 */", "inFinallyC();"))); + assertTrue(source.matches(PatternMaker.make(": 445 */", "inFinally();"))); - assertTrue(source.indexOf("[ 902: 902]") != -1); + assertTrue(source.indexOf("/* 902: 902 */") != -1); assertTrue(source.indexOf("long l = System.currentTimeMillis(); return l;") == -1); assertTrue(source.indexOf("catch (RuntimeException null)") == -1); @@ -1784,19 +1896,21 @@ public void testJdk170TryCatchFinally() throws Exception { assertTrue(source.indexOf("Exception exception8;") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170AnnotatedClass() throws Exception { + String internalClassName = "org/jd/core/test/AnnotatedClass"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); // PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/AnnotatedClass"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -1811,6 +1925,7 @@ public void testJdk170AnnotatedClass() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("@Quality(Quality.Level.HIGH)") != -1); assertTrue(source.indexOf("@Author(value = @Name(salutation = \"Mr\", value = \"Donald\", last = \"Duck\"), contributors = {@Name(\"Huey\"), @Name(\"Dewey\"), @Name(\"Louie\")})") != -1); assertTrue(source.indexOf("@Value(z = true)") != -1); @@ -1826,12 +1941,18 @@ public void testJdk170AnnotatedClass() throws Exception { assertTrue(source.indexOf("public void ping(@Deprecated Writer writer, @Deprecated @Value(str = \"localhost\") String host, long timeout)") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source), + new JavaSourceFileObject("org/jd/core/test/annotation/Author", "package org.jd.core.test.annotation; public @interface Author {Name value(); Name[] contributors() default {};}"), + new JavaSourceFileObject("org/jd/core/test/annotation/Name", "package org.jd.core.test.annotation; public @interface Name {String salutation() default \"\"; String value(); String last() default \"\";}"), + new JavaSourceFileObject("org/jd/core/test/annotation/Quality", "package org.jd.core.test.annotation; public @interface Quality {enum Level {LOW,MIDDLE,HIGH}; Level value();}"), + new JavaSourceFileObject("org/jd/core/test/annotation/Value", "package org.jd.core.test.annotation; public @interface Value {boolean z() default true; byte b() default 1; short s() default 1; int i() default 1; long l() default 1L; float f() default 1.0F; double d() default 1.0D; String str() default \"str\"; Class clazz() default Object.class;}"))); } @Test public void testJdk170AnonymousClass() throws Exception { + String internalClassName = "org/jd/core/test/AnonymousClass"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); // PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1839,7 +1960,7 @@ public void testJdk170AnonymousClass() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/AnonymousClass"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1855,36 +1976,40 @@ public void testJdk170AnonymousClass() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 21]", "Object obj = new Object()"))); - assertTrue(source.matches(PatternMaker.make(": 23]", "return \"toString() return \" + super.toString() + \" at \" + AnonymousClass.this.time;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 21 */", "Object obj = new Object()"))); + assertTrue(source.matches(PatternMaker.make(": 23 */", "return \"toString() return \" + super.toString() + \" at \" + AnonymousClass.this.time;"))); - assertTrue(source.matches(PatternMaker.make(": 39]", "Enumeration e = new Enumeration()"))); - assertTrue(source.matches(PatternMaker.make(": 40]", "Iterator i = AnonymousClass.this.list.iterator();"))); - assertTrue(source.matches(PatternMaker.make(": 44]", "return (this.i.hasNext() && s1 == s2 && i1 > l1);"))); + assertTrue(source.matches(PatternMaker.make(": 39 */", "Enumeration e = new Enumeration()"))); + assertTrue(source.matches(PatternMaker.make(": 40 */", "Iterator i = AnonymousClass.this.list.iterator();"))); + assertTrue(source.matches(PatternMaker.make(": 44 */", "return (this.i.hasNext() && s1 == s2 && i1 > l1);"))); - assertTrue(source.matches(PatternMaker.make(": 61]", "final int i = s1.length();"))); - assertTrue(source.matches(PatternMaker.make(": 63]", "System.out.println(\"2\" + new StringWrapper(123456L)"))); - assertTrue(source.matches(PatternMaker.make(": 67]", "if (s1 == s2 && i == 5)"))); - assertTrue(source.matches(PatternMaker.make("[ 72: 0]", "} + \"3\");"))); + assertTrue(source.matches(PatternMaker.make(": 61 */", "final int i = s1.length();"))); + assertTrue(source.matches(PatternMaker.make(": 63 */", "System.out.println(\"2\" + new StringWrapper(123456L)"))); + assertTrue(source.matches(PatternMaker.make(": 67 */", "if (s1 == s2 && i == 5)"))); + assertTrue(source.matches(PatternMaker.make("/* 72: 0 */", "} + \"3\");"))); - assertTrue(source.matches(PatternMaker.make(": 81]", "final Object abc = \"abc\";"))); - assertTrue(source.matches(PatternMaker.make(": 82]", "final Object def = \"def\";"))); - assertTrue(source.matches(PatternMaker.make(": 84]", "Serializable serializable = new Serializable()"))); - assertTrue(source.matches(PatternMaker.make(": 90]", "Serializable serializable = new Serializable()"))); - assertTrue(source.matches(PatternMaker.make(": 96]", "return (abc.equals(obj) || def.equals(obj) || ghi.equals(obj) || jkl.equals(obj));"))); - assertTrue(source.matches(PatternMaker.make(": 100]", "return (abc.equals(obj) || def.equals(obj));"))); - assertTrue(source.matches(PatternMaker.make("[ 102: 0]", "};"))); + assertTrue(source.matches(PatternMaker.make(": 81 */", "final Object abc = \"abc\";"))); + assertTrue(source.matches(PatternMaker.make(": 82 */", "final Object def = \"def\";"))); + assertTrue(source.matches(PatternMaker.make(": 84 */", "Serializable serializable = new Serializable()"))); + assertTrue(source.matches(PatternMaker.make(": 90 */", "Serializable serializable = new Serializable()"))); + assertTrue(source.matches(PatternMaker.make(": 96 */", "return (abc.equals(obj) || def.equals(obj) || ghi.equals(obj) || jkl.equals(obj));"))); + assertTrue(source.matches(PatternMaker.make(": 100 */", "return (abc.equals(obj) || def.equals(obj));"))); + assertTrue(source.matches(PatternMaker.make("/* 102: 0 */", "};"))); - assertTrue(source.matches(PatternMaker.make(": 111]", "this.l = l & 0x80L;"))); + assertTrue(source.matches(PatternMaker.make(": 111 */", "this.l = l & 0x80L;"))); assertTrue(source.indexOf("} ;") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source), + new JavaSourceFileObject("org/jd/core/test/annotation/Name", "package org.jd.core.test.annotation; public @interface Name {String value();}"))); } @Test public void testJdk170GenericClass() throws Exception { + String internalClassName = "org/jd/core/test/GenericClass"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -1892,7 +2017,7 @@ public void testJdk170GenericClass() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/GenericClass"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -1908,37 +2033,41 @@ public void testJdk170GenericClass() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.indexOf("public class GenericClass, T7 extends Map, T8 extends Map, T9 extends T8>") != -1); assertTrue(source.indexOf("extends ArrayList") != -1); assertTrue(source.indexOf("implements Serializable, Comparable") != -1); - assertTrue(source.matches(PatternMaker.make("[ 26: 26]", "public List> list1 = new ArrayList();"))); + assertTrue(source.matches(PatternMaker.make("/* 26: 26 */", "public List> list1 = new ArrayList();"))); assertTrue(source.indexOf("public List> list2;") != -1); - assertTrue(source.matches(PatternMaker.make("[ 31: 31]", "list2 = new ArrayList();"))); + assertTrue(source.matches(PatternMaker.make("/* 31: 31 */", "list2 = new ArrayList();"))); assertTrue(source.indexOf("public void fromArrayToCollection(T[] a, Collection c)") != -1); assertTrue(source.indexOf("public void copy(List dest, List src)") != -1); assertTrue(source.indexOf("public List copy2(List dest, List src) throws InvalidParameterException, ClassCastException") != -1); assertTrue(source.indexOf("public List print(List list) throws T2, InvalidParameterException") != -1); - assertTrue(source.matches(PatternMaker.make(": 100]", "return (T1)call(0);"))); - assertTrue(source.matches(PatternMaker.make(": 104]", "return (T1)this;"))); + assertTrue(source.matches(PatternMaker.make(": 100 */", "return (T1)call(0);"))); + assertTrue(source.matches(PatternMaker.make(": 104 */", "return (T1)this;"))); - assertTrue(source.indexOf("[ 104: 104]") != -1); + assertTrue(source.indexOf("/* 104: 104 */") != -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source), + new JavaSourceFileObject("org/jd/core/test/AnnotatedClass", "package org.jd.core.test; public class AnnotatedClass {}"))); } @Test public void testJdk170AnnotationAuthor() throws Exception { + String internalClassName = "org/jd/core/test/annotation/Author"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/annotation/Author"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -1953,24 +2082,28 @@ public void testJdk170AnnotationAuthor() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 3: 0]", "public @interface Author"))); - assertTrue(source.matches(PatternMaker.make("[ 4: 0]", "Name value();"))); - assertTrue(source.matches(PatternMaker.make("[ 6: 0]", "Name[] contributors() default {};"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 3: 0 */", "public @interface Author"))); + assertTrue(source.matches(PatternMaker.make("/* 4: 0 */", "Name value();"))); + assertTrue(source.matches(PatternMaker.make("/* 6: 0 */", "Name[] contributors() default {};"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source), + new JavaSourceFileObject("org/jd/core/test/annotation/Name", "package org.jd.core.test.annotation; public @interface Name {String value();}"))); } @Test public void testJdk170AnnotationValue() throws Exception { + String internalClassName = "org/jd/core/test/annotation/Value"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/annotation/Value"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -1985,21 +2118,24 @@ public void testJdk170AnnotationValue() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make("[ 8: 0]", "@Retention(RetentionPolicy.RUNTIME)"))); - assertTrue(source.matches(PatternMaker.make("[ 9: 0]", "@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})"))); - assertTrue(source.matches(PatternMaker.make("[ 10: 0]", "public @interface Value {"))); - assertTrue(source.matches(PatternMaker.make("[ 11: 0]", "boolean z() default true;"))); - assertTrue(source.matches(PatternMaker.make("[ 13: 0]", "byte b() default 1;"))); - assertTrue(source.matches(PatternMaker.make("[ 25: 0]", "String str() default \"str\";"))); - assertTrue(source.matches(PatternMaker.make("[ 27: 0]", "Class clazz() default Object.class;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make("/* 8: 0 */", "@Retention(RetentionPolicy.RUNTIME)"))); + assertTrue(source.matches(PatternMaker.make("/* 9: 0 */", "@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})"))); + assertTrue(source.matches(PatternMaker.make("/* 10: 0 */", "public @interface Value {"))); + assertTrue(source.matches(PatternMaker.make("/* 11: 0 */", "boolean z() default true;"))); + assertTrue(source.matches(PatternMaker.make("/* 13: 0 */", "byte b() default 1;"))); + assertTrue(source.matches(PatternMaker.make("/* 25: 0 */", "String str() default \"str\";"))); + assertTrue(source.matches(PatternMaker.make("/* 27: 0 */", "Class clazz() default Object.class;"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170OuterClass() throws Exception { + String internalClassName = "org/jd/core/test/OuterClass"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //ClassPathLoader loader = new ClassPathLoader(); @@ -2008,7 +2144,7 @@ public void testJdk170OuterClass() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/OuterClass"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2024,65 +2160,68 @@ public void testJdk170OuterClass() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 10]", "protected int outerField1 = 0;"))); - assertTrue(source.matches(PatternMaker.make(": 11]", "protected String[] outerField2 = { \"0\" };"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 10 */", "protected int outerField1 = 0;"))); + assertTrue(source.matches(PatternMaker.make(": 11 */", "protected String[] outerField2 = { \"0\" };"))); assertTrue(source.indexOf("final int localVariable1 = param1;") != -1); assertTrue(source.indexOf("final String[] localVariable2 = param2;") != -1); - assertTrue(source.matches(PatternMaker.make(": 21]", "InnerClass innerClass = new InnerClass(param1, param2);"))); - assertTrue(source.matches(PatternMaker.make(": 22]", "innerClass.innerMethod(localVariable1, localVariable2);"))); - assertTrue(source.matches(PatternMaker.make(": 24]", "StaticInnerClass staticInnerClass = new StaticInnerClass(param1, param2);"))); - assertTrue(source.matches(PatternMaker.make(": 25]", "staticInnerClass.innerMethod(localVariable1, localVariable2);"))); + assertTrue(source.matches(PatternMaker.make(": 21 */", "InnerClass innerClass = new InnerClass(param1, param2);"))); + assertTrue(source.matches(PatternMaker.make(": 22 */", "innerClass.innerMethod(localVariable1, localVariable2);"))); + assertTrue(source.matches(PatternMaker.make(": 24 */", "StaticInnerClass staticInnerClass = new StaticInnerClass(param1, param2);"))); + assertTrue(source.matches(PatternMaker.make(": 25 */", "staticInnerClass.innerMethod(localVariable1, localVariable2);"))); - assertTrue(source.matches(PatternMaker.make(": 27]", "InnerClass anonymousClass = new InnerClass(param1, param2)"))); + assertTrue(source.matches(PatternMaker.make(": 27 */", "InnerClass anonymousClass = new InnerClass(param1, param2)"))); assertTrue(source.indexOf("public void innerMethod(int param1, String... param2)") != -1); - assertTrue(source.matches(PatternMaker.make(": 30]", "this.innerField2 = param2;"))); - assertTrue(source.matches(PatternMaker.make(": 32]", "OuterClass.this.outerField1 = param1;"))); - assertTrue(source.matches(PatternMaker.make(": 33]", "OuterClass.this.outerField2 = param2;"))); - assertTrue(source.matches(PatternMaker.make(": 35]", "this.innerField1 = localVariable1;"))); - assertTrue(source.matches(PatternMaker.make(": 36]", "this.innerField2 = localVariable2;"))); - - assertTrue(source.matches(PatternMaker.make(": 39]", "anonymousClass.innerMethod(localVariable1, localVariable2);"))); - - assertTrue(source.matches(PatternMaker.make(": 41]", "StaticInnerClass staticAnonymousClass = new StaticInnerClass(param1, param2)"))); - assertTrue(source.matches(PatternMaker.make(": 44]", "this.innerField2 = param2;"))); - assertTrue(source.matches(PatternMaker.make(": 46]", "OuterClass.this.outerField1 = param1;"))); - assertTrue(source.matches(PatternMaker.make(": 47]", "OuterClass.this.outerField2 = param2;"))); - assertTrue(source.matches(PatternMaker.make(": 49]", "this.innerField1 = localVariable1;"))); - assertTrue(source.matches(PatternMaker.make(": 50]", "this.innerField2 = localVariable2;"))); - - assertTrue(source.matches(PatternMaker.make(": 53]", "staticAnonymousClass.innerMethod(localVariable1, localVariable2);"))); - - assertTrue(source.matches(PatternMaker.make(": 55]", "InnerEnum.A.innerMethod(localVariable1, localVariable2);"))); - - assertTrue(source.matches(PatternMaker.make("[ 56: 0]", "class LocalClass"))); - assertTrue(source.matches(PatternMaker.make(": 58]", "protected int innerField1 = 0;"))); - assertTrue(source.matches(PatternMaker.make(": 59]", "protected String[] innerField2 = { \"0\" } ;"))); - assertTrue(source.matches(PatternMaker.make(": 69]", "this.innerField1 = param1;"))); - assertTrue(source.matches(PatternMaker.make(": 70]", "this.innerField2 = param2;"))); - assertTrue(source.matches(PatternMaker.make(": 72]", "OuterClass.this.outerField1 = param1;"))); - assertTrue(source.matches(PatternMaker.make(": 73]", "OuterClass.this.outerField2 = param2;"))); - assertTrue(source.matches(PatternMaker.make(": 75]", "this.innerField1 = localVariable1;"))); - assertTrue(source.matches(PatternMaker.make(": 76]", "this.innerField2 = localVariable2;"))); - assertTrue(source.matches(PatternMaker.make(": 94]", "LocalClass localClass = new LocalClass(param1, param2);"))); - assertTrue(source.matches(PatternMaker.make(": 95]", "localClass.localMethod(localVariable1, localVariable2);"))); - - assertTrue(source.matches(PatternMaker.make(": 114]", "this(param1, param2);"))); - assertTrue(source.matches(PatternMaker.make(": 144]", "this(param1, param2);"))); - - assertTrue(source.matches(PatternMaker.make(": 158]", "A,", "B,", "C;"))); - assertTrue(source.indexOf("[ 182: 182]") != -1); + assertTrue(source.matches(PatternMaker.make(": 30 */", "this.innerField2 = param2;"))); + assertTrue(source.matches(PatternMaker.make(": 32 */", "OuterClass.this.outerField1 = param1;"))); + assertTrue(source.matches(PatternMaker.make(": 33 */", "OuterClass.this.outerField2 = param2;"))); + assertTrue(source.matches(PatternMaker.make(": 35 */", "this.innerField1 = localVariable1;"))); + assertTrue(source.matches(PatternMaker.make(": 36 */", "this.innerField2 = localVariable2;"))); + + assertTrue(source.matches(PatternMaker.make(": 39 */", "anonymousClass.innerMethod(localVariable1, localVariable2);"))); + + assertTrue(source.matches(PatternMaker.make(": 41 */", "StaticInnerClass staticAnonymousClass = new StaticInnerClass(param1, param2)"))); + assertTrue(source.matches(PatternMaker.make(": 44 */", "this.innerField2 = param2;"))); + assertTrue(source.matches(PatternMaker.make(": 46 */", "OuterClass.this.outerField1 = param1;"))); + assertTrue(source.matches(PatternMaker.make(": 47 */", "OuterClass.this.outerField2 = param2;"))); + assertTrue(source.matches(PatternMaker.make(": 49 */", "this.innerField1 = localVariable1;"))); + assertTrue(source.matches(PatternMaker.make(": 50 */", "this.innerField2 = localVariable2;"))); + + assertTrue(source.matches(PatternMaker.make(": 53 */", "staticAnonymousClass.innerMethod(localVariable1, localVariable2);"))); + + assertTrue(source.matches(PatternMaker.make(": 55 */", "InnerEnum.A.innerMethod(localVariable1, localVariable2);"))); + + assertTrue(source.matches(PatternMaker.make("/* 56: 0 */", "class LocalClass"))); + assertTrue(source.matches(PatternMaker.make(": 58 */", "protected int innerField1 = 0;"))); + assertTrue(source.matches(PatternMaker.make(": 59 */", "protected String[] innerField2 = { \"0\" } ;"))); + assertTrue(source.matches(PatternMaker.make(": 69 */", "this.innerField1 = param1;"))); + assertTrue(source.matches(PatternMaker.make(": 70 */", "this.innerField2 = param2;"))); + assertTrue(source.matches(PatternMaker.make(": 72 */", "OuterClass.this.outerField1 = param1;"))); + assertTrue(source.matches(PatternMaker.make(": 73 */", "OuterClass.this.outerField2 = param2;"))); + assertTrue(source.matches(PatternMaker.make(": 75 */", "this.innerField1 = localVariable1;"))); + assertTrue(source.matches(PatternMaker.make(": 76 */", "this.innerField2 = localVariable2;"))); + assertTrue(source.matches(PatternMaker.make(": 94 */", "LocalClass localClass = new LocalClass(param1, param2);"))); + assertTrue(source.matches(PatternMaker.make(": 95 */", "localClass.localMethod(localVariable1, localVariable2);"))); + + assertTrue(source.matches(PatternMaker.make(": 114 */", "this(param1, param2);"))); + assertTrue(source.matches(PatternMaker.make(": 144 */", "this(param1, param2);"))); + + assertTrue(source.matches(PatternMaker.make(": 158 */", "A,", "B,", "C;"))); + assertTrue(source.indexOf("/* 182: 182 */") != -1); assertTrue(source.matches(PatternMaker.make("public class InnerInnerClass", "{", "}"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk170Enum() throws Exception { + String internalClassName = "org/jd/core/test/Enum"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.7.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -2090,7 +2229,7 @@ public void testJdk170Enum() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Enum"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2106,29 +2245,32 @@ public void testJdk170Enum() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 5]", "SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 5 */", "SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;"))); - assertTrue(source.matches(PatternMaker.make(": 9]", "MERCURY(3.303E23D, 2439700.0D),"))); - assertTrue(source.matches(PatternMaker.make(": 17]", "URANUS(8.686E25D, 2.5559E7D),"))); - assertTrue(source.matches(PatternMaker.make(": 20]", "NEPTUNE(1.024E26D, 2.4746E7D);"))); + assertTrue(source.matches(PatternMaker.make(": 9 */", "MERCURY(3.303E23D, 2439700.0D),"))); + assertTrue(source.matches(PatternMaker.make(": 17 */", "URANUS(8.686E25D, 2.5559E7D),"))); + assertTrue(source.matches(PatternMaker.make(": 20 */", "NEPTUNE(1.024E26D, 2.4746E7D);"))); assertTrue(source.indexOf("this.mass = mass;") != -1); - assertTrue(source.matches(PatternMaker.make(": 27]", "this.radius = radius;"))); - assertTrue(source.matches(PatternMaker.make(": 37]", "return 6.673E-11D * this.mass / this.radius * this.radius;"))); - assertTrue(source.matches(PatternMaker.make(": 49]", "double earthWeight = Double.parseDouble(args[0]);"))); - assertTrue(source.matches(PatternMaker.make(": 50]", "double mass = earthWeight / EARTH.surfaceGravity();"))); - assertTrue(source.matches(PatternMaker.make(": 51]", "for (Planet p : values()) {"))); - assertTrue(source.matches(PatternMaker.make(": 52]", "System.out.printf(\"Your weight on %s is %f%n\", new Object[]", "{ p, Double.valueOf(p.surfaceWeight(mass)) } );"))); + assertTrue(source.matches(PatternMaker.make(": 27 */", "this.radius = radius;"))); + assertTrue(source.matches(PatternMaker.make(": 37 */", "return 6.673E-11D * this.mass / this.radius * this.radius;"))); + assertTrue(source.matches(PatternMaker.make(": 49 */", "double earthWeight = Double.parseDouble(args[0]);"))); + assertTrue(source.matches(PatternMaker.make(": 50 */", "double mass = earthWeight / EARTH.surfaceGravity();"))); + assertTrue(source.matches(PatternMaker.make(": 51 */", "for (Planet p : values()) {"))); + assertTrue(source.matches(PatternMaker.make(": 52 */", "System.out.printf(\"Your weight on %s is %f%n\", new Object[]", "{ p, Double.valueOf(p.surfaceWeight(mass)) } );"))); assertTrue(source.matches(PatternMaker.make("enum EmptyEnum {}"))); assertTrue(source.indexOf("public static final enum") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk901Enum() throws Exception { + String internalClassName = "org/jd/core/test/Enum"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-9.0.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -2136,7 +2278,7 @@ public void testJdk901Enum() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Enum"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2152,29 +2294,32 @@ public void testJdk901Enum() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 5]", "SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 5 */", "SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;"))); - assertTrue(source.matches(PatternMaker.make(": 9]", "MERCURY(3.303E23D, 2439700.0D),"))); - assertTrue(source.matches(PatternMaker.make(": 17]", "URANUS(8.686E25D, 2.5559E7D),"))); - assertTrue(source.matches(PatternMaker.make(": 20]", "NEPTUNE(1.024E26D, 2.4746E7D);"))); + assertTrue(source.matches(PatternMaker.make(": 9 */", "MERCURY(3.303E23D, 2439700.0D),"))); + assertTrue(source.matches(PatternMaker.make(": 17 */", "URANUS(8.686E25D, 2.5559E7D),"))); + assertTrue(source.matches(PatternMaker.make(": 20 */", "NEPTUNE(1.024E26D, 2.4746E7D);"))); assertTrue(source.indexOf("this.mass = mass;") != -1); - assertTrue(source.matches(PatternMaker.make(": 27]", "this.radius = radius;"))); - assertTrue(source.matches(PatternMaker.make(": 37]", "return 6.673E-11D * this.mass / this.radius * this.radius;"))); - assertTrue(source.matches(PatternMaker.make(": 49]", "double earthWeight = Double.parseDouble(args[0]);"))); - assertTrue(source.matches(PatternMaker.make(": 50]", "double mass = earthWeight / EARTH.surfaceGravity();"))); - assertTrue(source.matches(PatternMaker.make(": 51]", "for (Planet p : values()) {"))); - assertTrue(source.matches(PatternMaker.make(": 52]", "System.out.printf(\"Your weight on %s is %f%n\", new Object[]", "{ p, Double.valueOf(p.surfaceWeight(mass)) } );"))); + assertTrue(source.matches(PatternMaker.make(": 27 */", "this.radius = radius;"))); + assertTrue(source.matches(PatternMaker.make(": 37 */", "return 6.673E-11D * this.mass / this.radius * this.radius;"))); + assertTrue(source.matches(PatternMaker.make(": 49 */", "double earthWeight = Double.parseDouble(args[0]);"))); + assertTrue(source.matches(PatternMaker.make(": 50 */", "double mass = earthWeight / EARTH.surfaceGravity();"))); + assertTrue(source.matches(PatternMaker.make(": 51 */", "for (Planet p : values()) {"))); + assertTrue(source.matches(PatternMaker.make(": 52 */", "System.out.printf(\"Your weight on %s is %f%n\", new Object[]", "{ p, Double.valueOf(p.surfaceWeight(mass)) } );"))); assertTrue(source.matches(PatternMaker.make("enum EmptyEnum {}"))); assertTrue(source.indexOf("public static final enum") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.7", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk1002Enum() throws Exception { + String internalClassName = "org/jd/core/test/Enum"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-10.0.2.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -2182,7 +2327,7 @@ public void testJdk1002Enum() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Enum"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2198,29 +2343,32 @@ public void testJdk1002Enum() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 5]", "SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 5 */", "SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;"))); - assertTrue(source.matches(PatternMaker.make(": 9]", "MERCURY(3.303E23D, 2439700.0D),"))); - assertTrue(source.matches(PatternMaker.make(": 17]", "URANUS(8.686E25D, 2.5559E7D),"))); - assertTrue(source.matches(PatternMaker.make(": 20]", "NEPTUNE(1.024E26D, 2.4746E7D);"))); + assertTrue(source.matches(PatternMaker.make(": 9 */", "MERCURY(3.303E23D, 2439700.0D),"))); + assertTrue(source.matches(PatternMaker.make(": 17 */", "URANUS(8.686E25D, 2.5559E7D),"))); + assertTrue(source.matches(PatternMaker.make(": 20 */", "NEPTUNE(1.024E26D, 2.4746E7D);"))); assertTrue(source.indexOf("this.mass = mass;") != -1); - assertTrue(source.matches(PatternMaker.make(": 27]", "this.radius = radius;"))); - assertTrue(source.matches(PatternMaker.make(": 37]", "return 6.673E-11D * this.mass / this.radius * this.radius;"))); - assertTrue(source.matches(PatternMaker.make(": 49]", "double earthWeight = Double.parseDouble(args[0]);"))); - assertTrue(source.matches(PatternMaker.make(": 50]", "double mass = earthWeight / EARTH.surfaceGravity();"))); - assertTrue(source.matches(PatternMaker.make(": 51]", "for (Planet p : values()) {"))); - assertTrue(source.matches(PatternMaker.make(": 52]", "System.out.printf(\"Your weight on %s is %f%n\", new Object[]", "{ p, Double.valueOf(p.surfaceWeight(mass)) } );"))); + assertTrue(source.matches(PatternMaker.make(": 27 */", "this.radius = radius;"))); + assertTrue(source.matches(PatternMaker.make(": 37 */", "return 6.673E-11D * this.mass / this.radius * this.radius;"))); + assertTrue(source.matches(PatternMaker.make(": 49 */", "double earthWeight = Double.parseDouble(args[0]);"))); + assertTrue(source.matches(PatternMaker.make(": 50 */", "double mass = earthWeight / EARTH.surfaceGravity();"))); + assertTrue(source.matches(PatternMaker.make(": 51 */", "for (Planet p : values()) {"))); + assertTrue(source.matches(PatternMaker.make(": 52 */", "System.out.printf(\"Your weight on %s is %f%n\", new Object[]", "{ p, Double.valueOf(p.surfaceWeight(mass)) } );"))); assertTrue(source.matches(PatternMaker.make("enum EmptyEnum {}"))); assertTrue(source.indexOf("public static final enum") == -1); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk118Basic() throws Exception { + String internalClassName = "org/jd/core/test/Basic"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.1.8.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -2228,7 +2376,7 @@ public void testJdk118Basic() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Basic"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2244,20 +2392,23 @@ public void testJdk118Basic() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 43]", "Class class3 = String.class, class2 = class3, class1 = class2;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 43 */", "Class class3 = String.class, class2 = class3, class1 = class2;"))); assertTrue(source.matches(PatternMaker.make("String stringNull = null;"))); assertTrue(source.indexOf("public static native int read();") != -1); - assertTrue(source.matches(PatternMaker.make(": 126]", "int int78 = getInt78(new Object[] { this }, (short)5);"))); - assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return String.valueOf(str) + str;"))); - assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;"))); + assertTrue(source.matches(PatternMaker.make(": 126 */", "int int78 = getInt78(new Object[] { this }, (short)5);"))); + assertTrue(source.matches(PatternMaker.make("/* 171: 171 */", "return String.valueOf(str) + str;"))); + assertTrue(source.matches(PatternMaker.make("/* 174: 174 */", "return str;"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.3", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk142Basic() throws Exception { + String internalClassName = "org/jd/core/test/Basic"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.4.2.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -2265,7 +2416,7 @@ public void testJdk142Basic() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Basic"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2281,23 +2432,26 @@ public void testJdk142Basic() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 18]", "protected short short56 = 56;"))); - assertTrue(source.matches(PatternMaker.make(": 19]", "protected int int78 = 78;"))); - assertTrue(source.matches(PatternMaker.make(": 43]", "Class class3 = String.class, class2 = class3, class1 = class2;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 18 */", "protected short short56 = 56;"))); + assertTrue(source.matches(PatternMaker.make(": 19 */", "protected int int78 = 78;"))); + assertTrue(source.matches(PatternMaker.make(": 43 */", "Class class3 = String.class, class2 = class3, class1 = class2;"))); assertTrue(source.matches(PatternMaker.make("String stringNull = null;"))); assertTrue(source.indexOf("public static native int read();") != -1); - assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;"))); - assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;"))); - assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;"))); - assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);"))); + assertTrue(source.matches(PatternMaker.make("/* 171: 171 */", "return str + str;"))); + assertTrue(source.matches(PatternMaker.make("/* 174: 174 */", "return str;"))); + assertTrue(source.matches(PatternMaker.make("/* 183: 183 */", "return ((Basic)objects[index]).int78;"))); + assertTrue(source.matches(PatternMaker.make("/* 186: 186 */", "protected static final Integer INTEGER_255 = new Integer(255);"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.4", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk901Basic() throws Exception { + String internalClassName = "org/jd/core/test/Basic"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-9.0.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -2305,7 +2459,7 @@ public void testJdk901Basic() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Basic"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2321,23 +2475,26 @@ public void testJdk901Basic() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 18]", "protected short short56 = 56;"))); - assertTrue(source.matches(PatternMaker.make(": 19]", "protected int int78 = 78;"))); - assertTrue(source.matches(PatternMaker.make(": 43]", "Class class3 = String.class, class2 = class3, class1 = class2;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 18 */", "protected short short56 = 56;"))); + assertTrue(source.matches(PatternMaker.make(": 19 */", "protected int int78 = 78;"))); + assertTrue(source.matches(PatternMaker.make(": 43 */", "Class class3 = String.class, class2 = class3, class1 = class2;"))); assertTrue(source.matches(PatternMaker.make("String stringNull = null;"))); assertTrue(source.indexOf("public static native int read();") != -1); - assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;"))); - assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;"))); - assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;"))); - assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);"))); + assertTrue(source.matches(PatternMaker.make("/* 171: 171 */", "return str + str;"))); + assertTrue(source.matches(PatternMaker.make("/* 174: 174 */", "return str;"))); + assertTrue(source.matches(PatternMaker.make("/* 183: 183 */", "return ((Basic)objects[index]).int78;"))); + assertTrue(source.matches(PatternMaker.make("/* 186: 186 */", "protected static final Integer INTEGER_255 = new Integer(255);"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk1002Basic() throws Exception { + String internalClassName = "org/jd/core/test/Basic"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-10.0.2.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); @@ -2345,7 +2502,7 @@ public void testJdk1002Basic() throws Exception { Map configuration = Collections.singletonMap("realignLineNumbers", Boolean.TRUE); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Basic"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); message.setHeader("configuration", configuration); @@ -2361,30 +2518,33 @@ public void testJdk1002Basic() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 18]", "protected short short56 = 56;"))); - assertTrue(source.matches(PatternMaker.make(": 19]", "protected int int78 = 78;"))); - assertTrue(source.matches(PatternMaker.make(": 43]", "Class class3 = String.class, class2 = class3, class1 = class2;"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 18 */", "protected short short56 = 56;"))); + assertTrue(source.matches(PatternMaker.make(": 19 */", "protected int int78 = 78;"))); + assertTrue(source.matches(PatternMaker.make(": 43 */", "Class class3 = String.class, class2 = class3, class1 = class2;"))); assertTrue(source.matches(PatternMaker.make("String stringNull = null;"))); assertTrue(source.indexOf("public static native int read();") != -1); - assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;"))); - assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;"))); - assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;"))); - assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);"))); + assertTrue(source.matches(PatternMaker.make("/* 171: 171 */", "return str + str;"))); + assertTrue(source.matches(PatternMaker.make("/* 174: 174 */", "return str;"))); + assertTrue(source.matches(PatternMaker.make("/* 183: 183 */", "return ((Basic)objects[index]).int78;"))); + assertTrue(source.matches(PatternMaker.make("/* 186: 186 */", "protected static final Integer INTEGER_255 = new Integer(255);"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk180Lambda() throws Exception { + String internalClassName = "org/jd/core/test/Lambda"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-1.8.0.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/Lambda"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -2399,37 +2559,40 @@ public void testJdk180Lambda() throws Exception { printSource(source); - assertTrue(source.matches(PatternMaker.make(": 16]", "list.forEach(System.out::println);"))); - assertTrue(source.matches(PatternMaker.make(": 20]", "list.stream().filter(s -> (s != null)).forEach(s -> System.out.println(s));"))); + // Check decompiled source code + assertTrue(source.matches(PatternMaker.make(": 16 */", "list.forEach(System.out::println);"))); + assertTrue(source.matches(PatternMaker.make(": 20 */", "list.stream().filter(s -> (s != null)).forEach(s -> System.out.println(s));"))); assertTrue(source.indexOf("Predicate filter = s -> (s.length() == length);") != -1); assertTrue(source.indexOf("Consumer println = s -> System.out.println(s);") != -1); - assertTrue(source.matches(PatternMaker.make(": 27]", "list.stream().filter(filter).forEach(println);"))); - assertTrue(source.matches(PatternMaker.make(": 31]", "((Map)list.stream()"))); - assertTrue(source.matches(PatternMaker.make(": 32]", ".collect(Collectors.toMap(lambda -> Integer.valueOf(lambda.index), Function.identity())))"))); - assertTrue(source.matches(PatternMaker.make(": 33]", ".forEach((key, value) ->"))); - assertTrue(source.matches(PatternMaker.make(": 48]", "Thread thread = new Thread(() -> {"))); - assertTrue(source.matches(PatternMaker.make(": 58]", "Consumer staticMethodReference = String::valueOf;"))); - assertTrue(source.matches(PatternMaker.make(": 59]", "BiFunction methodReference = String::compareTo;"))); - assertTrue(source.matches(PatternMaker.make(": 60]", "Supplier instanceMethodReference = s::toString;"))); - assertTrue(source.matches(PatternMaker.make(": 61]", "Supplier constructorReference = String::new;"))); - assertTrue(source.matches(PatternMaker.make(": 65]", "MethodType mtToString = MethodType.methodType(String.class);"))); - assertTrue(source.matches(PatternMaker.make(": 66]", "MethodType mtSetter = MethodType.methodType(void.class, Object.class);"))); - assertTrue(source.matches(PatternMaker.make(": 67]", "MethodType mtStringComparator = MethodType.methodType(int[].class, String.class, new Class[]", "{ String.class"))); + assertTrue(source.matches(PatternMaker.make(": 27 */", "list.stream().filter(filter).forEach(println);"))); + assertTrue(source.matches(PatternMaker.make(": 31 */", "((Map)list.stream()"))); + assertTrue(source.matches(PatternMaker.make(": 32 */", ".collect(Collectors.toMap(lambda -> Integer.valueOf(lambda.index), Function.identity())))"))); + assertTrue(source.matches(PatternMaker.make(": 33 */", ".forEach((key, value) ->"))); + assertTrue(source.matches(PatternMaker.make(": 48 */", "Thread thread = new Thread(() -> {"))); + assertTrue(source.matches(PatternMaker.make(": 58 */", "Consumer staticMethodReference = String::valueOf;"))); + assertTrue(source.matches(PatternMaker.make(": 59 */", "BiFunction methodReference = String::compareTo;"))); + assertTrue(source.matches(PatternMaker.make(": 60 */", "Supplier instanceMethodReference = s::toString;"))); + assertTrue(source.matches(PatternMaker.make(": 61 */", "Supplier constructorReference = String::new;"))); + assertTrue(source.matches(PatternMaker.make(": 65 */", "MethodType mtToString = MethodType.methodType(String.class);"))); + assertTrue(source.matches(PatternMaker.make(": 66 */", "MethodType mtSetter = MethodType.methodType(void.class, Object.class);"))); + assertTrue(source.matches(PatternMaker.make(": 67 */", "MethodType mtStringComparator = MethodType.methodType(int[].class, String.class, new Class[]", "{ String.class"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + assertTrue(CompilerUtil.compile("1.8", new JavaSourceFileObject(internalClassName, source))); } @Test public void testJdk901InterfaceWithDefaultMethods() throws Exception { + String internalClassName = "org/jd/core/test/InterfaceWithDefaultMethods"; InputStream is = this.getClass().getResourceAsStream("/zip/data-java-jdk-9.0.1.zip"); Loader loader = new ZipLoader(is); //PlainTextMetaPrinter printer = new PlainTextMetaPrinter(); PlainTextPrinter printer = new PlainTextPrinter(); Message message = new Message(); - message.setHeader("mainInternalTypeName", "org/jd/core/test/InterfaceWithDefaultMethods"); + message.setHeader("mainInternalTypeName", internalClassName); message.setHeader("loader", loader); message.setHeader("printer", printer); @@ -2444,20 +2607,30 @@ public void testJdk901InterfaceWithDefaultMethods() throws Exception { printSource(source); + // Check decompiled source code assertTrue(source.matches(PatternMaker.make("public interface InterfaceWithDefaultMethods"))); assertTrue(source.matches(PatternMaker.make("void setTime(int paramInt1, int paramInt2, int paramInt3);"))); assertTrue(source.matches(PatternMaker.make("LocalDateTime getLocalDateTime();"))); assertTrue(source.matches(PatternMaker.make("static ZoneId getZoneId(String zoneString)"))); - assertTrue(source.matches(PatternMaker.make(": 24]", "return unsafeGetZoneId(zoneString);"))); - assertTrue(source.matches(PatternMaker.make(": 26]", "System.err.println(\"Invalid time zone: \" + zoneString + \"; using default time zone instead.\");"))); - assertTrue(source.matches(PatternMaker.make(": 27]", "return ZoneId.systemDefault();"))); - assertTrue(source.matches(PatternMaker.make(": 32]", "default ZonedDateTime getZonedDateTime(String zoneString) { return getZonedDateTime(getLocalDateTime(), getZoneId(zoneString)); }"))); - assertTrue(source.matches(PatternMaker.make(": 36]", "private static ZoneId unsafeGetZoneId(String zoneString) { return ZoneId.of(zoneString); }"))); - assertTrue(source.matches(PatternMaker.make(": 40]", "private ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) { return ZonedDateTime.of(localDateTime, zoneId); }"))); + assertTrue(source.matches(PatternMaker.make(": 24 */", "return unsafeGetZoneId(zoneString);"))); + assertTrue(source.matches(PatternMaker.make(": 26 */", "System.err.println(\"Invalid time zone: \" + zoneString + \"; using default time zone instead.\");"))); + assertTrue(source.matches(PatternMaker.make(": 27 */", "return ZoneId.systemDefault();"))); + assertTrue(source.matches(PatternMaker.make(": 32 */", "default ZonedDateTime getZonedDateTime(String zoneString) { return getZonedDateTime(getLocalDateTime(), getZoneId(zoneString)); }"))); + assertTrue(source.matches(PatternMaker.make(": 36 */", "private static ZoneId unsafeGetZoneId(String zoneString) { return ZoneId.of(zoneString); }"))); + assertTrue(source.matches(PatternMaker.make(": 40 */", "private ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) { return ZonedDateTime.of(localDateTime, zoneId); }"))); assertTrue(source.indexOf("// Byte code:") == -1); - assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1); - assertTrue(source.indexOf("/* ") == -1); + + // Recompile decompiled source code and check errors + try { + assertTrue(CompilerUtil.compile("1.9", new JavaSourceFileObject(internalClassName, source))); + } catch (IllegalArgumentException e) { + if (e.getMessage().contains("invalid source release: 1.9")) { + System.err.println("testJdk901InterfaceWithDefaultMethods() need a Java SDK 9+"); + } else { + assertTrue("Compilation failed: " + e.getMessage(), false); + } + } } protected void printSource(String source) { diff --git a/src/test/java/org/jd/core/v1/ControlFlowGraphTest.java b/src/test/java/org/jd/core/v1/ControlFlowGraphTest.java index 24cb2b5c..b3c35d8d 100644 --- a/src/test/java/org/jd/core/v1/ControlFlowGraphTest.java +++ b/src/test/java/org/jd/core/v1/ControlFlowGraphTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2019 Emmanuel Dupuy. + * Copyright (c) 2008, 2019 Emmanuel Dupuy. * This project is distributed under the GPLv3 license. * This is a Copyleft license that gives the user the right to use, * copy and modify the code freely for non-commercial purposes. @@ -22,7 +22,7 @@ import org.jd.core.v1.service.converter.classfiletojavasyntax.processor.ConvertClassFileProcessor; import org.jd.core.v1.service.converter.classfiletojavasyntax.util.*; import org.jd.core.v1.service.deserializer.classfile.DeserializeClassFileProcessor; -import org.jd.core.v1.util.ControlFlowGraphPlantUMLWriter; +import org.jd.core.v1.cfg.ControlFlowGraphPlantUMLWriter; import org.junit.Test; import java.io.FileInputStream; diff --git a/src/test/java/org/jd/core/v1/JavaFragmentToTokenTest.java b/src/test/java/org/jd/core/v1/JavaFragmentToTokenTest.java index 9e93dd38..96554796 100644 --- a/src/test/java/org/jd/core/v1/JavaFragmentToTokenTest.java +++ b/src/test/java/org/jd/core/v1/JavaFragmentToTokenTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2019 Emmanuel Dupuy. + * Copyright (c) 2008, 2019 Emmanuel Dupuy. * This project is distributed under the GPLv3 license. * This is a Copyleft license that gives the user the right to use, * copy and modify the code freely for non-commercial purposes. @@ -19,7 +19,7 @@ import org.jd.core.v1.service.writer.WriteTokenProcessor; import org.jd.core.v1.services.tokenizer.javafragmenttotoken.TestTokenizeJavaFragmentProcessor; import org.jd.core.v1.util.DefaultList; -import org.jd.core.v1.util.PatternMaker; +import org.jd.core.v1.regex.PatternMaker; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/org/jd/core/v1/LayoutFragmentProcessorTest.java b/src/test/java/org/jd/core/v1/LayoutFragmentProcessorTest.java index 458cd923..f2061d84 100644 --- a/src/test/java/org/jd/core/v1/LayoutFragmentProcessorTest.java +++ b/src/test/java/org/jd/core/v1/LayoutFragmentProcessorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2019 Emmanuel Dupuy. + * Copyright (c) 2008, 2019 Emmanuel Dupuy. * This project is distributed under the GPLv3 license. * This is a Copyleft license that gives the user the right to use, * copy and modify the code freely for non-commercial purposes. @@ -18,7 +18,7 @@ import org.jd.core.v1.service.tokenizer.javafragmenttotoken.JavaFragmentToTokenProcessor; import org.jd.core.v1.service.writer.WriteTokenProcessor; import org.jd.core.v1.services.tokenizer.javafragmenttotoken.TestTokenizeJavaFragmentProcessor; -import org.jd.core.v1.util.PatternMaker; +import org.jd.core.v1.regex.PatternMaker; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/org/jd/core/v1/util/ControlFlowGraphPlantUMLWriter.java b/src/test/java/org/jd/core/v1/cfg/ControlFlowGraphPlantUMLWriter.java similarity index 99% rename from src/test/java/org/jd/core/v1/util/ControlFlowGraphPlantUMLWriter.java rename to src/test/java/org/jd/core/v1/cfg/ControlFlowGraphPlantUMLWriter.java index b7eac98f..0cf8ac0b 100644 --- a/src/test/java/org/jd/core/v1/util/ControlFlowGraphPlantUMLWriter.java +++ b/src/test/java/org/jd/core/v1/cfg/ControlFlowGraphPlantUMLWriter.java @@ -1,16 +1,17 @@ /* - * Copyright (c) 2008-2019 Emmanuel Dupuy. + * Copyright (c) 2008, 2019 Emmanuel Dupuy. * This project is distributed under the GPLv3 license. * This is a Copyleft license that gives the user the right to use, * copy and modify the code freely for non-commercial purposes. */ -package org.jd.core.v1.util; +package org.jd.core.v1.cfg; import org.jd.core.v1.model.classfile.Method; import org.jd.core.v1.service.converter.classfiletojavasyntax.model.cfg.BasicBlock; import org.jd.core.v1.service.converter.classfiletojavasyntax.model.cfg.ControlFlowGraph; import org.jd.core.v1.service.converter.classfiletojavasyntax.util.ByteCodeWriter; +import org.jd.core.v1.util.DefaultList; import java.util.Comparator; import java.util.HashSet; diff --git a/src/test/java/org/jd/core/v1/compiler/CompilerUtil.java b/src/test/java/org/jd/core/v1/compiler/CompilerUtil.java new file mode 100644 index 00000000..6eeee267 --- /dev/null +++ b/src/test/java/org/jd/core/v1/compiler/CompilerUtil.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2008, 2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.core.v1.compiler; + +import javax.tools.*; +import java.io.File; +import java.io.StringWriter; +import java.util.Arrays; + +public class CompilerUtil { + protected static final File DESTINATION_DIRECTORY = new File("build/test-recompiled"); + protected static final String DESTINATION_DIRECTORY_PATH = DESTINATION_DIRECTORY.getAbsolutePath(); + + public static boolean compile(String javaVersion, JavaFileObject... JavaFileObjects) throws Exception { + boolean compilationSuccess = false; + + DESTINATION_DIRECTORY.mkdirs(); + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + StringWriter writer = new StringWriter(); + DiagnosticCollector diagnostics = new DiagnosticCollector<>(); + + try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) { + Iterable options = Arrays.asList("-source", javaVersion, "-target", javaVersion, "-d", DESTINATION_DIRECTORY_PATH); + Iterable compilationUnits = Arrays.asList(JavaFileObjects); + compilationSuccess = compiler.getTask(writer, fileManager, diagnostics, options, null, compilationUnits).call(); + + for (Diagnostic d : diagnostics.getDiagnostics()) { + if (d.getLineNumber() > 0) { + System.err.print(String.format("%-7s - line %-4d- %s%n", d.getKind(), d.getLineNumber(), d.getMessage(null))); + } else { + System.err.print(String.format("%-7s - - %s%n", d.getKind(), d.getMessage(null))); + } + } + } + + return compilationSuccess; + } +} diff --git a/src/test/java/org/jd/core/v1/compiler/JavaSourceFileObject.java b/src/test/java/org/jd/core/v1/compiler/JavaSourceFileObject.java new file mode 100644 index 00000000..a5cf67c4 --- /dev/null +++ b/src/test/java/org/jd/core/v1/compiler/JavaSourceFileObject.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2008, 2019 Emmanuel Dupuy. + * This project is distributed under the GPLv3 license. + * This is a Copyleft license that gives the user the right to use, + * copy and modify the code freely for non-commercial purposes. + */ + +package org.jd.core.v1.compiler; + +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; +import java.net.URI; + +public class JavaSourceFileObject extends SimpleJavaFileObject { + /** + * The source code of this "file". + */ + final String code; + + /** + * Constructs a new JavaSourceFromString. + * @param name the name of the compilation unit represented by this file object + * @param code the source code for the compilation unit represented by this file object + */ + public JavaSourceFileObject(String name, String code) { + super(URI.create("string:///" + name.replace('.','/') + JavaFileObject.Kind.SOURCE.extension), JavaFileObject.Kind.SOURCE); + this.code = code; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { + return code; + } +} diff --git a/src/test/java/org/jd/core/v1/printer/PlainTextPrinter.java b/src/test/java/org/jd/core/v1/printer/PlainTextPrinter.java index 8b6b48da..539bf684 100644 --- a/src/test/java/org/jd/core/v1/printer/PlainTextPrinter.java +++ b/src/test/java/org/jd/core/v1/printer/PlainTextPrinter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008-2019 Emmanuel Dupuy. + * Copyright (c) 2008, 2019 Emmanuel Dupuy. * This project is distributed under the GPLv3 license. * This is a Copyleft license that gives the user the right to use, * copy and modify the code freely for non-commercial purposes. @@ -11,10 +11,10 @@ import org.jd.core.v1.api.printer.Printer; public class PlainTextPrinter implements Printer { - protected static final String TAB = " "; - protected static final String NEWLINE = "\n"; + protected static final String TAB = " "; + protected static final String NEWLINE = "\n"; - protected int indentationCount; + protected int indentationCount; protected StringBuilder sb = new StringBuilder(); protected int realLineNumber = 0; protected String format; @@ -117,10 +117,10 @@ public void startMarker(int type) {} public void endMarker(int type) {} protected void printLineNumber(int lineNumber) { - sb.append('['); + sb.append("/*"); sb.append(String.format(format, ++realLineNumber)); sb.append(':'); sb.append(String.format(format, lineNumber)); - sb.append("] "); + sb.append(" */ "); } } diff --git a/src/test/java/org/jd/core/v1/util/PatternMaker.java b/src/test/java/org/jd/core/v1/regex/PatternMaker.java similarity index 94% rename from src/test/java/org/jd/core/v1/util/PatternMaker.java rename to src/test/java/org/jd/core/v1/regex/PatternMaker.java index d1463a7a..4bc8183b 100644 --- a/src/test/java/org/jd/core/v1/util/PatternMaker.java +++ b/src/test/java/org/jd/core/v1/regex/PatternMaker.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2008-2019 Emmanuel Dupuy. + * Copyright (c) 2008, 2019 Emmanuel Dupuy. * This project is distributed under the GPLv3 license. * This is a Copyleft license that gives the user the right to use, * copy and modify the code freely for non-commercial purposes. */ -package org.jd.core.v1.util; +package org.jd.core.v1.regex; public class PatternMaker { public static String make (String first, String... next) {