decompiler: registry flag to dump original line numbers as comments

master
Egor.Ushakov 10 years ago
parent eb3db8dc8b
commit 92af36f412
  1. 3
      src/org/jetbrains/java/decompiler/main/ClassesProcessor.java
  2. 33
      src/org/jetbrains/java/decompiler/main/TextBuffer.java
  3. 2
      src/org/jetbrains/java/decompiler/main/extern/IFernflowerPreferences.java

@ -285,6 +285,9 @@ public class ClassesProcessor {
if (DecompilerContext.getOption(IFernflowerPreferences.BYTECODE_SOURCE_MAPPING)) { if (DecompilerContext.getOption(IFernflowerPreferences.BYTECODE_SOURCE_MAPPING)) {
BytecodeSourceMapper mapper = DecompilerContext.getBytecodeSourceMapper(); BytecodeSourceMapper mapper = DecompilerContext.getBytecodeSourceMapper();
mapper.addTotalOffset(total_offset_lines); mapper.addTotalOffset(total_offset_lines);
if (DecompilerContext.getOption(IFernflowerPreferences.DUMP_ORIGINAL_LINES)) {
buffer.dumpOriginalLineNumbers(mapper.getOriginalLinesMapping());
}
if (DecompilerContext.getOption(IFernflowerPreferences.UNIT_TEST_MODE)) { if (DecompilerContext.getOption(IFernflowerPreferences.UNIT_TEST_MODE)) {
buffer.appendLineSeparator(); buffer.appendLineSeparator();
mapper.dumpMapping(buffer, true); mapper.dumpMapping(buffer, true);

@ -104,6 +104,9 @@ public class TextBuffer {
public String toString() { public String toString() {
String original = myStringBuilder.toString(); String original = myStringBuilder.toString();
if (myLineToOffsetMapping == null || myLineToOffsetMapping.isEmpty()) { if (myLineToOffsetMapping == null || myLineToOffsetMapping.isEmpty()) {
if (myLineMapping != null) {
return addOriginalLineNumbers();
}
return original; return original;
} }
else { else {
@ -140,6 +143,26 @@ public class TextBuffer {
} }
} }
private String addOriginalLineNumbers() {
StringBuilder sb = new StringBuilder();
int lineStart = 0, lineEnd;
int count = 0, length = myLineSeparator.length();
while ((lineEnd = myStringBuilder.indexOf(myLineSeparator, lineStart)) > 0) {
++count;
sb.append(myStringBuilder.substring(lineStart, lineEnd));
Integer integer = myLineMapping.get(count);
if (integer != null) {
sb.append("// ").append(integer);
}
sb.append(myLineSeparator);
lineStart = lineEnd + length;
}
if (lineStart < myStringBuilder.length()) {
sb.append(myStringBuilder.substring(lineStart));
}
return sb.toString();
}
private void appendLines(StringBuilder res, String[] srcLines, int from, int to, int requiredLineNumber) { private void appendLines(StringBuilder res, String[] srcLines, int from, int to, int requiredLineNumber) {
if (to - from > requiredLineNumber) { if (to - from > requiredLineNumber) {
List<String> strings = compactLines(Arrays.asList(srcLines).subList(from, to) ,requiredLineNumber); List<String> strings = compactLines(Arrays.asList(srcLines).subList(from, to) ,requiredLineNumber);
@ -278,4 +301,14 @@ public class TextBuffer {
public StringBuilder getOriginalText() { public StringBuilder getOriginalText() {
return myStringBuilder; return myStringBuilder;
} }
private Map<Integer, Integer> myLineMapping = null; // new to original
public void dumpOriginalLineNumbers(int[] lineMapping) {
if (lineMapping.length > 0) {
myLineMapping = new HashMap<Integer, Integer>();
for (int i = 0; i < lineMapping.length; i+=2) {
myLineMapping.put(lineMapping[i+1], lineMapping[i]);
}
}
}
} }

@ -54,6 +54,7 @@ public interface IFernflowerPreferences {
String INDENT_STRING = "ind"; String INDENT_STRING = "ind";
String BANNER = "ban"; String BANNER = "ban";
String DUMP_ORIGINAL_LINES = "__dump_original_lines__";
String UNIT_TEST_MODE = "__unit_test_mode__"; String UNIT_TEST_MODE = "__unit_test_mode__";
String LINE_SEPARATOR_WIN = "\r\n"; String LINE_SEPARATOR_WIN = "\r\n";
@ -91,5 +92,6 @@ public interface IFernflowerPreferences {
put(INDENT_STRING, " "); put(INDENT_STRING, " ");
put(BANNER, ""); put(BANNER, "");
put(UNIT_TEST_MODE, "0"); put(UNIT_TEST_MODE, "0");
put(DUMP_ORIGINAL_LINES, "0");
}}); }});
} }

Loading…
Cancel
Save