From 9f9da912f666dc2d25b7ba7e03283f13b4c5b4bf Mon Sep 17 00:00:00 2001 From: Stiver Date: Tue, 7 Oct 2014 07:31:22 +0200 Subject: Fixed some errors in line counting --- .../java/decompiler/main/ClassWriter.java | 13 ++++++----- .../java/decompiler/main/ClassesProcessor.java | 3 ++- .../main/collectors/BytecodeMappingTracer.java | 25 ++++++++++++++++++---- .../main/collectors/BytecodeSourceMapper.java | 2 +- .../modules/decompiler/ExprProcessor.java | 6 +++--- .../decompiler/stats/CatchAllStatement.java | 12 +++++------ .../modules/decompiler/stats/CatchStatement.java | 10 ++++----- .../modules/decompiler/stats/DoStatement.java | 18 ++++++++-------- .../modules/decompiler/stats/IfStatement.java | 16 ++++++++------ .../decompiler/stats/SequenceStatement.java | 6 +++--- .../modules/decompiler/stats/SwitchStatement.java | 10 ++++----- .../decompiler/stats/SynchronizedStatement.java | 8 +++---- 12 files changed, 77 insertions(+), 52 deletions(-) (limited to 'src/org') diff --git a/src/org/jetbrains/java/decompiler/main/ClassWriter.java b/src/org/jetbrains/java/decompiler/main/ClassWriter.java index 830e668..0383dd4 100644 --- a/src/org/jetbrains/java/decompiler/main/ClassWriter.java +++ b/src/org/jetbrains/java/decompiler/main/ClassWriter.java @@ -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; } diff --git a/src/org/jetbrains/java/decompiler/main/ClassesProcessor.java b/src/org/jetbrains/java/decompiler/main/ClassesProcessor.java index bab6976..c79e355 100644 --- a/src/org/jetbrains/java/decompiler/main/ClassesProcessor.java +++ b/src/org/jetbrains/java/decompiler/main/ClassesProcessor.java @@ -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); diff --git a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeMappingTracer.java b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeMappingTracer.java index 95321e5..20af427 100644 --- a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeMappingTracer.java +++ b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeMappingTracer.java @@ -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 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 entry : tracer.mapping.entrySet()) { + if(!mapping.containsKey(entry.getKey())) { + mapping.put(entry.getKey(), entry.getValue()); + } + } + } + } + public HashMap 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; } diff --git a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java index f31394b..53f92b2 100644 --- a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java +++ b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java @@ -59,7 +59,7 @@ public class BytecodeSourceMapper { buffer.append(indentstr1 + "method " + method_entry.getKey() + "{" + lineSeparator); for(Entry 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; diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java index f4b7aa2..dc28485 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java @@ -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(); } } 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 9ab7ec2..f22d1b1 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchAllStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchAllStatement.java @@ -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 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(); } 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 7162f15..a096258 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/CatchStatement.java @@ -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(); } 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 299659a..1ddce1b 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/DoStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/DoStatement.java @@ -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(); 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 343d99e..ebe2bd7 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/IfStatement.java @@ -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(); diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SequenceStatement.java b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SequenceStatement.java index 24152f3..38420e0 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SequenceStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SequenceStatement.java @@ -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(); 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 3b75fdc..aea44de 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SwitchStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SwitchStatement.java @@ -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(); } 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 3d61198..eb94ddc 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SynchronizedStatement.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/stats/SynchronizedStatement.java @@ -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(); } -- cgit v1.2.3