1 package de.loskutov.bco.editors; 2 3 import org.eclipse.core.runtime.CoreException; 4 import org.eclipse.core.runtime.IStatus; 5 import org.eclipse.jdt.core.IBuffer; 6 import org.eclipse.jdt.core.IClassFile; 7 import org.eclipse.jdt.internal.core.BufferManager; 8 import org.eclipse.jdt.internal.ui.javaeditor.ClassFileDocumentProvider; 9 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 10 import org.eclipse.jface.text.AbstractDocument; 11 import org.eclipse.jface.text.BadLocationException; 12 import org.eclipse.jface.text.IDocument; 13 import org.eclipse.jface.text.IRegion; 14 import org.eclipse.ui.IEditorInput; 15 import org.eclipse.ui.IEditorPart; 16 import org.eclipse.ui.PlatformUI; 17 18 import de.loskutov.bco.BytecodeOutlinePlugin; 19 20 24 public class BytecodeDocumentProvider extends ClassFileDocumentProvider { 25 26 public BytecodeDocumentProvider() { 27 super(); 28 } 29 30 31 35 protected boolean setDocumentContent(IDocument document, 36 IEditorInput editorInput, String encoding) throws CoreException { 37 38 if (editorInput instanceof IClassFileEditorInput) { 39 IClassFile classFile = ((IClassFileEditorInput) editorInput) 40 .getClassFile(); 41 42 String source = classFile.getSource(); 43 if (source == null) { 44 IBuffer buffer = BufferManager.getDefaultBufferManager().getBuffer(classFile); 48 if (buffer != null) { 49 source = buffer.getContents(); 50 } 51 } 52 document.set(source); 53 return true; 54 } 55 return super.setDocumentContent(document, editorInput, encoding); 56 } 57 58 78 public IDocument getDocument(Object element) { 79 IDocument document = super.getDocument(element); 80 if (element instanceof IClassFileEditorInput && isDebuggerCall()) { 81 IClassFileEditorInput input = (IClassFileEditorInput) element; 82 83 return new DocumentProxy4Debugger(document, input.getClassFile()); 84 } 85 return document; 86 } 87 88 99 private boolean isDebuggerCall() { 100 Exception e = new Exception (); 101 StackTraceElement [] stackTrace = e.getStackTrace(); 102 boolean stackOk = true; 103 for (int i = 2; i < stackTrace.length; i++) { 105 StackTraceElement elt = stackTrace[i]; 106 switch (i) { 107 case 2 : 108 stackOk = "getLineInformation".equals(elt.getMethodName()) 109 || "addAnnotation".equals(elt.getMethodName()); 110 break; 111 case 3 : 112 stackOk = "positionEditor".equals(elt.getMethodName()) 113 || "display".equals(elt.getMethodName()); 114 break; 115 default : 116 break; 117 } 118 if(! stackOk || i > 3){ 119 return false; 120 } 121 122 if (stackOk && i == 3) { 123 IEditorPart activeEditor = PlatformUI.getWorkbench() 124 .getActiveWorkbenchWindow().getActivePage() 125 .getActiveEditor(); 126 if(activeEditor instanceof BytecodeClassFileEditor){ 127 BytecodeClassFileEditor editor = (BytecodeClassFileEditor) activeEditor; 128 return editor.isDecompiled(); 129 } 130 } 131 } 132 return false; 133 } 134 135 public IRegion getDecompiledLineInfo(IEditorInput input, int decompiledLine) { 136 IDocument document = getDocument(input); 137 try { 138 return document.getLineInformation(decompiledLine); 139 } catch (BadLocationException e) { 140 BytecodeOutlinePlugin.log(e, IStatus.ERROR); 141 } 142 return null; 143 } 144 145 155 156 160 private static final class DocumentProxy4Debugger extends AbstractDocument { 161 162 private final IDocument delegate; 163 private final IClassFile cf; 164 165 public DocumentProxy4Debugger(IDocument delegate, IClassFile cf) { 166 super(); 167 this.delegate = delegate; 168 this.cf = cf; 169 } 170 171 public IRegion getLineInformation(int line) throws BadLocationException { 172 BytecodeSourceMapper mapper = BytecodeClassFileEditor 173 .getSourceMapper(); 174 175 int decompiledLine; 176 if(line < -1){ 177 181 decompiledLine = mapper.mapDebuggerToDecompiled(cf); 182 } else { 183 decompiledLine = mapper.mapToDecompiled(line + 1, cf); 185 if(decompiledLine == -1){ 186 192 return BytecodeClassFileEditor.checkForInnerClass(line, cf); 193 } 194 } 195 return delegate.getLineInformation(decompiledLine + 1); 197 } 198 } 199 200 } 201 | Popular Tags |