From 9bd8af2b431e06f783755ff1cca8d586bc7b29d5 Mon Sep 17 00:00:00 2001 From: "Egor.Ushakov" Date: Tue, 7 Oct 2014 21:03:09 +0400 Subject: [PATCH] calculate less indent buffers --- .../java/decompiler/main/ClassWriter.java | 54 ++++++++----------- .../java/decompiler/main/TextBuffer.java | 16 +++++- .../main/collectors/BytecodeSourceMapper.java | 20 +++---- .../java/decompiler/util/InterpreterUtil.java | 11 ++-- 4 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/org/jetbrains/java/decompiler/main/ClassWriter.java b/src/org/jetbrains/java/decompiler/main/ClassWriter.java index 61507fb..bdc2afa 100644 --- a/src/org/jetbrains/java/decompiler/main/ClassWriter.java +++ b/src/org/jetbrains/java/decompiler/main/ClassWriter.java @@ -149,8 +149,7 @@ public class ClassWriter { methodLambdaToJava(node, classNode, mt, buffer, indent + 1, !lambdaToAnonymous, tracer); - InterpreterUtil.appendIndent(buffer, indent); - buffer.append("}"); + buffer.appendIndent(indent).append("}"); } } finally { @@ -265,8 +264,7 @@ public class ClassWriter { } } - InterpreterUtil.appendIndent(buffer, indent); - buffer.append('}'); + buffer.appendIndent(indent).append('}'); if (node.type != ClassNode.CLASS_ANONYMOUS) { buffer.append(lineSeparator); @@ -281,7 +279,6 @@ public class ClassWriter { private void writeClassDefinition(ClassNode node, TextBuffer buffer, int indent) { String lineSeparator = DecompilerContext.getNewLineSeparator(); - String indentString = InterpreterUtil.getIndentString(indent); if (node.type == ClassNode.CLASS_ANONYMOUS) { buffer.append(" {"); @@ -300,7 +297,7 @@ public class ClassWriter { boolean isAnnotation = (flags & CodeConstants.ACC_ANNOTATION) != 0; if (isDeprecated) { - appendDeprecation(buffer, indentString, lineSeparator); + appendDeprecation(buffer, indent, lineSeparator); } if (interceptor != null) { @@ -309,12 +306,12 @@ public class ClassWriter { } if (isSynthetic) { - appendComment(buffer, "synthetic class", indentString, lineSeparator); + appendComment(buffer, "synthetic class", indent, lineSeparator); } appendAnnotations(buffer, cl, indent, lineSeparator); - buffer.append(indentString); + buffer.appendIndent(indent); if (isEnum) { // remove abstract and final flags (JLS 8.9 Enums) @@ -391,7 +388,6 @@ public class ClassWriter { } private void fieldToJava(ClassWrapper wrapper, StructClass cl, StructField fd, TextBuffer buffer, int indent, BytecodeMappingTracer tracer) { - String indentString = InterpreterUtil.getIndentString(indent); String lineSeparator = DecompilerContext.getNewLineSeparator(); boolean isInterface = cl.hasModifier(CodeConstants.ACC_INTERFACE); @@ -399,7 +395,7 @@ public class ClassWriter { boolean isEnum = fd.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM); if (isDeprecated) { - appendDeprecation(buffer, indentString, lineSeparator); + appendDeprecation(buffer, indent, lineSeparator); } if (interceptor != null) { @@ -408,12 +404,12 @@ public class ClassWriter { } if (fd.isSynthetic()) { - appendComment(buffer, "synthetic field", indentString, lineSeparator); + appendComment(buffer, "synthetic field", indent, lineSeparator); } appendAnnotations(buffer, fd, indent, lineSeparator); - buffer.append(indentString); + buffer.appendIndent(indent); if (!isEnum) { appendModifiers(buffer, fd.getAccessFlags(), FIELD_ALLOWED, isInterface, FIELD_EXCLUDED); @@ -494,7 +490,7 @@ public class ClassWriter { MethodDescriptor md_lambda = MethodDescriptor.parseDescriptor(lambdaNode.lambda_information.method_descriptor); if (!codeOnly) { - InterpreterUtil.appendIndent(buffer, indent); + buffer.appendIndent(indent); buffer.append("public "); buffer.append(method_name); buffer.append("("); @@ -547,7 +543,7 @@ public class ClassWriter { } if (methodWrapper.decompiledWithErrors) { - InterpreterUtil.appendIndent(buffer, indent); + buffer.appendIndent(indent); buffer.append("// $FF: Couldn't be decompiled"); buffer.append(DecompilerContext.getNewLineSeparator()); } @@ -555,7 +551,7 @@ public class ClassWriter { if (!codeOnly) { indent -= 1; - InterpreterUtil.appendIndent(buffer, indent); + buffer.appendIndent(indent); buffer.append('}'); buffer.append(DecompilerContext.getNewLineSeparator()); } @@ -573,7 +569,6 @@ public class ClassWriter { boolean hideMethod = false; int start_index_method = buffer.length(); - String indentString = InterpreterUtil.getIndentString(indent); String lineSeparator = DecompilerContext.getNewLineSeparator(); MethodWrapper outerWrapper = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER); @@ -606,7 +601,7 @@ public class ClassWriter { } if (isDeprecated) { - appendDeprecation(buffer, indentString, lineSeparator); + appendDeprecation(buffer, indent, lineSeparator); } if (interceptor != null) { @@ -617,15 +612,15 @@ public class ClassWriter { boolean isSynthetic = (flags & CodeConstants.ACC_SYNTHETIC) != 0 || mt.getAttributes().containsKey("Synthetic"); boolean isBridge = (flags & CodeConstants.ACC_BRIDGE) != 0; if (isSynthetic) { - appendComment(buffer, "synthetic method", indentString, lineSeparator); + appendComment(buffer, "synthetic method", indent, lineSeparator); } if (isBridge) { - appendComment(buffer, "bridge method", indentString, lineSeparator); + appendComment(buffer, "bridge method", indent, lineSeparator); } appendAnnotations(buffer, mt, indent, lineSeparator); - buffer.append(indentString); + buffer.appendIndent(indent); appendModifiers(buffer, flags, METHOD_ALLOWED, isInterface, METHOD_EXCLUDED); @@ -811,8 +806,7 @@ public class ClassWriter { //TODO: for now only start line set buffer.setCurrentLine(startLine); - buffer.append('{'); - buffer.append(lineSeparator); + buffer.append('{').appendLineSeparator(); RootStatement root = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root; @@ -834,14 +828,12 @@ public class ClassWriter { } if (methodWrapper.decompiledWithErrors) { - buffer.append(InterpreterUtil.getIndentString(indent + 1)); + buffer.appendIndent(indent + 1); buffer.append("// $FF: Couldn't be decompiled"); buffer.append(lineSeparator); } - buffer.append(indentString); - buffer.append('}'); - buffer.append(lineSeparator); + buffer.appendIndent(indent).append('}').appendLineSeparator(); } } finally { @@ -872,8 +864,8 @@ public class ClassWriter { return true; } - private static void appendDeprecation(TextBuffer buffer, String indentString, String lineSeparator) { - buffer.append(indentString).append("/** @deprecated */").append(lineSeparator); + private static void appendDeprecation(TextBuffer buffer, int indent, String lineSeparator) { + buffer.appendIndent(indent).append("/** @deprecated */").append(lineSeparator); } private enum MType {CLASS, FIELD, METHOD} @@ -881,7 +873,7 @@ public class ClassWriter { private static void appendRenameComment(TextBuffer buffer, String oldName, MType type, int indent, String lineSeparator) { if (oldName == null) return; - InterpreterUtil.appendIndent(buffer, indent); + buffer.appendIndent(indent); buffer.append("// $FF: renamed from: "); switch (type) { @@ -926,8 +918,8 @@ public class ClassWriter { return typeText; } - private static void appendComment(TextBuffer buffer, String comment, String indentString, String lineSeparator) { - buffer.append(indentString).append("// $FF: ").append(comment).append(lineSeparator); + private static void appendComment(TextBuffer buffer, String comment, int indent, String lineSeparator) { + buffer.appendIndent(indent).append("// $FF: ").append(comment).append(lineSeparator); } private static final String[] ANNOTATION_ATTRIBUTES = { diff --git a/src/org/jetbrains/java/decompiler/main/TextBuffer.java b/src/org/jetbrains/java/decompiler/main/TextBuffer.java index 6d279bf..32766ca 100644 --- a/src/org/jetbrains/java/decompiler/main/TextBuffer.java +++ b/src/org/jetbrains/java/decompiler/main/TextBuffer.java @@ -15,6 +15,8 @@ */ package org.jetbrains.java.decompiler.main; +import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; + import java.util.*; /** @@ -24,6 +26,7 @@ import java.util.*; */ public class TextBuffer { private final String myLineSeparator = DecompilerContext.getNewLineSeparator(); + private final String myIndent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING); private final StringBuilder myStringBuilder; private Map myLineToOffsetMapping = null; @@ -57,13 +60,21 @@ public class TextBuffer { return this; } - public void addBanner(String banner) { + public TextBuffer appendIndent(int length) { + while (length-- > 0) { + append(myIndent); + } + return this; + } + + public TextBuffer addBanner(String banner) { myStringBuilder.insert(0, banner); if (myLineToOffsetMapping != null) { for (Integer line : myLineToOffsetMapping.keySet()) { myLineToOffsetMapping.put(line, myLineToOffsetMapping.get(line) + banner.length()); } } + return this; } @Override @@ -138,7 +149,7 @@ public class TextBuffer { myStringBuilder.setLength(position); } - public void append(TextBuffer buffer) { + public TextBuffer append(TextBuffer buffer) { if (buffer.myLineToOffsetMapping != null && !buffer.myLineToOffsetMapping.isEmpty()) { checkMapCreated(); for (Map.Entry entry : buffer.myLineToOffsetMapping.entrySet()) { @@ -146,6 +157,7 @@ public class TextBuffer { } } myStringBuilder.append(buffer.myStringBuilder); + return this; } private void checkMapCreated() { diff --git a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java index 36c8ac8..7bf4f08 100644 --- a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java +++ b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java @@ -1,11 +1,10 @@ package org.jetbrains.java.decompiler.main.collectors; -import java.util.HashMap; -import java.util.Map.Entry; - import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.TextBuffer; -import org.jetbrains.java.decompiler.util.InterpreterUtil; + +import java.util.HashMap; +import java.util.Map.Entry; public class BytecodeSourceMapper { @@ -41,9 +40,6 @@ public class BytecodeSourceMapper { public void dumpMapping(TextBuffer buffer) { String lineSeparator = DecompilerContext.getNewLineSeparator(); - String indentstr1 = InterpreterUtil.getIndentString(1); - String indentstr2 = InterpreterUtil.getIndentString(2); - for(Entry>> class_entry : mapping.entrySet()) { HashMap> class_mapping = class_entry.getValue(); @@ -55,17 +51,17 @@ public class BytecodeSourceMapper { HashMap method_mapping = method_entry.getValue(); if(!is_first_method) { - buffer.append(lineSeparator); + buffer.appendLineSeparator(); } - buffer.append(indentstr1 + "method " + method_entry.getKey() + "{" + lineSeparator); + buffer.appendIndent(1).append("method " + method_entry.getKey() + "{" + lineSeparator); for(Entry line : method_mapping.entrySet()) { - buffer.append(indentstr2 + line.getKey() + indentstr2 + (line.getValue() +offset_total) + lineSeparator); + buffer.appendIndent(2).append(line.getKey().toString()).appendIndent(2).append((line.getValue() + offset_total) + lineSeparator); } - buffer.append(indentstr1 + "}" + lineSeparator); + buffer.appendIndent(1).append("}").appendLineSeparator(); is_first_method = false; } - buffer.append("}" + lineSeparator); + buffer.append("}").appendLineSeparator(); } } diff --git a/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java b/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java index 50411bc..67af3ad 100644 --- a/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java +++ b/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java @@ -92,17 +92,12 @@ public class InterpreterUtil { public static String getIndentString(int length) { if (length == 0) return ""; - TextBuffer buf = new TextBuffer(); - appendIndent(buf, length); - return buf.toString(); - } - - public static void appendIndent(TextBuffer buffer, int length) { - if (length == 0) return; + StringBuilder buf = new StringBuilder(); String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING); while (length-- > 0) { - buffer.append(indent); + buf.append(indent); } + return buf.toString(); } public static boolean equalSets(Collection c1, Collection c2) {