calculate less indent buffers

master
Egor.Ushakov 10 years ago
parent 125441a88f
commit 9bd8af2b43
  1. 54
      src/org/jetbrains/java/decompiler/main/ClassWriter.java
  2. 16
      src/org/jetbrains/java/decompiler/main/TextBuffer.java
  3. 20
      src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java
  4. 11
      src/org/jetbrains/java/decompiler/util/InterpreterUtil.java

@ -149,8 +149,7 @@ public class ClassWriter {
methodLambdaToJava(node, classNode, mt, buffer, indent + 1, !lambdaToAnonymous, tracer); methodLambdaToJava(node, classNode, mt, buffer, indent + 1, !lambdaToAnonymous, tracer);
InterpreterUtil.appendIndent(buffer, indent); buffer.appendIndent(indent).append("}");
buffer.append("}");
} }
} }
finally { finally {
@ -265,8 +264,7 @@ public class ClassWriter {
} }
} }
InterpreterUtil.appendIndent(buffer, indent); buffer.appendIndent(indent).append('}');
buffer.append('}');
if (node.type != ClassNode.CLASS_ANONYMOUS) { if (node.type != ClassNode.CLASS_ANONYMOUS) {
buffer.append(lineSeparator); buffer.append(lineSeparator);
@ -281,7 +279,6 @@ public class ClassWriter {
private void writeClassDefinition(ClassNode node, TextBuffer buffer, int indent) { private void writeClassDefinition(ClassNode node, TextBuffer buffer, int indent) {
String lineSeparator = DecompilerContext.getNewLineSeparator(); String lineSeparator = DecompilerContext.getNewLineSeparator();
String indentString = InterpreterUtil.getIndentString(indent);
if (node.type == ClassNode.CLASS_ANONYMOUS) { if (node.type == ClassNode.CLASS_ANONYMOUS) {
buffer.append(" {"); buffer.append(" {");
@ -300,7 +297,7 @@ public class ClassWriter {
boolean isAnnotation = (flags & CodeConstants.ACC_ANNOTATION) != 0; boolean isAnnotation = (flags & CodeConstants.ACC_ANNOTATION) != 0;
if (isDeprecated) { if (isDeprecated) {
appendDeprecation(buffer, indentString, lineSeparator); appendDeprecation(buffer, indent, lineSeparator);
} }
if (interceptor != null) { if (interceptor != null) {
@ -309,12 +306,12 @@ public class ClassWriter {
} }
if (isSynthetic) { if (isSynthetic) {
appendComment(buffer, "synthetic class", indentString, lineSeparator); appendComment(buffer, "synthetic class", indent, lineSeparator);
} }
appendAnnotations(buffer, cl, indent, lineSeparator); appendAnnotations(buffer, cl, indent, lineSeparator);
buffer.append(indentString); buffer.appendIndent(indent);
if (isEnum) { if (isEnum) {
// remove abstract and final flags (JLS 8.9 Enums) // 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) { private void fieldToJava(ClassWrapper wrapper, StructClass cl, StructField fd, TextBuffer buffer, int indent, BytecodeMappingTracer tracer) {
String indentString = InterpreterUtil.getIndentString(indent);
String lineSeparator = DecompilerContext.getNewLineSeparator(); String lineSeparator = DecompilerContext.getNewLineSeparator();
boolean isInterface = cl.hasModifier(CodeConstants.ACC_INTERFACE); 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); boolean isEnum = fd.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM);
if (isDeprecated) { if (isDeprecated) {
appendDeprecation(buffer, indentString, lineSeparator); appendDeprecation(buffer, indent, lineSeparator);
} }
if (interceptor != null) { if (interceptor != null) {
@ -408,12 +404,12 @@ public class ClassWriter {
} }
if (fd.isSynthetic()) { if (fd.isSynthetic()) {
appendComment(buffer, "synthetic field", indentString, lineSeparator); appendComment(buffer, "synthetic field", indent, lineSeparator);
} }
appendAnnotations(buffer, fd, indent, lineSeparator); appendAnnotations(buffer, fd, indent, lineSeparator);
buffer.append(indentString); buffer.appendIndent(indent);
if (!isEnum) { if (!isEnum) {
appendModifiers(buffer, fd.getAccessFlags(), FIELD_ALLOWED, isInterface, FIELD_EXCLUDED); 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); MethodDescriptor md_lambda = MethodDescriptor.parseDescriptor(lambdaNode.lambda_information.method_descriptor);
if (!codeOnly) { if (!codeOnly) {
InterpreterUtil.appendIndent(buffer, indent); buffer.appendIndent(indent);
buffer.append("public "); buffer.append("public ");
buffer.append(method_name); buffer.append(method_name);
buffer.append("("); buffer.append("(");
@ -547,7 +543,7 @@ public class ClassWriter {
} }
if (methodWrapper.decompiledWithErrors) { if (methodWrapper.decompiledWithErrors) {
InterpreterUtil.appendIndent(buffer, indent); buffer.appendIndent(indent);
buffer.append("// $FF: Couldn't be decompiled"); buffer.append("// $FF: Couldn't be decompiled");
buffer.append(DecompilerContext.getNewLineSeparator()); buffer.append(DecompilerContext.getNewLineSeparator());
} }
@ -555,7 +551,7 @@ public class ClassWriter {
if (!codeOnly) { if (!codeOnly) {
indent -= 1; indent -= 1;
InterpreterUtil.appendIndent(buffer, indent); buffer.appendIndent(indent);
buffer.append('}'); buffer.append('}');
buffer.append(DecompilerContext.getNewLineSeparator()); buffer.append(DecompilerContext.getNewLineSeparator());
} }
@ -573,7 +569,6 @@ public class ClassWriter {
boolean hideMethod = false; boolean hideMethod = false;
int start_index_method = buffer.length(); int start_index_method = buffer.length();
String indentString = InterpreterUtil.getIndentString(indent);
String lineSeparator = DecompilerContext.getNewLineSeparator(); String lineSeparator = DecompilerContext.getNewLineSeparator();
MethodWrapper outerWrapper = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER); MethodWrapper outerWrapper = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
@ -606,7 +601,7 @@ public class ClassWriter {
} }
if (isDeprecated) { if (isDeprecated) {
appendDeprecation(buffer, indentString, lineSeparator); appendDeprecation(buffer, indent, lineSeparator);
} }
if (interceptor != null) { if (interceptor != null) {
@ -617,15 +612,15 @@ public class ClassWriter {
boolean isSynthetic = (flags & CodeConstants.ACC_SYNTHETIC) != 0 || mt.getAttributes().containsKey("Synthetic"); boolean isSynthetic = (flags & CodeConstants.ACC_SYNTHETIC) != 0 || mt.getAttributes().containsKey("Synthetic");
boolean isBridge = (flags & CodeConstants.ACC_BRIDGE) != 0; boolean isBridge = (flags & CodeConstants.ACC_BRIDGE) != 0;
if (isSynthetic) { if (isSynthetic) {
appendComment(buffer, "synthetic method", indentString, lineSeparator); appendComment(buffer, "synthetic method", indent, lineSeparator);
} }
if (isBridge) { if (isBridge) {
appendComment(buffer, "bridge method", indentString, lineSeparator); appendComment(buffer, "bridge method", indent, lineSeparator);
} }
appendAnnotations(buffer, mt, indent, lineSeparator); appendAnnotations(buffer, mt, indent, lineSeparator);
buffer.append(indentString); buffer.appendIndent(indent);
appendModifiers(buffer, flags, METHOD_ALLOWED, isInterface, METHOD_EXCLUDED); appendModifiers(buffer, flags, METHOD_ALLOWED, isInterface, METHOD_EXCLUDED);
@ -811,8 +806,7 @@ public class ClassWriter {
//TODO: for now only start line set //TODO: for now only start line set
buffer.setCurrentLine(startLine); buffer.setCurrentLine(startLine);
buffer.append('{'); buffer.append('{').appendLineSeparator();
buffer.append(lineSeparator);
RootStatement root = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root; RootStatement root = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root;
@ -834,14 +828,12 @@ public class ClassWriter {
} }
if (methodWrapper.decompiledWithErrors) { if (methodWrapper.decompiledWithErrors) {
buffer.append(InterpreterUtil.getIndentString(indent + 1)); buffer.appendIndent(indent + 1);
buffer.append("// $FF: Couldn't be decompiled"); buffer.append("// $FF: Couldn't be decompiled");
buffer.append(lineSeparator); buffer.append(lineSeparator);
} }
buffer.append(indentString); buffer.appendIndent(indent).append('}').appendLineSeparator();
buffer.append('}');
buffer.append(lineSeparator);
} }
} }
finally { finally {
@ -872,8 +864,8 @@ public class ClassWriter {
return true; return true;
} }
private static void appendDeprecation(TextBuffer buffer, String indentString, String lineSeparator) { private static void appendDeprecation(TextBuffer buffer, int indent, String lineSeparator) {
buffer.append(indentString).append("/** @deprecated */").append(lineSeparator); buffer.appendIndent(indent).append("/** @deprecated */").append(lineSeparator);
} }
private enum MType {CLASS, FIELD, METHOD} 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) { private static void appendRenameComment(TextBuffer buffer, String oldName, MType type, int indent, String lineSeparator) {
if (oldName == null) return; if (oldName == null) return;
InterpreterUtil.appendIndent(buffer, indent); buffer.appendIndent(indent);
buffer.append("// $FF: renamed from: "); buffer.append("// $FF: renamed from: ");
switch (type) { switch (type) {
@ -926,8 +918,8 @@ public class ClassWriter {
return typeText; return typeText;
} }
private static void appendComment(TextBuffer buffer, String comment, String indentString, String lineSeparator) { private static void appendComment(TextBuffer buffer, String comment, int indent, String lineSeparator) {
buffer.append(indentString).append("// $FF: ").append(comment).append(lineSeparator); buffer.appendIndent(indent).append("// $FF: ").append(comment).append(lineSeparator);
} }
private static final String[] ANNOTATION_ATTRIBUTES = { private static final String[] ANNOTATION_ATTRIBUTES = {

@ -15,6 +15,8 @@
*/ */
package org.jetbrains.java.decompiler.main; package org.jetbrains.java.decompiler.main;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import java.util.*; import java.util.*;
/** /**
@ -24,6 +26,7 @@ import java.util.*;
*/ */
public class TextBuffer { public class TextBuffer {
private final String myLineSeparator = DecompilerContext.getNewLineSeparator(); private final String myLineSeparator = DecompilerContext.getNewLineSeparator();
private final String myIndent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING);
private final StringBuilder myStringBuilder; private final StringBuilder myStringBuilder;
private Map<Integer, Integer> myLineToOffsetMapping = null; private Map<Integer, Integer> myLineToOffsetMapping = null;
@ -57,13 +60,21 @@ public class TextBuffer {
return this; 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); myStringBuilder.insert(0, banner);
if (myLineToOffsetMapping != null) { if (myLineToOffsetMapping != null) {
for (Integer line : myLineToOffsetMapping.keySet()) { for (Integer line : myLineToOffsetMapping.keySet()) {
myLineToOffsetMapping.put(line, myLineToOffsetMapping.get(line) + banner.length()); myLineToOffsetMapping.put(line, myLineToOffsetMapping.get(line) + banner.length());
} }
} }
return this;
} }
@Override @Override
@ -138,7 +149,7 @@ public class TextBuffer {
myStringBuilder.setLength(position); myStringBuilder.setLength(position);
} }
public void append(TextBuffer buffer) { public TextBuffer append(TextBuffer buffer) {
if (buffer.myLineToOffsetMapping != null && !buffer.myLineToOffsetMapping.isEmpty()) { if (buffer.myLineToOffsetMapping != null && !buffer.myLineToOffsetMapping.isEmpty()) {
checkMapCreated(); checkMapCreated();
for (Map.Entry<Integer, Integer> entry : buffer.myLineToOffsetMapping.entrySet()) { for (Map.Entry<Integer, Integer> entry : buffer.myLineToOffsetMapping.entrySet()) {
@ -146,6 +157,7 @@ public class TextBuffer {
} }
} }
myStringBuilder.append(buffer.myStringBuilder); myStringBuilder.append(buffer.myStringBuilder);
return this;
} }
private void checkMapCreated() { private void checkMapCreated() {

@ -1,11 +1,10 @@
package org.jetbrains.java.decompiler.main.collectors; 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.DecompilerContext;
import org.jetbrains.java.decompiler.main.TextBuffer; 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 { public class BytecodeSourceMapper {
@ -41,9 +40,6 @@ public class BytecodeSourceMapper {
public void dumpMapping(TextBuffer buffer) { public void dumpMapping(TextBuffer buffer) {
String lineSeparator = DecompilerContext.getNewLineSeparator(); String lineSeparator = DecompilerContext.getNewLineSeparator();
String indentstr1 = InterpreterUtil.getIndentString(1);
String indentstr2 = InterpreterUtil.getIndentString(2);
for(Entry<String, HashMap<String, HashMap<Integer, Integer>>> class_entry : mapping.entrySet()) { for(Entry<String, HashMap<String, HashMap<Integer, Integer>>> class_entry : mapping.entrySet()) {
HashMap<String, HashMap<Integer, Integer>> class_mapping = class_entry.getValue(); HashMap<String, HashMap<Integer, Integer>> class_mapping = class_entry.getValue();
@ -55,17 +51,17 @@ public class BytecodeSourceMapper {
HashMap<Integer, Integer> method_mapping = method_entry.getValue(); HashMap<Integer, Integer> method_mapping = method_entry.getValue();
if(!is_first_method) { 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<Integer, Integer> line : method_mapping.entrySet()) { for(Entry<Integer, Integer> 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; is_first_method = false;
} }
buffer.append("}" + lineSeparator); buffer.append("}").appendLineSeparator();
} }
} }

@ -92,17 +92,12 @@ public class InterpreterUtil {
public static String getIndentString(int length) { public static String getIndentString(int length) {
if (length == 0) return ""; if (length == 0) return "";
TextBuffer buf = new TextBuffer(); StringBuilder buf = new StringBuilder();
appendIndent(buf, length);
return buf.toString();
}
public static void appendIndent(TextBuffer buffer, int length) {
if (length == 0) return;
String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING); String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING);
while (length-- > 0) { while (length-- > 0) {
buffer.append(indent); buf.append(indent);
} }
return buf.toString();
} }
public static boolean equalSets(Collection<?> c1, Collection<?> c2) { public static boolean equalSets(Collection<?> c1, Collection<?> c2) {

Loading…
Cancel
Save