Skip to content

Commit bfb797b

Browse files
committed
fix: leverage jdtutils to get project filepath
1 parent 3effb19 commit bfb797b

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/ProjectCommand.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.eclipse.core.runtime.MultiStatus;
4545
import org.eclipse.core.runtime.OperationCanceledException;
4646
import org.eclipse.core.runtime.Path;
47-
import org.eclipse.jdt.core.IJavaElement;
4847
import org.eclipse.jdt.core.IJavaProject;
4948
import org.eclipse.jdt.core.IMethod;
5049
import org.eclipse.jdt.core.IModuleDescription;
@@ -58,6 +57,7 @@
5857
import org.eclipse.jdt.core.search.SearchPattern;
5958
import org.eclipse.jdt.core.search.SearchRequestor;
6059
import org.eclipse.jdt.launching.JavaRuntime;
60+
import org.eclipse.jdt.ls.core.internal.JDTUtils;
6161
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
6262
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
6363
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
@@ -501,43 +501,22 @@ public static ImportClassContentResult getImportClassContent(List<Object> argume
501501
if (fileUri == null || fileUri.trim().isEmpty()) {
502502
return new ImportClassContentResult(ImportClassContentErrorReason.INVALID_URI, fileUri);
503503
}
504-
// Parse URI manually to avoid restricted API
505-
java.net.URI uri = new java.net.URI(fileUri);
506-
String filePath = uri.getPath();
507-
if (filePath == null) {
508-
return new ImportClassContentResult(ImportClassContentErrorReason.URI_PARSE_FAILED, filePath);
509-
}
510-
511-
IPath path = new Path(filePath);
512504

513-
// Get the file resource
514-
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
515-
IFile file = root.getFileForLocation(path);
516-
if (file == null || !file.exists()) {
517-
return new ImportClassContentResult(ImportClassContentErrorReason.FILE_NOT_FOUND, filePath);
518-
}
519-
if (!file.exists()) {
520-
return new ImportClassContentResult(ImportClassContentErrorReason.FILE_NOT_EXISTS, filePath);
505+
// Directly resolve compilation unit from URI using JDTUtils
506+
java.net.URI uri = JDTUtils.toURI(fileUri);
507+
org.eclipse.jdt.core.ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(uri);
508+
509+
if (compilationUnit == null || !compilationUnit.exists()) {
510+
return new ImportClassContentResult(ImportClassContentErrorReason.FILE_NOT_FOUND, fileUri);
521511
}
522512

523-
// Get the Java project
524-
IJavaProject javaProject = JavaCore.create(file.getProject());
525-
if (javaProject == null) {
526-
return new ImportClassContentResult(ImportClassContentErrorReason.NOT_JAVA_PROJECT, filePath);
527-
}
528-
if (!javaProject.exists()) {
529-
String projectName = javaProject.getProject().getName();
513+
// Get the Java project from the compilation unit
514+
IJavaProject javaProject = compilationUnit.getJavaProject();
515+
if (javaProject == null || !javaProject.exists()) {
516+
String projectName = javaProject != null ? javaProject.getProject().getName() : "unknown";
530517
return new ImportClassContentResult(ImportClassContentErrorReason.PROJECT_NOT_EXISTS, projectName);
531518
}
532519

533-
// Find the compilation unit
534-
IJavaElement javaElement = JavaCore.create(file);
535-
if (!(javaElement instanceof org.eclipse.jdt.core.ICompilationUnit)) {
536-
return new ImportClassContentResult(ImportClassContentErrorReason.NOT_COMPILATION_UNIT, filePath);
537-
}
538-
539-
org.eclipse.jdt.core.ICompilationUnit compilationUnit = (org.eclipse.jdt.core.ICompilationUnit) javaElement;
540-
541520
// Parse imports and resolve local project files
542521
List<ImportClassInfo> classInfoList = new ArrayList<>();
543522

0 commit comments

Comments
 (0)