4444import org .eclipse .core .runtime .MultiStatus ;
4545import org .eclipse .core .runtime .OperationCanceledException ;
4646import org .eclipse .core .runtime .Path ;
47- import org .eclipse .jdt .core .IJavaElement ;
4847import org .eclipse .jdt .core .IJavaProject ;
4948import org .eclipse .jdt .core .IMethod ;
5049import org .eclipse .jdt .core .IModuleDescription ;
5857import org .eclipse .jdt .core .search .SearchPattern ;
5958import org .eclipse .jdt .core .search .SearchRequestor ;
6059import org .eclipse .jdt .launching .JavaRuntime ;
60+ import org .eclipse .jdt .ls .core .internal .JDTUtils ;
6161import org .eclipse .jdt .ls .core .internal .JavaLanguageServerPlugin ;
6262import org .eclipse .jdt .ls .core .internal .ProjectUtils ;
6363import org .eclipse .jdt .ls .core .internal .ResourceUtils ;
@@ -155,7 +155,7 @@ public String getMessage() {
155155 * Error context information for operations
156156 */
157157 public static class ErrorContext {
158- public final String errorValue ; // The value that caused the error (e.g., invalid URI, null parsedPath, etc.)
158+ public final String errorValue ; // The value that caused the error (e.g., invalid URI, null parsedPath, etc.)
159159
160160 public ErrorContext (String errorValue ) {
161161 this .errorValue = errorValue ;
@@ -491,7 +491,7 @@ public static ImportClassContentResult getImportClassContent(List<Object> argume
491491 // Record start time for timeout control
492492 long startTime = System .currentTimeMillis ();
493493 final long TIMEOUT_MS = 80 ; // 80ms timeout
494-
494+
495495 if (arguments == null || arguments .isEmpty ()) {
496496 return new ImportClassContentResult (ImportClassContentErrorReason .NULL_ARGUMENTS );
497497 }
@@ -501,43 +501,24 @@ 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- }
510504
511- IPath path = new Path (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 );
512508
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 );
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 () != null
517+ ? javaProject .getProject ().getName ()
518+ : "unknown" ;
530519 return new ImportClassContentResult (ImportClassContentErrorReason .PROJECT_NOT_EXISTS , projectName );
531520 }
532521
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-
541522 // Parse imports and resolve local project files
542523 List <ImportClassInfo > classInfoList = new ArrayList <>();
543524
@@ -580,28 +561,30 @@ public static ImportClassContentResult getImportClassContent(List<Object> argume
580561 // Check if we have exceeded the timeout before starting external resolution
581562 long currentTime = System .currentTimeMillis ();
582563 long elapsedTime = currentTime - startTime ;
583-
564+
584565 if (elapsedTime >= TIMEOUT_MS ) {
585566 // Return early due to timeout, but still return what we have collected so far
586567 if (classInfoList .isEmpty ()) {
587- return new ImportClassContentResult (ImportClassContentErrorReason .TIME_LIMIT_EXCEEDED , String .valueOf (elapsedTime ) + "ms" );
568+ return new ImportClassContentResult (ImportClassContentErrorReason .TIME_LIMIT_EXCEEDED ,
569+ String .valueOf (elapsedTime ) + "ms" );
588570 }
589571 return new ImportClassContentResult (classInfoList );
590572 }
591-
573+
592574 List <ImportClassInfo > externalClasses = new ArrayList <>();
593575
594576 for (org .eclipse .jdt .core .IImportDeclaration importDecl : imports ) {
595577 // Check cancellation before each external resolution
596578 if (monitor .isCanceled ()) {
597579 break ;
598580 }
599-
581+
600582 // Check timeout before each external resolution
601583 currentTime = System .currentTimeMillis ();
602584 elapsedTime = currentTime - startTime ;
603585 if (elapsedTime >= TIMEOUT_MS ) {
604- // Timeout reached, stop processing external dependencies but keep existing results
586+ // Timeout reached, stop processing external dependencies but keep existing
587+ // results
605588 break ;
606589 }
607590
@@ -673,8 +656,9 @@ private static String getSeverityString(int severity) {
673656 * Get project dependencies information including JDK version.
674657 *
675658 * @param arguments List containing the file URI as the first element
676- * @param monitor Progress monitor for cancellation support
677- * @return List of DependencyInfo containing key-value pairs of project information
659+ * @param monitor Progress monitor for cancellation support
660+ * @return List of DependencyInfo containing key-value pairs of project
661+ * information
678662 */
679663 public static ProjectDependenciesResult getProjectDependencies (List <Object > arguments ,
680664 IProgressMonitor monitor ) {
0 commit comments