decompiler: map dummy return line to the method closing bracket

This commit is contained in:
Egor.Ushakov
2015-03-24 17:56:33 +03:00
parent 07e1d66a53
commit 500f8b12d8
34 changed files with 565 additions and 128 deletions

View File

@@ -534,8 +534,8 @@ public class ClassWriter {
indent += 1;
}
RootStatement root = classWrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root;
if (!methodWrapper.decompiledWithErrors) {
RootStatement root = classWrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()).root;
if (root != null) { // check for existence
try {
buffer.append(root.toJava(indent, tracer));
@@ -553,12 +553,13 @@ public class ClassWriter {
buffer.appendLineSeparator();
}
if (root != null) {
tracer.addMapping(root.getDummyExit().bytecode);
}
if (!codeOnly) {
indent -= 1;
buffer.appendIndent(indent);
buffer.append('}');
buffer.appendLineSeparator();
buffer.appendIndent(indent).append('}').appendLineSeparator();
}
}
finally {
@@ -869,6 +870,9 @@ public class ClassWriter {
tracer.incrementCurrentSourceLine();
}
if (root != null) {
tracer.addMapping(root.getDummyExit().bytecode);
}
buffer.appendIndent(indent).append('}').appendLineSeparator();
tracer.incrementCurrentSourceLine();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -89,6 +89,12 @@ public class BytecodeMappingTracer {
this.lineNumberTable = lineNumberTable;
}
private final Set<Integer> unmappedLines = new HashSet<Integer>();
public Set<Integer> getUnmappedLines() {
return unmappedLines;
}
public Map<Integer, Integer> getOriginalLinesMapping() {
if (lineNumberTable == null) {
return Collections.emptyMap();
@@ -103,11 +109,15 @@ public class BytecodeMappingTracer {
if (newLine != null) {
res.put(originalLine, newLine);
}
else {
unmappedLines.add(originalLine);
}
}
for (Entry<Integer, Integer> entry : mapping.entrySet()) {
int originalLine = lineNumberTable.findLineNumber(entry.getKey());
if (originalLine > -1) {
res.put(originalLine, entry.getValue());
unmappedLines.remove(originalLine);
}
}
return res;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ public class BytecodeSourceMapper {
// original line to decompiled line
private final Map<Integer, Integer> linesMapping = new HashMap<Integer, Integer>();
private final Set<Integer> unmappedLines = new TreeSet<Integer>();
public void addMapping(String className, String methodName, int bytecodeOffset, int sourceLine) {
Map<String, Map<Integer, Integer>> class_mapping = mapping.get(className);
@@ -53,6 +54,7 @@ public class BytecodeSourceMapper {
addMapping(className, methodName, entry.getKey(), entry.getValue());
}
linesMapping.putAll(tracer.getOriginalLinesMapping());
unmappedLines.addAll(tracer.getUnmappedLines());
}
public void dumpMapping(TextBuffer buffer, boolean offsetsToHex) {
@@ -97,7 +99,16 @@ public class BytecodeSourceMapper {
buffer.append("Lines mapping:").appendLineSeparator();
Map<Integer, Integer> sorted = new TreeMap<Integer, Integer>(linesMapping);
for (Entry<Integer, Integer> entry : sorted.entrySet()) {
buffer.append(entry.getKey()).append(" <-> ").append(entry.getValue()+ offset_total + 1).appendLineSeparator();
buffer.append(entry.getKey()).append(" <-> ").append(entry.getValue() + offset_total + 1).appendLineSeparator();
}
if (!unmappedLines.isEmpty()) {
buffer.append("Not mapped:").appendLineSeparator();
for (Integer line : unmappedLines) {
if (!linesMapping.containsKey(line)) {
buffer.append(line).appendLineSeparator();
}
}
}
}
@@ -121,6 +132,7 @@ public class BytecodeSourceMapper {
int i = 0;
for (Entry<Integer, Integer> entry : linesMapping.entrySet()) {
res[i] = entry.getKey();
unmappedLines.remove(entry.getKey());
res[i + 1] = entry.getValue() + offset_total + 1; // make it 1 based
i += 2;
}