Propagated bytecode-to-source tracer
This commit is contained in:
@@ -164,7 +164,8 @@ public class ClassWriter {
|
||||
ClassNode outerNode = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
|
||||
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, node);
|
||||
|
||||
BytecodeMappingTracer tracer = new BytecodeMappingTracer();
|
||||
int total_offset_lines = 0;
|
||||
BytecodeMappingTracer dummy_tracer = new BytecodeMappingTracer();
|
||||
|
||||
try {
|
||||
// last minute processing
|
||||
@@ -177,8 +178,13 @@ public class ClassWriter {
|
||||
|
||||
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
||||
|
||||
// write class definition
|
||||
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;
|
||||
|
||||
boolean hasContent = false;
|
||||
|
||||
// fields
|
||||
@@ -204,7 +210,7 @@ public class ClassWriter {
|
||||
enumFields = false;
|
||||
}
|
||||
|
||||
fieldToJava(wrapper, cl, fd, buffer, indent + 1, tracer);
|
||||
fieldToJava(wrapper, cl, fd, buffer, indent + 1, dummy_tracer); // FIXME: insert real tracer
|
||||
|
||||
hasContent = true;
|
||||
}
|
||||
@@ -225,9 +231,13 @@ public class ClassWriter {
|
||||
if (hasContent) {
|
||||
buffer.append(lineSeparator);
|
||||
}
|
||||
boolean methodSkipped = !methodToJava(node, mt, buffer, indent + 1, tracer);
|
||||
BytecodeMappingTracer method_tracer = new BytecodeMappingTracer(total_offset_lines);
|
||||
boolean methodSkipped = !methodToJava(node, mt, buffer, indent + 1, method_tracer);
|
||||
if (!methodSkipped) {
|
||||
hasContent = true;
|
||||
DecompilerContext.getBytecodeSourceMapper().addTracer(cl.qualifiedName,
|
||||
InterpreterUtil.makeUniqueKey(mt.getName(), mt.getDescriptor()), method_tracer);
|
||||
total_offset_lines = method_tracer.getCurrentSourceline();
|
||||
}
|
||||
else {
|
||||
buffer.setLength(position);
|
||||
@@ -558,6 +568,10 @@ public class ClassWriter {
|
||||
MethodWrapper methodWrapper = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor());
|
||||
|
||||
boolean hideMethod = false;
|
||||
int start_index_method = buffer.length();
|
||||
|
||||
String indentString = InterpreterUtil.getIndentString(indent);
|
||||
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
||||
|
||||
MethodWrapper outerWrapper = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
|
||||
DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, methodWrapper);
|
||||
@@ -569,9 +583,6 @@ public class ClassWriter {
|
||||
boolean isDeprecated = mt.getAttributes().containsKey("Deprecated");
|
||||
boolean clinit = false, init = false, dinit = false;
|
||||
|
||||
String indentString = InterpreterUtil.getIndentString(indent);
|
||||
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
||||
|
||||
MethodDescriptor md = MethodDescriptor.parseDescriptor(mt.getDescriptor());
|
||||
|
||||
int flags = mt.getAccessFlags();
|
||||
@@ -793,6 +804,9 @@ public class ClassWriter {
|
||||
|
||||
if (root != null && !methodWrapper.decompiledWithErrors) { // check for existence
|
||||
try {
|
||||
|
||||
tracer.setCurrentSourceline(buffer.substring(start_index_method).split(lineSeparator, -1).length - 1);
|
||||
|
||||
String code = root.toJava(indent + 1, tracer);
|
||||
|
||||
hideMethod = (clinit || dinit || hideConstructor(wrapper, init, throwsExceptions, paramCount)) && code.length() == 0;
|
||||
@@ -820,6 +834,10 @@ public class ClassWriter {
|
||||
DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, outerWrapper);
|
||||
}
|
||||
|
||||
// save total lines
|
||||
// TODO: optimize
|
||||
tracer.setCurrentSourceline(buffer.substring(start_index_method).split(lineSeparator, -1).length - 1);
|
||||
|
||||
return !hideMethod;
|
||||
}
|
||||
|
||||
|
||||
@@ -262,10 +262,13 @@ public class ClassesProcessor {
|
||||
new ClassWriter().classToJava(root, classBuffer, 0);
|
||||
|
||||
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
||||
int total_offset_lines = 0;
|
||||
|
||||
int index = cl.qualifiedName.lastIndexOf("/");
|
||||
if (index >= 0) {
|
||||
total_offset_lines++;
|
||||
String packageName = cl.qualifiedName.substring(0, index).replace('/', '.');
|
||||
|
||||
buffer.append("package ");
|
||||
buffer.append(packageName);
|
||||
buffer.append(";");
|
||||
@@ -273,15 +276,20 @@ public class ClassesProcessor {
|
||||
buffer.append(lineSeparator);
|
||||
}
|
||||
|
||||
if (importCollector.writeImports(buffer)) {
|
||||
int import_lines_written = importCollector.writeImports(buffer);
|
||||
if (import_lines_written > 0) {
|
||||
buffer.append(lineSeparator);
|
||||
total_offset_lines += import_lines_written + 1;
|
||||
}
|
||||
|
||||
buffer.append(classBuffer);
|
||||
|
||||
if(DecompilerContext.getOption(IFernflowerPreferences.BYTECODE_SOURCE_MAPPING)) {
|
||||
BytecodeSourceMapper mapper = DecompilerContext.getBytecodeSourceMapper();
|
||||
mapper.addTotalOffset(total_offset_lines);
|
||||
|
||||
buffer.append(lineSeparator);
|
||||
DecompilerContext.getBytecodeSourceMapper().dumpMapping(classBuffer);
|
||||
mapper.dumpMapping(buffer);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
|
||||
@@ -1,26 +1,53 @@
|
||||
package org.jetbrains.java.decompiler.main.collectors;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
public class BytecodeMappingTracer {
|
||||
|
||||
private int current_sourceline;
|
||||
|
||||
|
||||
// bytecode offset, source line
|
||||
private HashMap<Integer, Integer> mapping;
|
||||
|
||||
private HashMap<Integer, Integer> mapping = new HashMap<Integer, Integer>();
|
||||
|
||||
public BytecodeMappingTracer() {}
|
||||
|
||||
public BytecodeMappingTracer(int initial_source_line) {
|
||||
current_sourceline = initial_source_line;
|
||||
}
|
||||
|
||||
public void incrementSourceLine() {
|
||||
current_sourceline++;
|
||||
}
|
||||
|
||||
|
||||
public void incrementSourceLine(int number_lines) {
|
||||
current_sourceline += number_lines;
|
||||
}
|
||||
|
||||
public void addMapping(int bytecode_offset) {
|
||||
if(!mapping.containsKey(bytecode_offset)) {
|
||||
mapping.put(bytecode_offset, current_sourceline);
|
||||
}
|
||||
}
|
||||
|
||||
public void addMapping(Set<Integer> bytecode_offsets) {
|
||||
if(bytecode_offsets != null) {
|
||||
for(Integer bytecode_offset : bytecode_offsets) {
|
||||
addMapping(bytecode_offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<Integer, Integer> getMapping() {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int getCurrentSourceline() {
|
||||
return current_sourceline;
|
||||
}
|
||||
|
||||
public void setCurrentSourceline(int current_sourceline) {
|
||||
this.current_sourceline = current_sourceline;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,59 +9,65 @@ import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
||||
public class BytecodeSourceMapper {
|
||||
|
||||
private int offset_total;
|
||||
|
||||
|
||||
// class, method, bytecode offset, source line
|
||||
private HashMap<String, HashMap<String, HashMap<Integer, Integer>>> mapping;
|
||||
|
||||
private HashMap<String, HashMap<String, HashMap<Integer, Integer>>> mapping = new HashMap<String, HashMap<String, HashMap<Integer, Integer>>>();
|
||||
|
||||
public void addMapping(String classname, String methodname, int bytecode_offset, int source_line) {
|
||||
|
||||
|
||||
HashMap<String, HashMap<Integer, Integer>> class_mapping = mapping.get(classname);
|
||||
if(class_mapping == null) {
|
||||
mapping.put(classname, class_mapping = new HashMap<String, HashMap<Integer, Integer>>());
|
||||
}
|
||||
|
||||
|
||||
HashMap<Integer, Integer> method_mapping = class_mapping.get(methodname);
|
||||
if(method_mapping == null) {
|
||||
class_mapping.put(methodname, method_mapping = new HashMap<Integer, Integer>());
|
||||
}
|
||||
|
||||
|
||||
// don't overwrite
|
||||
if(!method_mapping.containsKey(bytecode_offset)) {
|
||||
method_mapping.put(bytecode_offset, source_line);
|
||||
}
|
||||
}
|
||||
|
||||
public void addTracer(String classname, String methodname, BytecodeMappingTracer tracer) {
|
||||
for(Entry<Integer, Integer> entry : tracer.getMapping().entrySet()) {
|
||||
addMapping(classname, methodname, entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void dumpMapping(StringBuilder buffer) {
|
||||
|
||||
|
||||
String lineSeparator = DecompilerContext.getNewLineSeparator();
|
||||
String indentstr1 = InterpreterUtil.getIndentString(1);
|
||||
String indentstr2 = InterpreterUtil.getIndentString(2);
|
||||
|
||||
|
||||
|
||||
for(Entry<String, HashMap<String, HashMap<Integer, Integer>>> class_entry : mapping.entrySet()) {
|
||||
HashMap<String, HashMap<Integer, Integer>> class_mapping = class_entry.getValue();
|
||||
buffer.append("class " + class_entry.getKey() + "{" + lineSeparator);
|
||||
|
||||
|
||||
boolean is_first_method = true;
|
||||
|
||||
|
||||
for(Entry<String, HashMap<Integer, Integer>> method_entry : class_mapping.entrySet()) {
|
||||
HashMap<Integer, Integer> method_mapping = method_entry.getValue();
|
||||
|
||||
|
||||
if(!is_first_method) {
|
||||
buffer.append(lineSeparator);
|
||||
}
|
||||
buffer.append(indentstr1 + "method " + method_entry.getKey() + "{" + lineSeparator);
|
||||
|
||||
|
||||
for(Entry<Integer, Integer> line : method_mapping.entrySet()) {
|
||||
buffer.append(indentstr2 + line.getKey() + indentstr2 + line.getValue() + lineSeparator);
|
||||
}
|
||||
buffer.append(indentstr1 + "}" + lineSeparator);
|
||||
is_first_method = false;
|
||||
}
|
||||
buffer.append("}" + lineSeparator);
|
||||
buffer.append("}" + lineSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int getTotalOffset() {
|
||||
return offset_total;
|
||||
}
|
||||
@@ -69,7 +75,10 @@ public class BytecodeSourceMapper {
|
||||
public void setTotalOffset(int offset_total) {
|
||||
this.offset_total = offset_total;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void addTotalOffset(int offset_total) {
|
||||
this.offset_total += offset_total;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -107,17 +107,23 @@ public class ImportCollector {
|
||||
return retname == null ? nshort : retname;
|
||||
}
|
||||
|
||||
public boolean writeImports(StringBuilder buffer) {
|
||||
public int writeImports(StringBuilder buffer) {
|
||||
|
||||
int importlines_written = 0;
|
||||
String new_line_separator = DecompilerContext.getNewLineSeparator();
|
||||
|
||||
List<String> imports = packImports();
|
||||
|
||||
for (String s : imports) {
|
||||
buffer.append("import ");
|
||||
buffer.append(s);
|
||||
buffer.append(";");
|
||||
buffer.append(DecompilerContext.getNewLineSeparator());
|
||||
buffer.append(new_line_separator);
|
||||
|
||||
importlines_written++;
|
||||
}
|
||||
|
||||
return imports.size() > 0;
|
||||
return importlines_written;
|
||||
}
|
||||
|
||||
private List<String> packImports() {
|
||||
|
||||
Reference in New Issue
Block a user