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;
}

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.
@@ -46,8 +46,7 @@ public class DomHelper {
// head statement
Statement firstst = stats.getWithKey(firstblock.id);
// dummy exit statement
Statement dummyexit = new Statement();
dummyexit.type = Statement.TYPE_DUMMYEXIT;
DummyExitStatement dummyexit = new DummyExitStatement();
Statement general;
if (stats.size() > 1 || firstblock.isSuccessor(firstblock)) { // multiple basic blocks or an infinite loop of one block

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.
@@ -247,7 +247,9 @@ public class ExitHelper {
boolean res = false;
for (StatEdge edge : root.getDummyExit().getAllPredecessorEdges()) {
DummyExitStatement dummyExit = root.getDummyExit();
for (StatEdge edge : dummyExit.getAllPredecessorEdges()) {
if (!edge.explicit) {
Statement source = edge.getSource();
List<Exprent> lstExpr = source.getExprents();
@@ -257,6 +259,7 @@ public class ExitHelper {
ExitExprent ex = (ExitExprent)expr;
if (ex.getExitType() == ExitExprent.EXIT_RETURN && ex.getValue() == null) {
// remove redundant return
dummyExit.addBytecodeOffsets(ex.bytecode);
lstExpr.remove(lstExpr.size() - 1);
res = true;
}

View File

@@ -0,0 +1,42 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.java.decompiler.modules.decompiler.stats;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* @author egor
*/
public class DummyExitStatement extends Statement {
public Set<Integer> bytecode = null; // offsets of bytecode instructions mapped to dummy exit
public DummyExitStatement() {
type = Statement.TYPE_DUMMYEXIT;
}
public void addBytecodeOffsets(Collection<Integer> bytecodeOffsets) {
if (bytecodeOffsets != null && !bytecodeOffsets.isEmpty()) {
if (bytecode == null) {
bytecode = new HashSet<Integer>(bytecodeOffsets);
}
else {
bytecode.addAll(bytecodeOffsets);
}
}
}
}

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.
@@ -22,9 +22,9 @@ import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
public class RootStatement extends Statement {
private Statement dummyExit;
private DummyExitStatement dummyExit;
public RootStatement(Statement head, Statement dummyExit) {
public RootStatement(Statement head, DummyExitStatement dummyExit) {
type = Statement.TYPE_ROOT;
@@ -39,11 +39,11 @@ public class RootStatement extends Statement {
return ExprProcessor.listToJava(varDefinitions, indent, tracer).append(first.toJava(indent, tracer));
}
public Statement getDummyExit() {
public DummyExitStatement getDummyExit() {
return dummyExit;
}
public void setDummyExit(Statement dummyExit) {
public void setDummyExit(DummyExitStatement dummyExit) {
this.dummyExit = dummyExit;
}
}