decompiler: build original lines mapping
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package org.jetbrains.java.decompiler.main.collectors;
|
||||
|
||||
import org.jetbrains.java.decompiler.struct.attr.StructLineNumberTableAttribute;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -8,6 +12,8 @@ public class BytecodeMappingTracer {
|
||||
|
||||
private int current_sourceline;
|
||||
|
||||
private StructLineNumberTableAttribute myLineNumberTable = null;
|
||||
|
||||
// bytecode offset, source line
|
||||
private HashMap<Integer, Integer> mapping = new HashMap<Integer, Integer>();
|
||||
|
||||
@@ -67,4 +73,30 @@ public class BytecodeMappingTracer {
|
||||
this.current_sourceline = current_sourceline;
|
||||
}
|
||||
|
||||
public void setLineNumberTable(StructLineNumberTableAttribute lineNumberTable) {
|
||||
myLineNumberTable = lineNumberTable;
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> getOriginalLinesMapping() {
|
||||
if (myLineNumberTable == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
HashMap<Integer, Integer> res = new HashMap<Integer, Integer>();
|
||||
int[] data = myLineNumberTable.getRawData();
|
||||
for (int i = 0; i < data.length; i+=2) {
|
||||
int originalOffset = data[i];
|
||||
int originalLine = data[i+1];
|
||||
Integer newLine = mapping.get(originalOffset);
|
||||
if (newLine != null) {
|
||||
res.put(originalLine, newLine);
|
||||
}
|
||||
}
|
||||
for (Entry<Integer, Integer> entry : mapping.entrySet()) {
|
||||
int originalLine = myLineNumberTable.findLineNumber(entry.getKey());
|
||||
if (originalLine > -1) {
|
||||
res.put(originalLine, entry.getValue());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ public class BytecodeSourceMapper {
|
||||
|
||||
private int offset_total;
|
||||
|
||||
private final HashMap<Integer, Integer> myOriginalLinesMapping = new HashMap<Integer, Integer>();
|
||||
|
||||
// class, method, bytecode offset, source line
|
||||
private final HashMap<String, HashMap<String, HashMap<Integer, Integer>>> mapping = new LinkedHashMap<String, HashMap<String, HashMap<Integer, Integer>>>(); // need to preserve order
|
||||
|
||||
@@ -35,6 +37,7 @@ public class BytecodeSourceMapper {
|
||||
for(Entry<Integer, Integer> entry : tracer.getMapping().entrySet()) {
|
||||
addMapping(classname, methodname, entry.getKey(), entry.getValue());
|
||||
}
|
||||
myOriginalLinesMapping.putAll(tracer.getOriginalLinesMapping());
|
||||
}
|
||||
|
||||
public void dumpMapping(TextBuffer buffer, boolean offsetsToHex) {
|
||||
@@ -69,6 +72,13 @@ public class BytecodeSourceMapper {
|
||||
}
|
||||
buffer.append("}").appendLineSeparator();
|
||||
}
|
||||
|
||||
// lines mapping
|
||||
buffer.append("Lines mapping:").appendLineSeparator();
|
||||
int[] mapping = getOriginalLinesMapping();
|
||||
for (int i = 0; i < mapping.length; i+=2) {
|
||||
buffer.append(mapping[i]).append(" <-> ").append(mapping[i+1]).appendLineSeparator();
|
||||
}
|
||||
}
|
||||
|
||||
public int getTotalOffset() {
|
||||
@@ -83,5 +93,17 @@ public class BytecodeSourceMapper {
|
||||
this.offset_total += offset_total;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* original to our line mapping
|
||||
*/
|
||||
public int[] getOriginalLinesMapping() {
|
||||
int[] res = new int[myOriginalLinesMapping.size()*2];
|
||||
int i = 0;
|
||||
for (Entry<Integer, Integer> entry : myOriginalLinesMapping.entrySet()) {
|
||||
res[i] = entry.getKey();
|
||||
res[i+1] = entry.getValue() + offset_total + 1; // make it 1 based
|
||||
i+=2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user