|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 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 | + |
| 15 | +package org.eclipse.jdt.internal.debug.ui.actions; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +import org.eclipse.core.resources.IProject; |
| 21 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 22 | +import org.eclipse.core.runtime.CoreException; |
| 23 | +import org.eclipse.debug.core.model.IDebugElement; |
| 24 | +import org.eclipse.debug.core.model.IStackFrame; |
| 25 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 26 | +import org.eclipse.jdt.core.IJavaProject; |
| 27 | +import org.eclipse.jdt.core.IType; |
| 28 | +import org.eclipse.jdt.core.JavaCore; |
| 29 | +import org.eclipse.jdt.core.dom.AST; |
| 30 | +import org.eclipse.jdt.core.dom.ASTParser; |
| 31 | +import org.eclipse.jdt.core.dom.ASTVisitor; |
| 32 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 33 | +import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 34 | +import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
| 35 | +import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
| 36 | +import org.eclipse.jdt.debug.core.IJavaStackFrame; |
| 37 | +import org.eclipse.jdt.debug.core.IJavaType; |
| 38 | +import org.eclipse.jdt.debug.core.IJavaVariable; |
| 39 | +import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
| 40 | +import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; |
| 41 | +import org.eclipse.jdt.ui.JavaUI; |
| 42 | +import org.eclipse.jface.text.IDocument; |
| 43 | +import org.eclipse.jface.text.IRegion; |
| 44 | +import org.eclipse.ui.IEditorPart; |
| 45 | +import org.eclipse.ui.texteditor.IDocumentProvider; |
| 46 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 47 | + |
| 48 | +public class OpenDeclLocalVarNavigationAction extends OpenVariableTypeAction { |
| 49 | + @Override |
| 50 | + protected IJavaType getTypeToOpen(IDebugElement element) throws CoreException { |
| 51 | + try { |
| 52 | + if (element instanceof IJavaVariable varE) { |
| 53 | + IJavaType javaType = varE.getJavaType(); |
| 54 | + String name = varE.getName(); |
| 55 | + IStackFrame jFrame = javaType.getDebugTarget().getThreads()[0].getTopStackFrame(); |
| 56 | + if (jFrame instanceof IJavaStackFrame jf) { |
| 57 | + jf.getReferenceType().getAllFieldNames(); |
| 58 | + String type = jf.getLaunch().getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null); |
| 59 | + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(type); |
| 60 | + IJavaProject iJavaProject = JavaCore.create(project); |
| 61 | + IType iType = iJavaProject.findType(jf.getReceivingTypeName()); |
| 62 | + String nameMethod = jf.getMethodName(); |
| 63 | + List<String> frameParams = jf.getArgumentTypeNames(); |
| 64 | + List<String> ref = frameParams.stream().map(e -> { |
| 65 | + int dot = e.lastIndexOf('.'); |
| 66 | + if(dot>=0) { |
| 67 | + return e.substring(dot+1); |
| 68 | + } |
| 69 | + return e; |
| 70 | + }).collect(Collectors.toList()); |
| 71 | + ICompilationUnit cu = iType.getCompilationUnit(); |
| 72 | + ASTParser parse = ASTParser.newParser(AST.getJLSLatest()); |
| 73 | + parse.setSource(cu); |
| 74 | + parse.setKind(ASTParser.K_COMPILATION_UNIT); |
| 75 | + |
| 76 | + parse.setResolveBindings(true); |
| 77 | + CompilationUnit ast = (CompilationUnit) parse.createAST(null); |
| 78 | + ast.accept(new ASTVisitor() { |
| 79 | + boolean meth = false; |
| 80 | + boolean found = false; |
| 81 | + @Override |
| 82 | + public boolean visit(MethodDeclaration node) { |
| 83 | + if (node.getName().getIdentifier().equals(nameMethod)) { |
| 84 | + List<Object> parameters = node.parameters(); |
| 85 | + List<String> methodParams = parameters.stream().map(p -> ((SingleVariableDeclaration) p).getType().toString()).toList(); |
| 86 | + if (methodParams.equals(ref)) { |
| 87 | + meth = true; |
| 88 | + for (Object op : node.parameters()) { |
| 89 | + SingleVariableDeclaration parm = (SingleVariableDeclaration) op; |
| 90 | + if (parm.getName().getIdentifier().equals(name)) { |
| 91 | + highlightLine(ast, cu, node.getStartPosition()); |
| 92 | + found = true; |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + return true; |
| 100 | + } |
| 101 | + @Override |
| 102 | + public boolean visit(VariableDeclarationFragment node) { |
| 103 | + if (found) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + if (meth && node.getName().getIdentifier().equals(name)) { |
| 107 | + found = true; |
| 108 | + highlightLine(ast, cu, node.getStartPosition()); |
| 109 | + return true; |
| 110 | + } |
| 111 | + return true; |
| 112 | + } |
| 113 | + private void highlightLine(CompilationUnit ast, ICompilationUnit cu, int startPos) { |
| 114 | + int line = ast.getLineNumber(startPos); |
| 115 | + try { |
| 116 | + IEditorPart editor = JavaUI.openInEditor(cu); |
| 117 | + if (editor instanceof ITextEditor txtEd) { |
| 118 | + IDocumentProvider prov = txtEd.getDocumentProvider(); |
| 119 | + IDocument doc = prov.getDocument(txtEd.getEditorInput()); |
| 120 | + IRegion lineReg = doc.getLineInformation(line - 1); |
| 121 | + txtEd.selectAndReveal(lineReg.getOffset(), lineReg.getLength()); |
| 122 | + } |
| 123 | + } catch (Exception e) { |
| 124 | + System.out.println("Exception: " + e.getMessage()); //$NON-NLS-1$ |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + ResourcesPlugin.getWorkspace(); |
| 129 | + } |
| 130 | + } |
| 131 | + } catch (CoreException e) { |
| 132 | + JDIDebugUIPlugin.statusDialog(e.getStatus()); |
| 133 | + } |
| 134 | + |
| 135 | + return null; |
| 136 | + } |
| 137 | +} |
0 commit comments