Fixed some errors in line counting

This commit is contained in:
Stiver 2014-10-07 07:31:22 +02:00
parent bef17b44fc
commit 9f9da912f6
12 changed files with 77 additions and 52 deletions

View File

@ -182,8 +182,8 @@ public class ClassWriter {
int start_class_def = buffer.length();
writeClassDefinition(node, buffer, indent);
// count lines in class definition the easiest way
total_offset_lines = buffer.substring(start_class_def).toString().split(lineSeparator, -1).length - 1;
// // count lines in class definition the easiest way
// total_offset_lines = buffer.substring(start_class_def).toString().split(lineSeparator, -1).length - 1;
boolean hasContent = false;
@ -220,6 +220,9 @@ public class ClassWriter {
buffer.append(lineSeparator);
}
// FIXME: fields don't matter at the moment
total_offset_lines = buffer.substring(start_class_def).toString().split(lineSeparator, -1).length - 1;
// methods
for (StructMethod mt : cl.getMethods()) {
boolean hide = mt.isSynthetic() && DecompilerContext.getOption(IFernflowerPreferences.REMOVE_SYNTHETIC) ||
@ -237,7 +240,7 @@ public class ClassWriter {
hasContent = true;
DecompilerContext.getBytecodeSourceMapper().addTracer(cl.qualifiedName,
InterpreterUtil.makeUniqueKey(mt.getName(), mt.getDescriptor()), method_tracer);
total_offset_lines = method_tracer.getCurrentSourceline();
total_offset_lines = (method_tracer.getCurrentSourceLine() + 1); // zero-based line index
}
else {
buffer.setLength(position);
@ -805,7 +808,7 @@ public class ClassWriter {
if (root != null && !methodWrapper.decompiledWithErrors) { // check for existence
try {
tracer.setCurrentSourceline(buffer.substring(start_index_method).split(lineSeparator, -1).length - 1);
tracer.incrementCurrentSourceLine(buffer.substring(start_index_method).split(lineSeparator, -1).length - 1);
String code = root.toJava(indent + 1, tracer);
@ -836,7 +839,7 @@ public class ClassWriter {
// save total lines
// TODO: optimize
tracer.setCurrentSourceline(buffer.substring(start_index_method).split(lineSeparator, -1).length - 1);
tracer.setCurrentSourceLine(buffer.substring(start_index_method).split(lineSeparator, -1).length - 1);
return !hideMethod;
}

View File

@ -268,7 +268,7 @@ public class ClassesProcessor {
int index = cl.qualifiedName.lastIndexOf("/");
if (index >= 0) {
total_offset_lines++;
total_offset_lines+=2;
String packageName = cl.qualifiedName.substring(0, index).replace('/', '.');
buffer.append("package ");
@ -283,6 +283,7 @@ public class ClassesProcessor {
buffer.append(lineSeparator);
total_offset_lines += import_lines_written + 1;
}
//buffer.append(lineSeparator);
buffer.append(classBuffer);

View File

@ -1,6 +1,7 @@
package org.jetbrains.java.decompiler.main.collectors;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class BytecodeMappingTracer {
@ -16,14 +17,20 @@ public class BytecodeMappingTracer {
current_sourceline = initial_source_line;
}
public void incrementSourceLine() {
public void incrementCurrentSourceLine() {
current_sourceline++;
}
public void incrementSourceLine(int number_lines) {
public void incrementCurrentSourceLine(int number_lines) {
current_sourceline += number_lines;
}
public void shiftSourceLines(int shift) {
for(Entry<Integer, Integer> entry : mapping.entrySet()) {
entry.setValue(entry.getValue() + shift);
}
}
public void addMapping(int bytecode_offset) {
if(!mapping.containsKey(bytecode_offset)) {
mapping.put(bytecode_offset, current_sourceline);
@ -38,15 +45,25 @@ public class BytecodeMappingTracer {
}
}
public void addTracer(BytecodeMappingTracer tracer) {
if(tracer != null) {
for(Entry<Integer, Integer> entry : tracer.mapping.entrySet()) {
if(!mapping.containsKey(entry.getKey())) {
mapping.put(entry.getKey(), entry.getValue());
}
}
}
}
public HashMap<Integer, Integer> getMapping() {
return mapping;
}
public int getCurrentSourceline() {
public int getCurrentSourceLine() {
return current_sourceline;
}
public void setCurrentSourceline(int current_sourceline) {
public void setCurrentSourceLine(int current_sourceline) {
this.current_sourceline = current_sourceline;
}

View File

@ -59,7 +59,7 @@ public class BytecodeSourceMapper {
buffer.append(indentstr1 + "method " + method_entry.getKey() + "{" + lineSeparator);
for(Entry<Integer, Integer> line : method_mapping.entrySet()) {
buffer.append(indentstr2 + line.getKey() + indentstr2 + line.getValue() + lineSeparator);
buffer.append(indentstr2 + line.getKey() + indentstr2 + (line.getValue() +offset_total) + lineSeparator);
}
buffer.append(indentstr1 + "}" + lineSeparator);
is_first_method = false;

View File

@ -778,13 +778,13 @@ public class ExprProcessor implements CodeConstants {
buf.append(" label").append(edge.closure.id);
}
buf.append(";").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
}
if (buf.length() == 0 && semicolon) {
buf.append(InterpreterUtil.getIndentString(indent)).append(";").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
return buf.toString();
@ -828,7 +828,7 @@ public class ExprProcessor implements CodeConstants {
buf.append(";");
}
buf.append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
}

View File

@ -124,7 +124,7 @@ public class CatchAllStatement extends Statement {
boolean labeled = isLabeled();
if (labeled) {
buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
List<StatEdge> lstSuccs = first.getSuccessorEdges(STATEDGE_DIRECT_ALL);
@ -137,30 +137,30 @@ public class CatchAllStatement extends Statement {
}
else {
buf.append(indstr).append("try {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer));
buf.append(indstr).append("}");
}
buf.append(isFinally ? " finally" :
" catch (" + vars.get(0).toJava(indent, tracer) + ")").append(" {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
if (monitor != null) {
indstr1 = InterpreterUtil.getIndentString(indent + 1);
buf.append(indstr1).append("if(").append(monitor.toJava(indent, tracer)).append(") {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
buf.append(ExprProcessor.jmpWrapper(handler, indent + 1 + (monitor != null ? 1 : 0), true, tracer));
if (monitor != null) {
buf.append(indstr1).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
return buf.toString();
}

View File

@ -160,11 +160,11 @@ public class CatchStatement extends Statement {
if (isLabeled()) {
buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
buf.append(indstr).append("try {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer));
buf.append(indstr).append("}");
@ -183,14 +183,14 @@ public class CatchStatement extends Statement {
}
buf.append(vars.get(i - 1).toJava(indent, tracer));
buf.append(") {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(ExprProcessor.jmpWrapper(stats.get(i), indent + 1, true, tracer)).append(indstr)
.append("}");
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
buf.append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
return buf.toString();
}

View File

@ -102,39 +102,39 @@ public class DoStatement extends Statement {
if (isLabeled()) {
buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
switch (looptype) {
case LOOP_DO:
buf.append(indstr).append("while(true) {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer));
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
break;
case LOOP_DOWHILE:
buf.append(indstr).append("do {").append(new_line_separator);
tracer.incrementSourceLine();
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);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
break;
case LOOP_WHILE:
buf.append(indstr).append("while(").append(conditionExprent.get(0).toJava(indent, tracer)).append(") {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer));
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
break;
case LOOP_FOR:
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.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, true, tracer));
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
return buf.toString();

View File

@ -211,11 +211,11 @@ public class IfStatement extends Statement {
if (isLabeled()) {
buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
buf.append(indstr).append(headexprent.get(0).toJava(indent, tracer)).append(" {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
if (ifstat == null) {
buf.append(InterpreterUtil.getIndentString(indent + 1));
@ -235,7 +235,7 @@ public class IfStatement extends Statement {
}
}
buf.append(";").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
else {
buf.append(ExprProcessor.jmpWrapper(ifstat, indent + 1, true, tracer));
@ -258,11 +258,15 @@ public class IfStatement extends Statement {
elseif = true;
}
else {
String content = ExprProcessor.jmpWrapper(elsestat, indent + 1, false, tracer);
BytecodeMappingTracer else_tracer = new BytecodeMappingTracer(tracer.getCurrentSourceLine());
String content = ExprProcessor.jmpWrapper(elsestat, indent + 1, false, else_tracer);
if (content.length() > 0) {
buf.append(indstr).append("} else {").append(new_line_separator);
tracer.incrementSourceLine(); // FIXME: wrong order
else_tracer.shiftSourceLines(1);
tracer.setCurrentSourceLine(else_tracer.getCurrentSourceLine() + 1);
tracer.addTracer(else_tracer);
buf.append(content);
}
@ -271,7 +275,7 @@ public class IfStatement extends Statement {
if (!elseif) {
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
return buf.toString();

View File

@ -114,7 +114,7 @@ public class SequenceStatement extends Statement {
indstr = InterpreterUtil.getIndentString(indent);
indent++;
buf.append(indstr).append("label").append(this.id).append(": {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
boolean notempty = false;
@ -125,7 +125,7 @@ public class SequenceStatement extends Statement {
if (i > 0 && notempty) {
buf.append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
String str = ExprProcessor.jmpWrapper(st, indent, false, tracer);
@ -136,7 +136,7 @@ public class SequenceStatement extends Statement {
if (islabeled) {
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
return buf.toString();

View File

@ -119,11 +119,11 @@ public class SwitchStatement extends Statement {
if (isLabeled()) {
buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
buf.append(indstr).append(headexprent.get(0).toJava(indent, tracer)).append(" {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
VarType switch_type = headexprent.get(0).getExprType();
@ -136,14 +136,14 @@ public class SwitchStatement extends Statement {
for (int j = 0; j < edges.size(); j++) {
if (edges.get(j) == default_edge) {
buf.append(indstr).append("default:").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
else {
ConstExprent value = (ConstExprent)values.get(j).copy();
value.setConsttype(switch_type);
buf.append(indstr).append("case ").append(value.toJava(indent, tracer)).append(":").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
}
@ -151,7 +151,7 @@ public class SwitchStatement extends Statement {
}
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
return buf.toString();
}

View File

@ -80,17 +80,17 @@ public class SynchronizedStatement extends Statement {
if (isLabeled()) {
buf.append(indstr).append("label").append(this.id).append(":").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
}
buf.append(indstr).append(headexprent.get(0).toJava(indent, tracer)).append(" {").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(ExprProcessor.jmpWrapper(body, indent + 1, true, tracer));
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
buf.append(indstr).append("}").append(new_line_separator);
tracer.incrementSourceLine();
tracer.incrementCurrentSourceLine();
return buf.toString();
}