From 1f6417624639de346ffa74351d30b58d30f9e80b Mon Sep 17 00:00:00 2001 From: Thinkofdeath Date: Tue, 9 Dec 2014 10:17:27 +0000 Subject: [PATCH] Mavenized and fixed formatting to match jacobe --- .gitignore | 6 ++- pom.xml | 43 +++++++++++++++ .../java/decompiler/main/ClassWriter.java | 53 ++++++++++++++----- .../main/extern/IFernflowerPreferences.java | 2 +- .../modules/decompiler/ExprProcessor.java | 45 ++++++++++++++-- .../decompiler/exps/AssignmentExprent.java | 2 +- .../decompiler/exps/FunctionExprent.java | 6 +-- .../modules/decompiler/exps/IfExprent.java | 2 +- .../decompiler/exps/InvocationExprent.java | 2 +- .../decompiler/exps/MonitorExprent.java | 2 +- .../modules/decompiler/exps/NewExprent.java | 4 +- .../decompiler/exps/SwitchExprent.java | 2 +- .../decompiler/stats/BasicBlockStatement.java | 3 +- .../decompiler/stats/CatchAllStatement.java | 6 ++- .../decompiler/stats/CatchStatement.java | 6 ++- .../modules/decompiler/stats/DoStatement.java | 16 ++++-- .../modules/decompiler/stats/IfStatement.java | 39 ++++++++++++-- .../decompiler/stats/RootStatement.java | 2 +- .../decompiler/stats/SwitchStatement.java | 32 ++++++++--- .../stats/SynchronizedStatement.java | 24 +++++++-- .../jetbrains/java/decompiler/util/Util.java | 12 +++++ 21 files changed, 260 insertions(+), 49 deletions(-) create mode 100644 pom.xml create mode 100644 src/org/jetbrains/java/decompiler/util/Util.java diff --git a/.gitignore b/.gitignore index 3f1306c..6dce58a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ /dist/ /nbproject/ -/build/ \ No newline at end of file +/build/ +/target +/.idea + +*.iml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9c9b723 --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + org.spigotmc + fernflower + 0.1-SNAPSHOT + + + UTF-8 + + + + ./src + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.5 + + + true + + org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler + + + + + + + \ No newline at end of file diff --git a/src/org/jetbrains/java/decompiler/main/ClassWriter.java b/src/org/jetbrains/java/decompiler/main/ClassWriter.java index 7d3e66d..df4511e 100644 --- a/src/org/jetbrains/java/decompiler/main/ClassWriter.java +++ b/src/org/jetbrains/java/decompiler/main/ClassWriter.java @@ -42,6 +42,7 @@ import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor; import org.jetbrains.java.decompiler.struct.gen.VarType; import org.jetbrains.java.decompiler.struct.gen.generics.*; import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.Util; import java.util.LinkedHashMap; import java.util.List; @@ -179,7 +180,17 @@ public class ClassWriter { // write class definition int start_class_def = buffer.length(); - writeClassDefinition(node, buffer, indent); + boolean empty = cl.getFields().isEmpty() && cl.getMethods().isEmpty(); + if (cl.hasModifier(CodeConstants.ACC_ENUM)) { + empty = true; + for (StructField fd : cl.getFields()) { + if (fd.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM)) { + empty = false; + break; + } + } + } + writeClassDefinition(node, buffer, indent, empty); // // count lines in class definition the easiest way // total_offset_lines = buffer.substring(start_class_def).toString().split(lineSeparator, -1).length - 1; @@ -189,6 +200,7 @@ public class ClassWriter { // fields boolean enumFields = false; boolean endEnumWritten = false; // Spigot + boolean firstEnum = true; for (StructField fd : cl.getFields()) { boolean hide = fd.isSynthetic() && DecompilerContext.getOption(IFernflowerPreferences.REMOVE_SYNTHETIC) || @@ -198,8 +210,7 @@ public class ClassWriter { boolean isEnum = fd.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM); if (isEnum) { if (enumFields) { - buffer.append(','); - buffer.append(lineSeparator); + buffer.append(", "); } enumFields = true; } @@ -210,7 +221,14 @@ public class ClassWriter { endEnumWritten = true; // Spigot } - fieldToJava(wrapper, cl, fd, buffer, indent + 1, dummy_tracer); // FIXME: insert real tracer + int ind = indent + 1; + if (isEnum) { + if (!firstEnum) { + ind = 0; + } + firstEnum = false; + } + fieldToJava(wrapper, cl, fd, buffer, ind, dummy_tracer); // FIXME: insert real tracer hasContent = true; } @@ -218,8 +236,6 @@ public class ClassWriter { int flags = node.type == ClassNode.CLASS_ROOT ? cl.getAccessFlags() : node.access; boolean isEnum = DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM) && (flags & CodeConstants.ACC_ENUM) != 0; if (isEnum && !endEnumWritten) { - buffer.append(lineSeparator); - buffer.append(lineSeparator); // Spigot End buffer.append(';'); buffer.append(lineSeparator); @@ -283,12 +299,12 @@ public class ClassWriter { DecompilerContext.getLogger().endWriteClass(); } - private void writeClassDefinition(ClassNode node, TextBuffer buffer, int indent) { + private void writeClassDefinition(ClassNode node, TextBuffer buffer, int indent, boolean empty) { String lineSeparator = DecompilerContext.getNewLineSeparator(); if (node.type == ClassNode.CLASS_ANONYMOUS) { buffer.append(" {"); - buffer.append(lineSeparator); + if (!empty) buffer.append(lineSeparator); return; } @@ -390,8 +406,10 @@ public class ClassWriter { } buffer.append('{'); - buffer.append(lineSeparator); - buffer.append(lineSeparator); // Spigot + if (!empty) { + buffer.append(lineSeparator); + buffer.append(lineSeparator); // Spigot + } } private void fieldToJava(ClassWrapper wrapper, StructClass cl, StructField fd, TextBuffer buffer, int indent, BytecodeMappingTracer tracer) { @@ -817,9 +835,10 @@ public class ClassWriter { //TODO: for now only start line set buffer.setCurrentLine(startLine); - buffer.append('{').appendLineSeparator(); + buffer.append('{'); RootStatement root = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root; + boolean empty = false; if (root != null && !methodWrapper.decompiledWithErrors) { // check for existence try { @@ -827,8 +846,15 @@ public class ClassWriter { String code = root.toJava(indent + 1, tracer); + if (Util.rtrim(code).isEmpty()) code = ""; + hideMethod = (clinit || dinit || hideConstructor(wrapper, init, throwsExceptions, paramCount)) && code.length() == 0; + if (!code.isEmpty()) { + buffer.appendLineSeparator(); + } else { + empty = true; + } buffer.append(code); } catch (Throwable ex) { @@ -843,7 +869,10 @@ public class ClassWriter { buffer.append(lineSeparator); } - buffer.appendIndent(indent).append('}').appendLineSeparator(); + if (!empty) { + buffer.appendIndent(indent); + } + buffer.append('}').appendLineSeparator(); } } finally { diff --git a/src/org/jetbrains/java/decompiler/main/extern/IFernflowerPreferences.java b/src/org/jetbrains/java/decompiler/main/extern/IFernflowerPreferences.java index 677d05d..7d96e62 100644 --- a/src/org/jetbrains/java/decompiler/main/extern/IFernflowerPreferences.java +++ b/src/org/jetbrains/java/decompiler/main/extern/IFernflowerPreferences.java @@ -91,6 +91,6 @@ public interface IFernflowerPreferences { put(MAX_PROCESSING_METHOD, "0"); put(RENAME_ENTITIES, "0"); put(NEW_LINE_SEPARATOR, (InterpreterUtil.IS_WINDOWS ? "0" : "1")); - put(INDENT_STRING, " "); + put(INDENT_STRING, " "); }}); } diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java index 7b2f3a2..2450475 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java @@ -42,6 +42,7 @@ import org.jetbrains.java.decompiler.util.InterpreterUtil; import java.util.*; import org.jetbrains.java.decompiler.util.SortUtil; +import org.jetbrains.java.decompiler.util.Util; public class ExprProcessor implements CodeConstants { @@ -758,10 +759,18 @@ public class ExprProcessor implements CodeConstants { } public static String jmpWrapper(Statement stat, int indent, boolean semicolon, BytecodeMappingTracer tracer) { - StringBuilder buf = new StringBuilder(stat.toJava(indent, tracer)); - String new_line_separator = DecompilerContext.getNewLineSeparator(); + StringBuilder buf = new StringBuilder(); + String content = stat.toJava(indent, tracer); + if (!(stat instanceof IfStatement) && !(stat instanceof DoStatement) && !(stat instanceof SequenceStatement)) { + content = Util.rtrim(content); + } + buf.append(content); + if (!(stat instanceof IfStatement) && !(stat instanceof DoStatement) && !(stat instanceof SequenceStatement) && buf.length() != 0) { + buf.append(new_line_separator); + } + List lstSuccs = stat.getSuccessorEdges(Statement.STATEDGE_DIRECT_ALL); if (lstSuccs.size() == 1) { StatEdge edge = lstSuccs.get(0); @@ -815,12 +824,39 @@ public class ExprProcessor implements CodeConstants { String new_line_separator = DecompilerContext.getNewLineSeparator(); StringBuilder buf = new StringBuilder(); - // Spigot Start + // Spigot Start + boolean inVar = false; + boolean bloodySpecialCaseForNewAssignments = false; for (Iterator iter = SortUtil.sortIndexed(lst.iterator()); iter.hasNext();) { Exprent expr = iter.next(); // Spigot End String content = expr.toJava(indent, tracer); if (content.length() > 0) { + + if (expr instanceof VarExprent || expr instanceof AssignmentExprent) { + inVar = true; + boolean wasASpecialLittleFlower = bloodySpecialCaseForNewAssignments; + bloodySpecialCaseForNewAssignments = expr instanceof VarExprent; + if (expr instanceof AssignmentExprent && ((AssignmentExprent) expr).getLeft() instanceof VarExprent) { + AssignmentExprent assignmentExprent = (AssignmentExprent) expr; + VarExprent var = (VarExprent) assignmentExprent.getLeft(); + bloodySpecialCaseForNewAssignments = var.isDefinition(); + } + if (wasASpecialLittleFlower && !bloodySpecialCaseForNewAssignments) { + buf.append(new_line_separator); + } + } else if (inVar) { + inVar = false; + if (!(expr instanceof InvocationExprent + || expr instanceof ExitExprent + || expr instanceof FunctionExprent + ) + || bloodySpecialCaseForNewAssignments) { + bloodySpecialCaseForNewAssignments = false; + buf.append(new_line_separator); + } + } + if (expr.type != Exprent.EXPRENT_VAR || !((VarExprent)expr).isClassdef()) { buf.append(indstr); } @@ -832,6 +868,7 @@ public class ExprProcessor implements CodeConstants { buf.append(";"); } buf.append(new_line_separator); + tracer.incrementCurrentSourceLine(); } } @@ -895,7 +932,7 @@ public class ExprProcessor implements CodeConstants { res = "(" + res + ")"; } - res = "(" + getCastTypeName(leftType) + ")" + res; + res = "(" + getCastTypeName(leftType) + ") " + res; ret = true; } diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/AssignmentExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/AssignmentExprent.java index 9553f3f..75f4b75 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/AssignmentExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/AssignmentExprent.java @@ -148,7 +148,7 @@ public class AssignmentExprent extends Exprent { res = "(" + res + ")"; } - res = "(" + ExprProcessor.getCastTypeName(leftType) + ")" + res; + res = "(" + ExprProcessor.getCastTypeName(leftType) + ") " + res; } buffer.append(condtype == CONDITION_NONE ? " = " : funceq[condtype]).append(res); diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FunctionExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FunctionExprent.java index a527918..52f09c2 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FunctionExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/FunctionExprent.java @@ -470,7 +470,7 @@ public class FunctionExprent extends Exprent { case FUNCTION_NEG: return "-" + wrapOperandString(lstOperands.get(0), true, indent, tracer); case FUNCTION_CAST: - return "(" + lstOperands.get(1).toJava(indent, tracer) + ")" + wrapOperandString(lstOperands.get(0), true, indent, tracer); + return "(" + lstOperands.get(1).toJava(indent, tracer) + ") " + wrapOperandString(lstOperands.get(0), true, indent, tracer); case FUNCTION_ARRAYLENGTH: Exprent arr = lstOperands.get(0); @@ -483,7 +483,7 @@ public class FunctionExprent extends Exprent { } return res + ".length"; case FUNCTION_IIF: - return wrapOperandString(lstOperands.get(0), true, indent, tracer) + "?" + wrapOperandString(lstOperands.get(1), true, indent, tracer) + ":" + + return wrapOperandString(lstOperands.get(0), true, indent, tracer) + " ? " + wrapOperandString(lstOperands.get(1), true, indent, tracer) + " : " + wrapOperandString(lstOperands.get(2), true, indent, tracer); case FUNCTION_IPP: return wrapOperandString(lstOperands.get(0), true, indent, tracer) + "++"; @@ -528,7 +528,7 @@ public class FunctionExprent extends Exprent { } if (functype <= FUNCTION_I2S) { - return "(" + ExprProcessor.getTypeName(types[functype - FUNCTION_I2L]) + ")" + wrapOperandString(lstOperands.get(0), true, indent, tracer); + return "(" + ExprProcessor.getTypeName(types[functype - FUNCTION_I2L]) + ") " + wrapOperandString(lstOperands.get(0), true, indent, tracer); } // return ""; diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/IfExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/IfExprent.java index 32cc19d..4a2fbf5 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/IfExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/IfExprent.java @@ -115,7 +115,7 @@ public class IfExprent extends Exprent { @Override public String toJava(int indent, BytecodeMappingTracer tracer) { tracer.addMapping(bytecode); - return "if(" + condition.toJava(indent, tracer) + ")"; + return "if (" + condition.toJava(indent, tracer) + ")"; } public boolean equals(Object o) { diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java index e9d9064..320cc73 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java @@ -282,7 +282,7 @@ public class InvocationExprent extends Exprent { VarType leftType = new VarType(CodeConstants.TYPE_OBJECT, 0, classname); if (rightType.equals(VarType.VARTYPE_OBJECT) && !leftType.equals(rightType)) { - buf.append("((").append(ExprProcessor.getCastTypeName(leftType)).append(")"); + buf.append("((").append(ExprProcessor.getCastTypeName(leftType)).append(") "); if (instance.getPrecedence() >= FunctionExprent.getPrecedence(FunctionExprent.FUNCTION_CAST)) { res = "(" + res + ")"; diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/MonitorExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/MonitorExprent.java index 091d424..5a71ce3 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/MonitorExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/MonitorExprent.java @@ -56,7 +56,7 @@ public class MonitorExprent extends Exprent { tracer.addMapping(bytecode); if (montype == MONITOR_ENTER) { - return "synchronized(" + value.toJava(indent, tracer) + ")"; + return "synchronized (" + value.toJava(indent, tracer) + ")"; } else { return ""; diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/NewExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/NewExprent.java index bb55e45..8f8f684 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/NewExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/NewExprent.java @@ -275,7 +275,7 @@ public class NewExprent extends Exprent { VarType leftType = newtype.copy(); leftType.decArrayDim(); - buf.append("{"); + buf.append("{ "); for (int i = 0; i < lstArrayElements.size(); i++) { if (i > 0) { buf.append(", "); @@ -369,7 +369,7 @@ public class NewExprent extends Exprent { VarType leftType = newtype.copy(); leftType.decArrayDim(); - buf.append("{"); + buf.append(" { "); for (int i = 0; i < lstArrayElements.size(); i++) { if (i > 0) { buf.append(", "); diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/SwitchExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/SwitchExprent.java index 4784eb1..828d06a 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/SwitchExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/SwitchExprent.java @@ -85,7 +85,7 @@ public class SwitchExprent extends Exprent { @Override public String toJava(int indent, BytecodeMappingTracer tracer) { tracer.addMapping(bytecode); - return "switch(" + value.toJava(indent, tracer) + ")"; + return "switch (" + value.toJava(indent, tracer) + ")"; } public boolean equals(Object o) { diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/BasicBlockStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/BasicBlockStatement.java index 1ff1b2b..2fb809e 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/BasicBlockStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/BasicBlockStatement.java @@ -23,6 +23,7 @@ import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.collectors.BytecodeMappingTracer; import org.jetbrains.java.decompiler.main.collectors.CounterContainer; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; +import org.jetbrains.java.decompiler.util.Util; public class BasicBlockStatement extends Statement { @@ -68,7 +69,7 @@ public class BasicBlockStatement extends Statement { public String toJava(int indent, BytecodeMappingTracer tracer) { return ExprProcessor.listToJava(varDefinitions, indent, tracer) + - ExprProcessor.listToJava(exprents, indent, tracer); + ExprProcessor.listToJava(exprents, indent, tracer); } public Statement getSimpleCopy() { diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchAllStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchAllStatement.java index f22d1b1..b993a81 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchAllStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchAllStatement.java @@ -119,7 +119,11 @@ public class CatchAllStatement extends Statement { StringBuilder buf = new StringBuilder(); - buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); + String defs = ExprProcessor.listToJava(varDefinitions, indent, tracer); + buf.append(defs); + if (!defs.isEmpty()) { + buf.append(new_line_separator); + } boolean labeled = isLabeled(); if (labeled) { diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchStatement.java index a096258..685a60e 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchStatement.java @@ -156,7 +156,11 @@ public class CatchStatement extends Statement { String new_line_separator = DecompilerContext.getNewLineSeparator(); - buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); + String content = ExprProcessor.listToJava(varDefinitions, indent, tracer); + buf.append(content); + if (!content.isEmpty()) { + buf.append(new_line_separator); + } if (isLabeled()) { buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator); diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/DoStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/DoStatement.java index 1ddce1b..81f5ca2 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/DoStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/DoStatement.java @@ -21,6 +21,7 @@ import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; import org.jetbrains.java.decompiler.modules.decompiler.StatEdge; import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent; import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.Util; import java.util.ArrayList; import java.util.List; @@ -98,7 +99,11 @@ public class DoStatement extends Statement { String new_line_separator = DecompilerContext.getNewLineSeparator(); - buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); + String content = ExprProcessor.listToJava(varDefinitions, indent, tracer); + buf.append(content); + if (!content.isEmpty()) { + buf.append(new_line_separator); + } if (isLabeled()) { buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator); @@ -107,7 +112,7 @@ public class DoStatement extends Statement { switch (looptype) { case LOOP_DO: - buf.append(indstr).append("while(true) {").append(new_line_separator); + buf.append(indstr).append("while (true) {").append(new_line_separator); tracer.incrementCurrentSourceLine(); buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer)); buf.append(indstr).append("}").append(new_line_separator); @@ -117,18 +122,18 @@ public class DoStatement extends Statement { buf.append(indstr).append("do {").append(new_line_separator); tracer.incrementCurrentSourceLine(); buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer)); - buf.append(indstr).append("} while(").append(conditionExprent.get(0).toJava(indent, tracer)).append(");").append(new_line_separator); + buf.append(indstr).append("} while (").append(conditionExprent.get(0).toJava(indent, tracer)).append(");").append(new_line_separator); tracer.incrementCurrentSourceLine(); break; case LOOP_WHILE: - buf.append(indstr).append("while(").append(conditionExprent.get(0).toJava(indent, tracer)).append(") {").append(new_line_separator); + buf.append(indstr).append("while (").append(conditionExprent.get(0).toJava(indent, tracer)).append(") {").append(new_line_separator); tracer.incrementCurrentSourceLine(); buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer)); buf.append(indstr).append("}").append(new_line_separator); tracer.incrementCurrentSourceLine(); break; case LOOP_FOR: - buf.append(indstr).append("for(").append(initExprent.get(0) == null ? "" : initExprent.get(0).toJava(indent, tracer)).append("; ") + buf.append(indstr).append("for (").append(initExprent.get(0) == null ? "" : initExprent.get(0).toJava(indent, tracer)).append("; ") .append(conditionExprent.get(0).toJava(indent, tracer)).append("; ").append(incExprent.get(0).toJava(indent, tracer)).append(") {") .append(new_line_separator); tracer.incrementCurrentSourceLine(); @@ -137,6 +142,7 @@ public class DoStatement extends Statement { tracer.incrementCurrentSourceLine(); } + // buf.append(new_line_separator); return buf.toString(); } diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java index ebe2bd7..3e9d368 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java @@ -20,9 +20,9 @@ import org.jetbrains.java.decompiler.main.collectors.BytecodeMappingTracer; import org.jetbrains.java.decompiler.modules.decompiler.DecHelper; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; import org.jetbrains.java.decompiler.modules.decompiler.StatEdge; -import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent; -import org.jetbrains.java.decompiler.modules.decompiler.exps.IfExprent; +import org.jetbrains.java.decompiler.modules.decompiler.exps.*; import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.Util; import java.util.ArrayList; import java.util.List; @@ -207,7 +207,40 @@ public class IfStatement extends Statement { String new_line_separator = DecompilerContext.getNewLineSeparator(); buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); - buf.append(first.toJava(indent, tracer)); + + boolean bloodySpecialCases = false; + if (first instanceof BasicBlockStatement) { + List exps = first.getExprents(); + if (exps.size() != 0) { + Exprent last = exps.get(exps.size() - 1); + if (last instanceof AssignmentExprent) { + AssignmentExprent assignmentExprent = (AssignmentExprent) exps.get(exps.size() - 1); + bloodySpecialCases = true; + if (assignmentExprent.getLeft() instanceof VarExprent) { + VarExprent var = (VarExprent) assignmentExprent.getLeft(); + bloodySpecialCases = !var.isDefinition(); + } + } else if (last instanceof InvocationExprent) { + bloodySpecialCases = true; + } + } + } + + if (bloodySpecialCases) { + buf.append(Util.rtrim(first.toJava(indent, tracer))).append(new_line_separator); + } else { + String content = first.toJava(indent, tracer); + buf.append(content); + if (first instanceof BasicBlockStatement && !content.isEmpty()) { + List exps = first.getExprents(); + if (exps.size() != 0) { + Exprent e = exps.get(exps.size() - 1); + if (!(e instanceof InvocationExprent || e instanceof FunctionExprent)) { + buf.append(new_line_separator); + } + } + } + } if (isLabeled()) { buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator); diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/RootStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/RootStatement.java index 07009d6..58ec365 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/RootStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/RootStatement.java @@ -36,7 +36,7 @@ public class RootStatement extends Statement { public String toJava(int indent, BytecodeMappingTracer tracer) { return ExprProcessor.listToJava(varDefinitions, indent, tracer) + - first.toJava(indent, tracer); + first.toJava(indent, tracer); } public Statement getDummyExit() { diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SwitchStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SwitchStatement.java index aea44de..b0ec5e3 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SwitchStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SwitchStatement.java @@ -23,11 +23,10 @@ import org.jetbrains.java.decompiler.main.collectors.CounterContainer; import org.jetbrains.java.decompiler.modules.decompiler.DecHelper; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; import org.jetbrains.java.decompiler.modules.decompiler.StatEdge; -import org.jetbrains.java.decompiler.modules.decompiler.exps.ConstExprent; -import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent; -import org.jetbrains.java.decompiler.modules.decompiler.exps.SwitchExprent; +import org.jetbrains.java.decompiler.modules.decompiler.exps.*; import org.jetbrains.java.decompiler.struct.gen.VarType; import org.jetbrains.java.decompiler.util.InterpreterUtil; +import org.jetbrains.java.decompiler.util.Util; import java.util.*; @@ -114,8 +113,24 @@ public class SwitchStatement extends Statement { String new_line_separator = DecompilerContext.getNewLineSeparator(); StringBuilder buf = new StringBuilder(); - buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); - buf.append(first.toJava(indent, tracer)); + String content = ExprProcessor.listToJava(varDefinitions, indent, tracer); + buf.append(content); + if (!content.isEmpty()) { + buf.append(new_line_separator); + } + content = first.toJava(indent, tracer); + buf.append(content); + if (first instanceof BasicBlockStatement && !content.isEmpty()) { + List exps = first.getExprents(); + if (exps.size() != 0) { + Exprent e = exps.get(exps.size() - 1); + if (!(e instanceof InvocationExprent + || e instanceof FunctionExprent + || (e instanceof AssignmentExprent && !(((AssignmentExprent) e).getLeft() instanceof VarExprent && ((VarExprent) ((AssignmentExprent) e).getLeft()).isDefinition())))) { + buf.append(new_line_separator); + } + } + } if (isLabeled()) { buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator); @@ -147,7 +162,12 @@ public class SwitchStatement extends Statement { } } - buf.append(ExprProcessor.jmpWrapper(stat, indent + 1, false, tracer)); + String c = Util.rtrim(ExprProcessor.jmpWrapper(stat, indent + 1, false, tracer)); + if (!c.isEmpty()) { + buf.append(c); + buf.append(new_line_separator); + } + if (i != caseStatements.size() - 1) buf.append(new_line_separator); } buf.append(indstr).append("}").append(new_line_separator); diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SynchronizedStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SynchronizedStatement.java index eb94ddc..3ac68a6 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SynchronizedStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SynchronizedStatement.java @@ -20,7 +20,7 @@ import org.jetbrains.java.decompiler.main.collectors.BytecodeMappingTracer; import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor; import org.jetbrains.java.decompiler.modules.decompiler.SequenceHelper; import org.jetbrains.java.decompiler.modules.decompiler.StatEdge; -import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent; +import org.jetbrains.java.decompiler.modules.decompiler.exps.*; import org.jetbrains.java.decompiler.util.InterpreterUtil; import java.util.ArrayList; @@ -75,8 +75,26 @@ public class SynchronizedStatement extends Statement { String new_line_separator = DecompilerContext.getNewLineSeparator(); StringBuilder buf = new StringBuilder(); - buf.append(ExprProcessor.listToJava(varDefinitions, indent, tracer)); - buf.append(first.toJava(indent, tracer)); + + String content = ExprProcessor.listToJava(varDefinitions, indent, tracer); + buf.append(content); + if (!content.isEmpty()) { + buf.append(new_line_separator); + } + + content = first.toJava(indent, tracer); + buf.append(content); + if (first instanceof BasicBlockStatement && !content.isEmpty()) { + List exps = first.getExprents(); + if (exps.size() != 0) { + Exprent e = exps.get(exps.size() - 1); + if (!(e instanceof InvocationExprent + || e instanceof FunctionExprent + || (e instanceof AssignmentExprent && !(((AssignmentExprent) e).getLeft() instanceof VarExprent && ((VarExprent) ((AssignmentExprent) e).getLeft()).isDefinition())))) { + buf.append(new_line_separator); + } + } + } if (isLabeled()) { buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator); diff --git a/src/org/jetbrains/java/decompiler/util/Util.java b/src/org/jetbrains/java/decompiler/util/Util.java new file mode 100644 index 0000000..5ade232 --- /dev/null +++ b/src/org/jetbrains/java/decompiler/util/Util.java @@ -0,0 +1,12 @@ +package org.jetbrains.java.decompiler.util; + +import java.util.regex.Pattern; + +public class Util { + + private final static Pattern RTRIM = Pattern.compile("\\s+$"); + + public static String rtrim(String s) { + return RTRIM.matcher(s).replaceAll(""); + } +}