package org.jetbrains.java.decompiler.main.collectors; import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.TextBuffer; import java.util.HashMap; import java.util.Map.Entry; public class BytecodeSourceMapper { private int offset_total; // class, method, bytecode offset, source line private HashMap>> mapping = new HashMap>>(); public void addMapping(String classname, String methodname, int bytecode_offset, int source_line) { HashMap> class_mapping = mapping.get(classname); if(class_mapping == null) { mapping.put(classname, class_mapping = new HashMap>()); } HashMap method_mapping = class_mapping.get(methodname); if(method_mapping == null) { class_mapping.put(methodname, method_mapping = new HashMap()); } // 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 entry : tracer.getMapping().entrySet()) { addMapping(classname, methodname, entry.getKey(), entry.getValue()); } } public void dumpMapping(TextBuffer buffer) { String lineSeparator = DecompilerContext.getNewLineSeparator(); for(Entry>> class_entry : mapping.entrySet()) { HashMap> class_mapping = class_entry.getValue(); buffer.append("class " + class_entry.getKey() + "{" + lineSeparator); boolean is_first_method = true; for(Entry> method_entry : class_mapping.entrySet()) { HashMap method_mapping = method_entry.getValue(); if(!is_first_method) { buffer.appendLineSeparator(); } buffer.appendIndent(1).append("method " + method_entry.getKey() + "{" + lineSeparator); for(Entry line : method_mapping.entrySet()) { buffer.appendIndent(2).append(line.getKey().toString()).appendIndent(2).append((line.getValue() + offset_total) + lineSeparator); } buffer.appendIndent(1).append("}").appendLineSeparator(); is_first_method = false; } buffer.append("}").appendLineSeparator(); } } public int getTotalOffset() { return offset_total; } public void setTotalOffset(int offset_total) { this.offset_total = offset_total; } public void addTotalOffset(int offset_total) { this.offset_total += offset_total; } }