diff --git a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java index 20de771..ad61f92 100644 --- a/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java +++ b/src/org/jetbrains/java/decompiler/main/collectors/BytecodeSourceMapper.java @@ -1,83 +1,99 @@ +/* + * 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. + */ package org.jetbrains.java.decompiler.main.collectors; -import java.util.*; -import java.util.Map.Entry; - import org.jetbrains.java.decompiler.main.DecompilerContext; import org.jetbrains.java.decompiler.main.TextBuffer; +import java.util.*; +import java.util.Map.Entry; + public class BytecodeSourceMapper { private int offset_total; - private final HashMap myOriginalLinesMapping = new HashMap(); - // class, method, bytecode offset, source line - private final HashMap>> mapping = new LinkedHashMap>>(); // need to preserve order + private final Map>> mapping = new LinkedHashMap>>(); - public void addMapping(String classname, String methodname, int bytecode_offset, int source_line) { + // original line to decompiled line + private final Map linesMapping = new LinkedHashMap(); - HashMap> class_mapping = mapping.get(classname); - if(class_mapping == null) { - mapping.put(classname, class_mapping = new LinkedHashMap>()); // need to preserve order + public void addMapping(String className, String methodName, int bytecodeOffset, int sourceLine) { + Map> class_mapping = mapping.get(className); + if (class_mapping == null) { + mapping.put(className, class_mapping = new LinkedHashMap>()); // need to preserve order } - HashMap method_mapping = class_mapping.get(methodname); - if(method_mapping == null) { - class_mapping.put(methodname, method_mapping = new HashMap()); + Map 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); + if (!method_mapping.containsKey(bytecodeOffset)) { + method_mapping.put(bytecodeOffset, sourceLine); } } - public void addTracer(String classname, String methodname, BytecodeMappingTracer tracer) { - for(Entry entry : tracer.getMapping().entrySet()) { - addMapping(classname, methodname, entry.getKey(), entry.getValue()); + public void addTracer(String className, String methodName, BytecodeMappingTracer tracer) { + for (Entry entry : tracer.getMapping().entrySet()) { + addMapping(className, methodName, entry.getKey(), entry.getValue()); } - myOriginalLinesMapping.putAll(tracer.getOriginalLinesMapping()); + linesMapping.putAll(tracer.getOriginalLinesMapping()); } public void dumpMapping(TextBuffer buffer, boolean offsetsToHex) { - String lineSeparator = DecompilerContext.getNewLineSeparator(); - for(Entry>> class_entry : mapping.entrySet()) { - HashMap> class_mapping = class_entry.getValue(); - buffer.append("class " + class_entry.getKey() + "{" + lineSeparator); + for (Entry>> class_entry : mapping.entrySet()) { + Map> class_mapping = class_entry.getValue(); + buffer.append("class '" + class_entry.getKey() + "' {" + lineSeparator); boolean is_first_method = true; + for (Entry> method_entry : class_mapping.entrySet()) { + Map method_mapping = method_entry.getValue(); - for(Entry> method_entry : class_mapping.entrySet()) { - HashMap method_mapping = method_entry.getValue(); - - if(!is_first_method) { + if (!is_first_method) { buffer.appendLineSeparator(); } - buffer.appendIndent(1).append("method " + method_entry.getKey() + "{" + lineSeparator); + + buffer.appendIndent(1).append("method '" + method_entry.getKey() + "' {" + lineSeparator); List lstBytecodeOffsets = new ArrayList(method_mapping.keySet()); Collections.sort(lstBytecodeOffsets); - for(Integer offset : lstBytecodeOffsets) { + for (Integer offset : lstBytecodeOffsets) { Integer line = method_mapping.get(offset); - String strOffset = offsetsToHex ? Integer.toHexString(offset): line.toString(); + String strOffset = offsetsToHex ? Integer.toHexString(offset) : line.toString(); buffer.appendIndent(2).append(strOffset).appendIndent(2).append((line + offset_total) + lineSeparator); } buffer.appendIndent(1).append("}").appendLineSeparator(); + is_first_method = false; } - buffer.append("}").appendLineSeparator(); + + buffer.append("}").appendLineSeparator().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(); + for (int i = 0; i < mapping.length; i += 2) { + buffer.append(mapping[i]).append(" <-> ").append(mapping[i + 1]).appendLineSeparator(); } } @@ -94,15 +110,15 @@ public class BytecodeSourceMapper { } /** - * original to our line mapping + * Original to decompiled line mapping. */ public int[] getOriginalLinesMapping() { - int[] res = new int[myOriginalLinesMapping.size()*2]; + int[] res = new int[linesMapping.size() * 2]; int i = 0; - for (Entry entry : myOriginalLinesMapping.entrySet()) { + for (Entry entry : linesMapping.entrySet()) { res[i] = entry.getKey(); - res[i+1] = entry.getValue() + offset_total + 1; // make it 1 based - i+=2; + res[i + 1] = entry.getValue() + offset_total + 1; // make it 1 based + i += 2; } return res; } diff --git a/testData/results/TestClassSimpleBytecodeMapping.dec b/testData/results/TestClassSimpleBytecodeMapping.dec index ce9cc0c..9dd881c 100644 --- a/testData/results/TestClassSimpleBytecodeMapping.dec +++ b/testData/results/TestClassSimpleBytecodeMapping.dec @@ -22,15 +22,16 @@ public class TestClassSimpleBytecodeMapping { } } -class pkg/TestClassSimpleBytecodeMapping$1{ - method run ()V{ +class 'pkg/TestClassSimpleBytecodeMapping$1' { + method 'run ()V' { 0 7 3 7 5 7 } } -class pkg/TestClassSimpleBytecodeMapping{ - method test ()I{ + +class 'pkg/TestClassSimpleBytecodeMapping' { + method 'test ()I' { 0 4 3 4 5 4 @@ -51,10 +52,11 @@ class pkg/TestClassSimpleBytecodeMapping{ 2f 15 } - method run (Ljava/lang/Runnable;)V{ + method 'run (Ljava/lang/Runnable;)V' { 1 20 } } + Lines mapping: 17 <-> 8 21 <-> 11