Skip to content

Commit 8aed2d1

Browse files
Add command to check if the hovered element is main method (#296)
* Add command to check if the hovered element is main method Signed-off-by: Jinbo Wang <[email protected]> * Make checkstyle happy Signed-off-by: Jinbo Wang <[email protected]> * Address review comments Signed-off-by: Jinbo Wang <[email protected]>
1 parent 80d480a commit 8aed2d1

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

com.microsoft.java.debug.plugin/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<command id="vscode.java.resolveMainMethod"/>
1414
<command id="vscode.java.inferLaunchCommandLength"/>
1515
<command id="vscode.java.checkProjectSettings"/>
16+
<command id="vscode.java.resolveElementAtSelection"/>
1617
</delegateCommandHandler>
1718
</extension>
1819
</plugin>

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler
3131
public static final String RESOLVE_MAINMETHOD = "vscode.java.resolveMainMethod";
3232
public static final String INFER_LAUNCH_COMMAND_LENGTH = "vscode.java.inferLaunchCommandLength";
3333
public static final String CHECK_PROJECT_SETTINGS = "vscode.java.checkProjectSettings";
34+
public static final String RESOLVE_ELEMENT_AT_SELECTION = "vscode.java.resolveElementAtSelection";
3435

3536
@Override
3637
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
@@ -60,6 +61,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
6061
return LaunchCommandHandler.getLaunchCommandLength(JsonUtils.fromJson((String) arguments.get(0), LaunchArguments.class));
6162
case CHECK_PROJECT_SETTINGS:
6263
return ProjectSettingsChecker.check(JsonUtils.fromJson((String) arguments.get(0), ProjectSettingsChecker.ProjectSettingsCheckerParams.class));
64+
case RESOLVE_ELEMENT_AT_SELECTION:
65+
return ResolveElementHandler.resolveElementAtSelection(arguments, progress);
6366
default:
6467
break;
6568
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 Microsoft Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Microsoft Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.microsoft.java.debug.plugin.internal;
13+
14+
import java.util.Collections;
15+
import java.util.List;
16+
17+
import org.eclipse.core.runtime.IProgressMonitor;
18+
import org.eclipse.jdt.core.ICompilationUnit;
19+
import org.eclipse.jdt.core.IJavaElement;
20+
import org.eclipse.jdt.core.IMethod;
21+
import org.eclipse.jdt.core.IType;
22+
import org.eclipse.jdt.core.JavaModelException;
23+
import org.eclipse.jdt.ls.core.internal.JDTUtils;
24+
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
25+
26+
import com.microsoft.java.debug.core.DebugException;
27+
28+
public class ResolveElementHandler {
29+
30+
/**
31+
* Resolve the Java element at the selected position.
32+
* @return the resolved Java element information.
33+
*/
34+
public static Object resolveElementAtSelection(List<Object> arguments, IProgressMonitor monitor) throws DebugException {
35+
if (arguments == null || arguments.size() < 3) {
36+
return Collections.emptyList();
37+
}
38+
39+
String uri = (String) arguments.get(0);
40+
int line = (int) Math.round((double) arguments.get(1));
41+
int column = (int) Math.round((double) arguments.get(2));
42+
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
43+
try {
44+
IJavaElement element = JDTUtils.findElementAtSelection(unit, line, column,
45+
JavaLanguageServerPlugin.getPreferencesManager(), monitor);
46+
if (element instanceof IMethod) {
47+
return new JavaElement(((IMethod) element).getDeclaringType().getFullyQualifiedName(),
48+
element.getJavaProject().getProject().getName(),
49+
((IMethod) element).isMainMethod());
50+
} else if (element instanceof IType) {
51+
return new JavaElement(((IType) element).getFullyQualifiedName(),
52+
element.getJavaProject().getProject().getName(),
53+
ResolveMainMethodHandler.getMainMethod((IType) element) != null);
54+
}
55+
} catch (JavaModelException e) {
56+
throw new DebugException("Failed to resolve the selected element information: " + e.getMessage(), e);
57+
}
58+
59+
return null;
60+
}
61+
62+
static class JavaElement {
63+
private String declaringType;
64+
private String projectName;
65+
private boolean hasMainMethod;
66+
67+
public JavaElement(String declaringType, String projectName, boolean hasMainMethod) {
68+
this.declaringType = declaringType;
69+
this.projectName = projectName;
70+
this.hasMainMethod = hasMainMethod;
71+
}
72+
}
73+
}

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveMainMethodHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ private static List<IMethod> searchMainMethods(ICompilationUnit compilationUnit)
9595
return result;
9696
}
9797

98-
private static IMethod getMainMethod(IType type) throws JavaModelException {
98+
/**
99+
* Returns the main method defined in the type.
100+
*/
101+
public static IMethod getMainMethod(IType type) throws JavaModelException {
99102
for (IMethod method : type.getMethods()) {
100103
// Have at most one main method in the member methods of the type.
101104
if (method.isMainMethod()) {

0 commit comments

Comments
 (0)