Skip to content

Commit de51193

Browse files
Check whether java file is on classpath (#302)
* Check whether java file is on classpath Signed-off-by: Jinbo Wang <[email protected]> * Throw exception for illgeal case Signed-off-by: Jinbo Wang <[email protected]>
1 parent 531b21a commit de51193

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<command id="vscode.java.checkProjectSettings"/>
1616
<command id="vscode.java.resolveElementAtSelection"/>
1717
<command id="vscode.java.resolveBuildFiles"/>
18+
<command id="vscode.java.isOnClasspath"/>
1819
</delegateCommandHandler>
1920
</extension>
2021
</plugin>

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import org.eclipse.core.resources.IFile;
1818
import org.eclipse.core.resources.ResourcesPlugin;
1919
import org.eclipse.core.runtime.IProgressMonitor;
20+
import org.eclipse.jdt.core.ICompilationUnit;
2021
import org.eclipse.jdt.core.IJavaProject;
2122
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
23+
import org.eclipse.jdt.ls.core.internal.JDTUtils;
2224
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
2325
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
2426

27+
import com.microsoft.java.debug.core.DebugException;
2528
import com.microsoft.java.debug.core.UsageDataStore;
2629
import com.microsoft.java.debug.core.protocol.JsonUtils;
2730
import com.microsoft.java.debug.core.protocol.Requests.LaunchArguments;
@@ -39,6 +42,7 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler
3942
public static final String CHECK_PROJECT_SETTINGS = "vscode.java.checkProjectSettings";
4043
public static final String RESOLVE_ELEMENT_AT_SELECTION = "vscode.java.resolveElementAtSelection";
4144
public static final String RESOLVE_BUILD_FILES = "vscode.java.resolveBuildFiles";
45+
public static final String IS_ON_CLASSPATH = "vscode.java.isOnClasspath";
4246

4347
@Override
4448
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
@@ -72,6 +76,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
7276
return ResolveElementHandler.resolveElementAtSelection(arguments, progress);
7377
case RESOLVE_BUILD_FILES:
7478
return getBuildFiles();
79+
case IS_ON_CLASSPATH:
80+
return isOnClasspath(arguments);
7581
default:
7682
break;
7783
}
@@ -97,4 +103,19 @@ private List<String> getBuildFiles() {
97103

98104
return result;
99105
}
106+
107+
private boolean isOnClasspath(List<Object> arguments) throws DebugException {
108+
if (arguments.size() < 1) {
109+
throw new DebugException("No file uri is specified.");
110+
}
111+
112+
String uri = (String) arguments.get(0);
113+
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
114+
if (unit == null || unit.getResource() == null || !unit.getResource().exists()) {
115+
throw new DebugException("The compilation unit " + uri + " doesn't exist.");
116+
}
117+
118+
IJavaProject javaProject = unit.getJavaProject();
119+
return javaProject == null || javaProject.isOnClasspath(unit);
120+
}
100121
}

0 commit comments

Comments
 (0)