Propagated bytecode-to-source tracer

master
Stiver 10 years ago
parent 0c8508ff8a
commit 9723ab4475
  1. 8
      src/org/jetbrains/java/decompiler/code/cfg/BasicBlock.java
  2. 30
      src/org/jetbrains/java/decompiler/main/ClassWriter.java
  3. 12
      src/org/jetbrains/java/decompiler/main/ClassesProcessor.java
  4. 29
      src/org/jetbrains/java/decompiler/main/collectors/BytecodeMappingTracer.java
  5. 11
      src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java
  6. 12
      src/org/jetbrains/java/decompiler/main/collectors/ImportCollector.java
  7. 1
      src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java
  8. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/ArrayExprent.java
  9. 3
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/AssertExprent.java
  10. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/AssignmentExprent.java
  11. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java
  12. 3
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/ExitExprent.java
  13. 3
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/Exprent.java
  14. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/FieldExprent.java
  15. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/FunctionExprent.java
  16. 1
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/IfExprent.java
  17. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java
  18. 3
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/MonitorExprent.java
  19. 1
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/SwitchExprent.java
  20. 2
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/VarExprent.java
  21. 6
      src/org/jetbrains/java/decompiler/modules/decompiler/stats/Statement.java

@ -93,6 +93,14 @@ public class BasicBlock implements IGraphNode {
} }
} }
public Integer getOldOffset(int index) {
if(index < instrOldOffsets.size()) {
return instrOldOffsets.get(index);
} else {
return -1;
}
}
public int size() { public int size() {
return seq.length(); return seq.length();
} }

@ -164,7 +164,8 @@ public class ClassWriter {
ClassNode outerNode = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE); ClassNode outerNode = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, node); DecompilerContext.setProperty(DecompilerContext.CURRENT_CLASS_NODE, node);
BytecodeMappingTracer tracer = new BytecodeMappingTracer(); int total_offset_lines = 0;
BytecodeMappingTracer dummy_tracer = new BytecodeMappingTracer();
try { try {
// last minute processing // last minute processing
@ -177,8 +178,13 @@ public class ClassWriter {
String lineSeparator = DecompilerContext.getNewLineSeparator(); String lineSeparator = DecompilerContext.getNewLineSeparator();
// write class definition
int start_class_def = buffer.length();
writeClassDefinition(node, buffer, indent); 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; boolean hasContent = false;
// fields // fields
@ -204,7 +210,7 @@ public class ClassWriter {
enumFields = false; 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; hasContent = true;
} }
@ -225,9 +231,13 @@ public class ClassWriter {
if (hasContent) { if (hasContent) {
buffer.append(lineSeparator); 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) { if (!methodSkipped) {
hasContent = true; hasContent = true;
DecompilerContext.getBytecodeSourceMapper().addTracer(cl.qualifiedName,
InterpreterUtil.makeUniqueKey(mt.getName(), mt.getDescriptor()), method_tracer);
total_offset_lines = method_tracer.getCurrentSourceline();
} }
else { else {
buffer.setLength(position); buffer.setLength(position);
@ -558,6 +568,10 @@ public class ClassWriter {
MethodWrapper methodWrapper = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor()); MethodWrapper methodWrapper = wrapper.getMethodWrapper(mt.getName(), mt.getDescriptor());
boolean hideMethod = false; 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); MethodWrapper outerWrapper = (MethodWrapper)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_WRAPPER);
DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, methodWrapper); DecompilerContext.setProperty(DecompilerContext.CURRENT_METHOD_WRAPPER, methodWrapper);
@ -569,9 +583,6 @@ public class ClassWriter {
boolean isDeprecated = mt.getAttributes().containsKey("Deprecated"); boolean isDeprecated = mt.getAttributes().containsKey("Deprecated");
boolean clinit = false, init = false, dinit = false; boolean clinit = false, init = false, dinit = false;
String indentString = InterpreterUtil.getIndentString(indent);
String lineSeparator = DecompilerContext.getNewLineSeparator();
MethodDescriptor md = MethodDescriptor.parseDescriptor(mt.getDescriptor()); MethodDescriptor md = MethodDescriptor.parseDescriptor(mt.getDescriptor());
int flags = mt.getAccessFlags(); int flags = mt.getAccessFlags();
@ -793,6 +804,9 @@ public class ClassWriter {
if (root != null && !methodWrapper.decompiledWithErrors) { // check for existence if (root != null && !methodWrapper.decompiledWithErrors) { // check for existence
try { try {
tracer.setCurrentSourceline(buffer.substring(start_index_method).split(lineSeparator, -1).length - 1);
String code = root.toJava(indent + 1, tracer); String code = root.toJava(indent + 1, tracer);
hideMethod = (clinit || dinit || hideConstructor(wrapper, init, throwsExceptions, paramCount)) && code.length() == 0; 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); 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; return !hideMethod;
} }

@ -262,10 +262,13 @@ public class ClassesProcessor {
new ClassWriter().classToJava(root, classBuffer, 0); new ClassWriter().classToJava(root, classBuffer, 0);
String lineSeparator = DecompilerContext.getNewLineSeparator(); String lineSeparator = DecompilerContext.getNewLineSeparator();
int total_offset_lines = 0;
int index = cl.qualifiedName.lastIndexOf("/"); int index = cl.qualifiedName.lastIndexOf("/");
if (index >= 0) { if (index >= 0) {
total_offset_lines++;
String packageName = cl.qualifiedName.substring(0, index).replace('/', '.'); String packageName = cl.qualifiedName.substring(0, index).replace('/', '.');
buffer.append("package "); buffer.append("package ");
buffer.append(packageName); buffer.append(packageName);
buffer.append(";"); buffer.append(";");
@ -273,15 +276,20 @@ public class ClassesProcessor {
buffer.append(lineSeparator); buffer.append(lineSeparator);
} }
if (importCollector.writeImports(buffer)) { int import_lines_written = importCollector.writeImports(buffer);
if (import_lines_written > 0) {
buffer.append(lineSeparator); buffer.append(lineSeparator);
total_offset_lines += import_lines_written + 1;
} }
buffer.append(classBuffer); buffer.append(classBuffer);
if(DecompilerContext.getOption(IFernflowerPreferences.BYTECODE_SOURCE_MAPPING)) { if(DecompilerContext.getOption(IFernflowerPreferences.BYTECODE_SOURCE_MAPPING)) {
BytecodeSourceMapper mapper = DecompilerContext.getBytecodeSourceMapper();
mapper.addTotalOffset(total_offset_lines);
buffer.append(lineSeparator); buffer.append(lineSeparator);
DecompilerContext.getBytecodeSourceMapper().dumpMapping(classBuffer); mapper.dumpMapping(buffer);
} }
} }
finally { finally {

@ -1,26 +1,53 @@
package org.jetbrains.java.decompiler.main.collectors; package org.jetbrains.java.decompiler.main.collectors;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set;
public class BytecodeMappingTracer { public class BytecodeMappingTracer {
private int current_sourceline; private int current_sourceline;
// bytecode offset, source line // 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() { public void incrementSourceLine() {
current_sourceline++; current_sourceline++;
} }
public void incrementSourceLine(int number_lines) {
current_sourceline += number_lines;
}
public void addMapping(int bytecode_offset) { public void addMapping(int bytecode_offset) {
if(!mapping.containsKey(bytecode_offset)) { if(!mapping.containsKey(bytecode_offset)) {
mapping.put(bytecode_offset, current_sourceline); 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() { public HashMap<Integer, Integer> getMapping() {
return mapping; return mapping;
} }
public int getCurrentSourceline() {
return current_sourceline;
}
public void setCurrentSourceline(int current_sourceline) {
this.current_sourceline = current_sourceline;
}
} }

@ -11,7 +11,7 @@ public class BytecodeSourceMapper {
private int offset_total; private int offset_total;
// class, method, bytecode offset, source line // 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) { public void addMapping(String classname, String methodname, int bytecode_offset, int source_line) {
@ -31,6 +31,12 @@ public class BytecodeSourceMapper {
} }
} }
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) { public void dumpMapping(StringBuilder buffer) {
String lineSeparator = DecompilerContext.getNewLineSeparator(); String lineSeparator = DecompilerContext.getNewLineSeparator();
@ -70,6 +76,9 @@ public class BytecodeSourceMapper {
this.offset_total = 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; 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(); List<String> imports = packImports();
for (String s : imports) { for (String s : imports) {
buffer.append("import "); buffer.append("import ");
buffer.append(s); buffer.append(s);
buffer.append(";"); buffer.append(";");
buffer.append(DecompilerContext.getNewLineSeparator()); buffer.append(new_line_separator);
importlines_written++;
} }
return imports.size() > 0; return importlines_written;
} }
private List<String> packImports() { private List<String> packImports() {

@ -331,6 +331,7 @@ public class ExprProcessor implements CodeConstants {
for (int i = 0; i < seq.length(); i++) { for (int i = 0; i < seq.length(); i++) {
Instruction instr = seq.getInstr(i); Instruction instr = seq.getInstr(i);
Integer bytecode_offset = block.getOldOffset(i);
switch (instr.opcode) { switch (instr.opcode) {
case opc_aconst_null: case opc_aconst_null:

@ -96,6 +96,8 @@ public class ArrayExprent extends Exprent {
res = "((" + ExprProcessor.getCastTypeName(objarr) + ")" + res + ")"; res = "((" + ExprProcessor.getCastTypeName(objarr) + ")" + res + ")";
} }
tracer.addMapping(bytecode);
return res + "[" + index.toJava(indent, tracer) + "]"; return res + "[" + index.toJava(indent, tracer) + "]";
} }

@ -38,12 +38,15 @@ public class AssertExprent extends Exprent {
buffer.append("assert "); buffer.append("assert ");
tracer.addMapping(bytecode);
if (parameters.get(0) == null) { if (parameters.get(0) == null) {
buffer.append("false"); buffer.append("false");
} }
else { else {
buffer.append(parameters.get(0).toJava(indent, tracer)); buffer.append(parameters.get(0).toJava(indent, tracer));
} }
if (parameters.size() > 1) { if (parameters.size() > 1) {
buffer.append(" : "); buffer.append(" : ");
buffer.append(parameters.get(1).toJava(indent, tracer)); buffer.append(parameters.get(1).toJava(indent, tracer));

@ -153,6 +153,8 @@ public class AssignmentExprent extends Exprent {
buffer.append(condtype == CONDITION_NONE ? " = " : funceq[condtype]).append(res); buffer.append(condtype == CONDITION_NONE ? " = " : funceq[condtype]).append(res);
tracer.addMapping(bytecode);
return buffer.toString(); return buffer.toString();
} }

@ -112,6 +112,8 @@ public class ConstExprent extends Exprent {
boolean literal = DecompilerContext.getOption(IFernflowerPreferences.LITERALS_AS_IS); boolean literal = DecompilerContext.getOption(IFernflowerPreferences.LITERALS_AS_IS);
boolean ascii = DecompilerContext.getOption(IFernflowerPreferences.ASCII_STRING_CHARACTERS); boolean ascii = DecompilerContext.getOption(IFernflowerPreferences.ASCII_STRING_CHARACTERS);
tracer.addMapping(bytecode);
if (consttype.type != CodeConstants.TYPE_NULL && value == null) { if (consttype.type != CodeConstants.TYPE_NULL && value == null) {
return ExprProcessor.getCastTypeName(consttype); return ExprProcessor.getCastTypeName(consttype);
} }

@ -77,6 +77,9 @@ public class ExitExprent extends Exprent {
@Override @Override
public String toJava(int indent, BytecodeMappingTracer tracer) { public String toJava(int indent, BytecodeMappingTracer tracer) {
tracer.addMapping(bytecode);
if (exittype == EXIT_RETURN) { if (exittype == EXIT_RETURN) {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();

@ -54,6 +54,9 @@ public class Exprent {
public int id; public int id;
//offsets of bytecode instructions decompiled to this exprent
public Set<Integer> bytecode = new HashSet<Integer>();
{ {
// set exprent id // set exprent id
id = DecompilerContext.getCounterContainer().getCounterAndIncrement(CounterContainer.EXPRENT_COUNTER); id = DecompilerContext.getCounterContainer().getCounterAndIncrement(CounterContainer.EXPRENT_COUNTER);

@ -159,6 +159,8 @@ public class FieldExprent extends Exprent {
buf.append(name); buf.append(name);
tracer.addMapping(bytecode);
return buf.toString(); return buf.toString();
} }

@ -450,6 +450,8 @@ public class FunctionExprent extends Exprent {
@Override @Override
public String toJava(int indent, BytecodeMappingTracer tracer) { public String toJava(int indent, BytecodeMappingTracer tracer) {
tracer.addMapping(bytecode);
if (functype <= FUNCTION_USHR) { if (functype <= FUNCTION_USHR) {
return wrapOperandString(lstOperands.get(0), false, indent, tracer) + operators[functype] + return wrapOperandString(lstOperands.get(0), false, indent, tracer) + operators[functype] +
wrapOperandString(lstOperands.get(1), true, indent, tracer); wrapOperandString(lstOperands.get(1), true, indent, tracer);

@ -114,6 +114,7 @@ public class IfExprent extends Exprent {
@Override @Override
public String toJava(int indent, BytecodeMappingTracer tracer) { public String toJava(int indent, BytecodeMappingTracer tracer) {
tracer.addMapping(bytecode);
return "if(" + condition.toJava(indent, tracer) + ")"; return "if(" + condition.toJava(indent, tracer) + ")";
} }

@ -194,6 +194,8 @@ public class InvocationExprent extends Exprent {
String super_qualifier = null; String super_qualifier = null;
boolean isInstanceThis = false; boolean isInstanceThis = false;
tracer.addMapping(bytecode);
if (invocationTyp == INVOKE_DYNAMIC) { if (invocationTyp == INVOKE_DYNAMIC) {
// ClassNode node = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASSNODE); // ClassNode node = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASSNODE);
// //

@ -52,6 +52,9 @@ public class MonitorExprent extends Exprent {
@Override @Override
public String toJava(int indent, BytecodeMappingTracer tracer) { public String toJava(int indent, BytecodeMappingTracer tracer) {
tracer.addMapping(bytecode);
if (montype == MONITOR_ENTER) { if (montype == MONITOR_ENTER) {
return "synchronized(" + value.toJava(indent, tracer) + ")"; return "synchronized(" + value.toJava(indent, tracer) + ")";
} }

@ -84,6 +84,7 @@ public class SwitchExprent extends Exprent {
@Override @Override
public String toJava(int indent, BytecodeMappingTracer tracer) { public String toJava(int indent, BytecodeMappingTracer tracer) {
tracer.addMapping(bytecode);
return "switch(" + value.toJava(indent, tracer) + ")"; return "switch(" + value.toJava(indent, tracer) + ")";
} }

@ -85,6 +85,8 @@ public class VarExprent extends Exprent {
public String toJava(int indent, BytecodeMappingTracer tracer) { public String toJava(int indent, BytecodeMappingTracer tracer) {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
tracer.addMapping(bytecode);
if (classdef) { if (classdef) {
ClassNode child = DecompilerContext.getClassProcessor().getMapRootClasses().get(vartype.value); ClassNode child = DecompilerContext.getClassProcessor().getMapRootClasses().get(vartype.value);
new ClassWriter().classToJava(child, buffer, indent); new ClassWriter().classToJava(child, buffer, indent);

@ -489,9 +489,9 @@ public class Statement {
} }
// to be overwritten // to be overwritten
//public String toJava() { public String toJava() {
// return toJava(0); return toJava(0, new BytecodeMappingTracer());
//} }
public String toJava(int indent, BytecodeMappingTracer tracer) { public String toJava(int indent, BytecodeMappingTracer tracer) {
throw new RuntimeException("not implemented"); throw new RuntimeException("not implemented");

Loading…
Cancel
Save