2014-10-17 12:19:39 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2000-2014 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.
|
|
|
|
*/
|
2014-10-01 01:00:11 -04:00
|
|
|
package org.jetbrains.java.decompiler.main.collectors;
|
|
|
|
|
2014-10-15 00:26:49 -04:00
|
|
|
import org.jetbrains.java.decompiler.main.DecompilerContext;
|
|
|
|
import org.jetbrains.java.decompiler.main.TextBuffer;
|
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2014-10-01 01:00:11 -04:00
|
|
|
public class BytecodeSourceMapper {
|
|
|
|
|
|
|
|
private int offset_total;
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-01 01:00:11 -04:00
|
|
|
// class, method, bytecode offset, source line
|
2014-10-17 12:19:39 -04:00
|
|
|
private final Map<String, Map<String, Map<Integer, Integer>>> mapping = new LinkedHashMap<String, Map<String, Map<Integer, Integer>>>();
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
// original line to decompiled line
|
|
|
|
private final Map<Integer, Integer> linesMapping = new LinkedHashMap<Integer, Integer>();
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
public void addMapping(String className, String methodName, int bytecodeOffset, int sourceLine) {
|
|
|
|
Map<String, Map<Integer, Integer>> class_mapping = mapping.get(className);
|
|
|
|
if (class_mapping == null) {
|
|
|
|
mapping.put(className, class_mapping = new LinkedHashMap<String, Map<Integer, Integer>>()); // need to preserve order
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
Map<Integer, Integer> method_mapping = class_mapping.get(methodName);
|
|
|
|
if (method_mapping == null) {
|
|
|
|
class_mapping.put(methodName, method_mapping = new HashMap<Integer, Integer>());
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-01 01:00:11 -04:00
|
|
|
// don't overwrite
|
2014-10-17 12:19:39 -04:00
|
|
|
if (!method_mapping.containsKey(bytecodeOffset)) {
|
|
|
|
method_mapping.put(bytecodeOffset, sourceLine);
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
public void addTracer(String className, String methodName, BytecodeMappingTracer tracer) {
|
|
|
|
for (Entry<Integer, Integer> entry : tracer.getMapping().entrySet()) {
|
|
|
|
addMapping(className, methodName, entry.getKey(), entry.getValue());
|
2014-10-05 22:27:26 -04:00
|
|
|
}
|
2014-10-17 12:19:39 -04:00
|
|
|
linesMapping.putAll(tracer.getOriginalLinesMapping());
|
2014-10-05 22:27:26 -04:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:26:49 -04:00
|
|
|
public void dumpMapping(TextBuffer buffer, boolean offsetsToHex) {
|
2014-10-01 01:00:11 -04:00
|
|
|
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
for (Entry<String, Map<String, Map<Integer, Integer>>> class_entry : mapping.entrySet()) {
|
|
|
|
Map<String, Map<Integer, Integer>> class_mapping = class_entry.getValue();
|
|
|
|
buffer.append("class '" + class_entry.getKey() + "' {" + lineSeparator);
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-01 01:00:11 -04:00
|
|
|
boolean is_first_method = true;
|
2014-10-17 12:19:39 -04:00
|
|
|
for (Entry<String, Map<Integer, Integer>> method_entry : class_mapping.entrySet()) {
|
|
|
|
Map<Integer, Integer> method_mapping = method_entry.getValue();
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
if (!is_first_method) {
|
2014-10-07 13:03:09 -04:00
|
|
|
buffer.appendLineSeparator();
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|
2014-10-17 12:19:39 -04:00
|
|
|
|
|
|
|
buffer.appendIndent(1).append("method '" + method_entry.getKey() + "' {" + lineSeparator);
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-15 02:35:45 -04:00
|
|
|
List<Integer> lstBytecodeOffsets = new ArrayList<Integer>(method_mapping.keySet());
|
|
|
|
Collections.sort(lstBytecodeOffsets);
|
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
for (Integer offset : lstBytecodeOffsets) {
|
2014-10-15 02:35:45 -04:00
|
|
|
Integer line = method_mapping.get(offset);
|
|
|
|
|
2014-10-17 12:19:39 -04:00
|
|
|
String strOffset = offsetsToHex ? Integer.toHexString(offset) : line.toString();
|
2014-10-15 02:35:45 -04:00
|
|
|
buffer.appendIndent(2).append(strOffset).appendIndent(2).append((line + offset_total) + lineSeparator);
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|
2014-10-07 13:03:09 -04:00
|
|
|
buffer.appendIndent(1).append("}").appendLineSeparator();
|
2014-10-17 12:19:39 -04:00
|
|
|
|
2014-10-01 01:00:11 -04:00
|
|
|
is_first_method = false;
|
|
|
|
}
|
2014-10-17 12:19:39 -04:00
|
|
|
|
|
|
|
buffer.append("}").appendLineSeparator().appendLineSeparator();
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|
2014-10-17 10:52:47 -04:00
|
|
|
|
|
|
|
// lines mapping
|
|
|
|
buffer.append("Lines mapping:").appendLineSeparator();
|
|
|
|
int[] mapping = getOriginalLinesMapping();
|
2014-10-17 12:19:39 -04:00
|
|
|
for (int i = 0; i < mapping.length; i += 2) {
|
|
|
|
buffer.append(mapping[i]).append(" <-> ").append(mapping[i + 1]).appendLineSeparator();
|
2014-10-17 10:52:47 -04:00
|
|
|
}
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|
2014-10-05 22:27:26 -04:00
|
|
|
|
2014-10-01 01:00:11 -04:00
|
|
|
public int getTotalOffset() {
|
|
|
|
return offset_total;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTotalOffset(int offset_total) {
|
|
|
|
this.offset_total = offset_total;
|
|
|
|
}
|
2014-10-05 22:27:26 -04:00
|
|
|
|
|
|
|
public void addTotalOffset(int offset_total) {
|
|
|
|
this.offset_total += offset_total;
|
|
|
|
}
|
|
|
|
|
2014-10-17 10:52:47 -04:00
|
|
|
/**
|
2014-10-17 12:19:39 -04:00
|
|
|
* Original to decompiled line mapping.
|
2014-10-17 10:52:47 -04:00
|
|
|
*/
|
|
|
|
public int[] getOriginalLinesMapping() {
|
2014-10-17 12:19:39 -04:00
|
|
|
int[] res = new int[linesMapping.size() * 2];
|
2014-10-17 10:52:47 -04:00
|
|
|
int i = 0;
|
2014-10-17 12:19:39 -04:00
|
|
|
for (Entry<Integer, Integer> entry : linesMapping.entrySet()) {
|
2014-10-17 10:52:47 -04:00
|
|
|
res[i] = entry.getKey();
|
2014-10-17 12:19:39 -04:00
|
|
|
res[i + 1] = entry.getValue() + offset_total + 1; // make it 1 based
|
|
|
|
i += 2;
|
2014-10-17 10:52:47 -04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2014-10-01 01:00:11 -04:00
|
|
|
}
|