Commit c4cd8e52 authored by ebruneton's avatar ebruneton

added draft implementation for Bytecode Reference View

parent 242f9b47
......@@ -9,6 +9,12 @@
category="org.eclipse.jdt.ui.java"
class="de.loskutov.bco.views.BytecodeOutlineView"
id="de.loskutov.bco.views.BytecodeOutlineView"/>
<view
name="Bytecode Reference"
icon="icons/bytecodeview.gif"
category="org.eclipse.jdt.ui.java"
class="de.loskutov.bco.views.BytecodeReferenceView"
id="de.loskutov.bco.views.BytecodeReferenceView"/>
</extension>
<extension
......
......@@ -80,6 +80,24 @@ public class DecompiledClass {
return -1;
}
public int getBytecodeInsn(final int decompiledLine) {
int currentDecompiledLine = 0;
for (int i = 0; i < text.size(); ++i) {
Object o = text.get(i);
if (o instanceof DecompiledMethod) {
DecompiledMethod m = (DecompiledMethod) o;
Integer opcode = m.getBytecodeInsn(decompiledLine - currentDecompiledLine);
if(opcode != null){
return opcode.intValue();
}
currentDecompiledLine += m.getLineCount();
} else {
currentDecompiledLine++;
}
}
return -1;
}
public int getSourceLine(final int decompiledLine) {
int currentDecompiledLine = 0;
for (int i = 0; i < text.size(); ++i) {
......
......@@ -36,6 +36,8 @@ public class DecompiledMethod {
private Map insns; // decompiled line -> insn
private Map opcodes; // decompiled line -> opcode
private Map insnLines; // insn -> decompile line
private int lineCount;
......@@ -56,6 +58,7 @@ public class DecompiledMethod {
this.sourceLines = new HashMap();
this.decompiledLines = new HashMap();
this.insns = new HashMap();
this.opcodes = new HashMap();
this.insnLines = new HashMap();
this.meth = meth;
......@@ -176,6 +179,7 @@ public class DecompiledMethod {
int currentSourceLine = -1;
int currentDecompiledLine = 0;
int currentInsn = -1;
int currentOpcode = -1;
for (int i = 0; i < text.size(); ++i) {
Object o = text.get(i);
if (o instanceof Index) {
......@@ -185,17 +189,20 @@ public class DecompiledMethod {
currentSourceLine = sourceLine.intValue();
}
currentInsn = index.insn;
currentOpcode = index.opcode;
} else {
++currentDecompiledLine;
}
Integer csl = new Integer(currentSourceLine);
Integer cdl = new Integer(currentDecompiledLine);
Integer ci = new Integer(currentInsn);
Integer co = new Integer(currentOpcode);
sourceLines.put(cdl, csl);
if (decompiledLines.get(csl) == null) {
decompiledLines.put(csl, cdl);
}
insns.put(cdl, ci);
opcodes.put(cdl, co);
if (insnLines.get(ci) == null) {
insnLines.put(ci, cdl);
}
......@@ -352,6 +359,15 @@ public class DecompiledMethod {
return insn;
}
/**
* @param decompiledLine
* @return
*/
public Integer getBytecodeInsn(final int decompiledLine) {
Integer insn = (Integer) opcodes.get(new Integer(decompiledLine));
return insn;
}
public String[][][] getFrameTables(final int decompiledLine, boolean useQualifiedNames) {
Integer insn = getBytecodeOffset(decompiledLine);
if (error != null && insn != null && insn.intValue() == errorInsn) {
......
......@@ -8,6 +8,7 @@ import java.util.Map;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.LocalVariableNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.util.AbstractVisitor;
......@@ -48,7 +49,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitInsn(final int opcode) {
addIndex();
addIndex(opcode);
super.visitInsn(opcode);
if (meth != null) {
meth.visitInsn(opcode);
......@@ -56,7 +57,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitIntInsn(final int opcode, final int operand) {
addIndex();
addIndex(opcode);
super.visitIntInsn(opcode, operand);
if (meth != null) {
meth.visitIntInsn(opcode, operand);
......@@ -64,7 +65,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitVarInsn(final int opcode, final int var) {
addIndex();
addIndex(opcode);
super.visitVarInsn(opcode, var);
if (meth != null) {
meth.visitVarInsn(opcode, var);
......@@ -72,7 +73,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitTypeInsn(final int opcode, final String desc) {
addIndex();
addIndex(opcode);
super.visitTypeInsn(opcode, desc);
if (meth != null) {
meth.visitTypeInsn(opcode, desc);
......@@ -81,7 +82,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
public void visitFieldInsn(final int opcode, final String owner1,
final String name, final String desc) {
addIndex();
addIndex(opcode);
super.visitFieldInsn(opcode, owner1, name, desc);
if (meth != null) {
meth.visitFieldInsn(opcode, owner1, name, desc);
......@@ -90,7 +91,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
public void visitMethodInsn(final int opcode, final String owner1,
final String name, final String desc) {
addIndex();
addIndex(opcode);
super.visitMethodInsn(opcode, owner1, name, desc);
if (meth != null) {
meth.visitMethodInsn(opcode, owner1, name, desc);
......@@ -98,7 +99,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitJumpInsn(final int opcode, final Label label) {
addIndex();
addIndex(opcode);
super.visitJumpInsn(opcode, label);
if (meth != null) {
meth.visitJumpInsn(opcode, label);
......@@ -107,7 +108,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
public void visitLabel(final Label label) {
currentLabel = label;
addIndex();
addIndex(-1);
super.visitLabel(label);
if (meth != null) {
meth.visitLabel(label);
......@@ -115,7 +116,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitLdcInsn(final Object cst) {
addIndex();
addIndex(Opcodes.LDC);
super.visitLdcInsn(cst);
if (meth != null) {
meth.visitLdcInsn(cst);
......@@ -123,7 +124,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitIincInsn(final int var, final int increment) {
addIndex();
addIndex(Opcodes.IINC);
super.visitIincInsn(var, increment);
if (meth != null) {
meth.visitIincInsn(var, increment);
......@@ -132,7 +133,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
public void visitTableSwitchInsn(final int min, final int max,
final Label dflt, final Label[] labels) {
addIndex();
addIndex(Opcodes.TABLESWITCH);
super.visitTableSwitchInsn(min, max, dflt, labels);
if (meth != null) {
meth.visitTableSwitchInsn(min, max, dflt, labels);
......@@ -141,7 +142,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
public void visitLookupSwitchInsn(final Label dflt, final int[] keys,
final Label[] labels) {
addIndex();
addIndex(Opcodes.LOOKUPSWITCH);
super.visitLookupSwitchInsn(dflt, keys, labels);
if (meth != null) {
meth.visitLookupSwitchInsn(dflt, keys, labels);
......@@ -149,7 +150,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
public void visitMultiANewArrayInsn(final String desc, final int dims) {
addIndex();
addIndex(Opcodes.MULTIANEWARRAY);
super.visitMultiANewArrayInsn(desc, dims);
if (meth != null) {
meth.visitMultiANewArrayInsn(desc, dims);
......@@ -190,7 +191,7 @@ public class DecompilerMethodVisitor extends MethodAdapter {
}
}
protected void addIndex() {
text.add(new Index(currentLabel, currentInsn++));
protected void addIndex(final int opcode) {
text.add(new Index(currentLabel, currentInsn++, opcode));
}
}
......@@ -11,9 +11,12 @@ public class Index {
public final Label label;
public final int insn;
public final int opcode;
public Index(final Label label, final int insn) {
public Index(final Label label, final int insn, final int opcode) {
this.label = label;
this.insn = insn;
this.opcode = opcode;
}
}
......@@ -954,6 +954,13 @@ public class BytecodeOutlineView extends ViewPart {
statusLineManager.setMessage( currentStatusMessage + selectionInfo);
}
}
public int getBytecodeInstructionAtLine (int line) {
if (lastDecompiledResult != null) {
return lastDecompiledResult.getBytecodeInsn(line);
}
return -1;
}
/**
* @return IJavaElement which fits in the current selection in java editor
......
/*
* Copyright area
*/
package de.loskutov.bco.views;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.objectweb.asm.util.AbstractVisitor;
public class BytecodeReferenceView extends ViewPart implements ISelectionListener {
Browser browser;
public void createPartControl(Composite parent) {
browser = new Browser(parent, SWT.NONE);
getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(this);
}
public void setFocus() {
/* nothing to do */
}
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (selection instanceof ITextSelection && part instanceof BytecodeOutlineView) {
int line = ((ITextSelection)selection).getStartLine();
int opcode = ((BytecodeOutlineView)part).getBytecodeInstructionAtLine(line);
/* TODO replace code below with something that gets HTML content from opcode */
String opcodeName = null;
if (opcode != -1) {
opcodeName = AbstractVisitor.OPCODES[opcode];
}
if (opcodeName != null) {
browser.setText("<html><body>" + opcodeName + "</body></html>");
} else {
browser.setText("");
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment