|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025, 2026 IBM Corporation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.internal.debug.ui.sourcelookup; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import org.eclipse.core.runtime.CoreException; |
| 19 | +import org.eclipse.debug.core.DebugException; |
| 20 | +import org.eclipse.debug.internal.ui.DebugUIPlugin; |
| 21 | +import org.eclipse.debug.internal.ui.sourcelookup.SourceLookupFacility; |
| 22 | +import org.eclipse.debug.internal.ui.sourcelookup.SourceLookupResult; |
| 23 | +import org.eclipse.debug.ui.DebugUITools; |
| 24 | +import org.eclipse.debug.ui.contexts.DebugContextEvent; |
| 25 | +import org.eclipse.debug.ui.contexts.IDebugContextListener; |
| 26 | +import org.eclipse.debug.ui.contexts.IDebugContextService; |
| 27 | +import org.eclipse.debug.ui.sourcelookup.ISourceDisplay; |
| 28 | +import org.eclipse.jdt.core.IJavaElement; |
| 29 | +import org.eclipse.jdt.core.JavaModelException; |
| 30 | +import org.eclipse.jdt.core.dom.IMethodBinding; |
| 31 | +import org.eclipse.jdt.core.dom.LambdaExpression; |
| 32 | +import org.eclipse.jdt.internal.debug.core.model.JDIStackFrame; |
| 33 | +import org.eclipse.jdt.internal.debug.core.model.LambdaUtils; |
| 34 | +import org.eclipse.jdt.internal.debug.ui.actions.ToggleBreakpointAdapter; |
| 35 | +import org.eclipse.jdt.internal.ui.javaeditor.ClassFileEditor; |
| 36 | +import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; |
| 37 | +import org.eclipse.jdt.ui.JavaUI; |
| 38 | +import org.eclipse.jface.text.BadLocationException; |
| 39 | +import org.eclipse.jface.text.IDocument; |
| 40 | +import org.eclipse.jface.text.IRegion; |
| 41 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 42 | +import org.eclipse.ui.IEditorInput; |
| 43 | +import org.eclipse.ui.IEditorPart; |
| 44 | +import org.eclipse.ui.IWorkbenchPage; |
| 45 | +import org.eclipse.ui.PartInitException; |
| 46 | +import org.eclipse.ui.texteditor.IDocumentProvider; |
| 47 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 48 | + |
| 49 | +/** |
| 50 | + * @since 3.2 |
| 51 | + */ |
| 52 | +public class JavaStackFrameSourceDisplayAdapter implements ISourceDisplay { |
| 53 | + |
| 54 | + private class ClassFileUnhighlightingListener implements IDebugContextListener { |
| 55 | + |
| 56 | + private final IDebugContextService service; |
| 57 | + |
| 58 | + public ClassFileUnhighlightingListener(IDebugContextService service) { |
| 59 | + this.service = service; |
| 60 | + } |
| 61 | + @Override |
| 62 | + public void debugContextChanged(DebugContextEvent event) { |
| 63 | + if ((event.getFlags() & DebugContextEvent.STATE) > 0) { |
| 64 | + if (event.getContext() instanceof IStructuredSelection selection && selection.getFirstElement() instanceof JDIStackFrame frame |
| 65 | + && frame.equals(currentClassFileFrame)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (currentClassFileEditor != null) { |
| 70 | + currentClassFileEditor.unhighlight(); |
| 71 | + currentClassFileEditor = null; |
| 72 | + currentClassFileFrame = null; |
| 73 | + service.removeDebugContextListener(this); |
| 74 | + classFileUnhighlighterRegistered = false; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private boolean classFileUnhighlighterRegistered = false; |
| 81 | + private JDIStackFrame currentClassFileFrame; |
| 82 | + private ClassFileEditor currentClassFileEditor; |
| 83 | + |
| 84 | + @Override |
| 85 | + public void displaySource(Object element, IWorkbenchPage page, boolean forceSourceLookup) { |
| 86 | + JDIStackFrame jdiFrame = (JDIStackFrame) element; |
| 87 | + try { |
| 88 | + SourceLookupResult sourceRes = SourceLookupFacility.getDefault().lookup(element, jdiFrame.getLaunch().getSourceLocator(), forceSourceLookup); |
| 89 | + |
| 90 | + if (sourceRes.getEditorInput() instanceof IClassFileEditorInput input) { |
| 91 | + if (handleClassFile(page, jdiFrame, input)) { |
| 92 | + ensureListenerRegistered(page); |
| 93 | + return; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (LambdaUtils.isLambdaFrame(jdiFrame)) { |
| 98 | + if (handleLambda(jdiFrame, sourceRes)) { |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + } catch (CoreException | BadLocationException e) { |
| 103 | + DebugUIPlugin.log(e); |
| 104 | + } |
| 105 | + SourceLookupFacility.getDefault().displaySource(jdiFrame, page, forceSourceLookup); |
| 106 | + } |
| 107 | + |
| 108 | + private boolean handleLambda(JDIStackFrame jdiFrame, SourceLookupResult sourceRes) throws CoreException, BadLocationException { |
| 109 | + IDocumentProvider provider = JavaUI.getDocumentProvider(); |
| 110 | + IEditorInput editorInput = sourceRes.getEditorInput(); |
| 111 | + provider.connect(editorInput); |
| 112 | + IDocument document = provider.getDocument(editorInput); |
| 113 | + IRegion region = document.getLineInformation(jdiFrame.getLineNumber() - 1); |
| 114 | + IJavaElement je = JavaUI.getEditorInputJavaElement(editorInput); |
| 115 | + if (je != null) { |
| 116 | + IEditorPart part = JavaUI.openInEditor(je); |
| 117 | + if (part instanceof ITextEditor textEditor) { |
| 118 | + List<LambdaExpression> inLineLambdas = ToggleBreakpointAdapter.findLambdaExpressions(textEditor, region); |
| 119 | + for (LambdaExpression exp : inLineLambdas) { |
| 120 | + IMethodBinding methodBinding = exp.resolveMethodBinding(); |
| 121 | + String key = methodBinding.getKey(); |
| 122 | + if (key.contains(jdiFrame.getName())) { |
| 123 | + textEditor.selectAndReveal(exp.getStartPosition(), exp.getLength()); |
| 124 | + return true; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + private boolean handleClassFile(IWorkbenchPage page, JDIStackFrame jdiFrame, IClassFileEditorInput input) throws JavaModelException, PartInitException, DebugException { |
| 133 | + IJavaElement je = JavaUI.getEditorInputJavaElement(input); |
| 134 | + if (je != null) { |
| 135 | + IEditorPart part = JavaUI.openInEditor(je); |
| 136 | + if (part instanceof ClassFileEditor editor && editor.getDocumentProvider().getDocument(editor.getEditorInput()).getLength() == 0) { |
| 137 | + editor.highlightInstruction(jdiFrame.getMethodName(), jdiFrame.getSignature(), jdiFrame.getCodeIndex(), "currentIPColor"); //$NON-NLS-1$ |
| 138 | + ensureListenerRegistered(page); |
| 139 | + currentClassFileEditor = editor; |
| 140 | + currentClassFileFrame = jdiFrame; |
| 141 | + return true; |
| 142 | + } |
| 143 | + } |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + private void ensureListenerRegistered(IWorkbenchPage page) { |
| 148 | + if (classFileUnhighlighterRegistered) { |
| 149 | + return; |
| 150 | + } |
| 151 | + IDebugContextService service = DebugUITools.getDebugContextManager().getContextService(page.getWorkbenchWindow()); |
| 152 | + service.addDebugContextListener(new ClassFileUnhighlightingListener(service)); |
| 153 | + classFileUnhighlighterRegistered = true; |
| 154 | + } |
| 155 | +} |
0 commit comments