|
44 | 44 | import org.eclipse.core.runtime.MultiStatus; |
45 | 45 | import org.eclipse.core.runtime.OperationCanceledException; |
46 | 46 | import org.eclipse.core.runtime.Path; |
47 | | -import org.eclipse.jdt.core.IJavaElement; |
48 | 47 | import org.eclipse.jdt.core.IJavaProject; |
49 | 48 | import org.eclipse.jdt.core.IMethod; |
50 | 49 | import org.eclipse.jdt.core.IModuleDescription; |
|
58 | 57 | import org.eclipse.jdt.core.search.SearchPattern; |
59 | 58 | import org.eclipse.jdt.core.search.SearchRequestor; |
60 | 59 | import org.eclipse.jdt.launching.JavaRuntime; |
| 60 | +import org.eclipse.jdt.ls.core.internal.JDTUtils; |
61 | 61 | import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; |
62 | 62 | import org.eclipse.jdt.ls.core.internal.ProjectUtils; |
63 | 63 | import org.eclipse.jdt.ls.core.internal.ResourceUtils; |
@@ -501,43 +501,22 @@ public static ImportClassContentResult getImportClassContent(List<Object> argume |
501 | 501 | if (fileUri == null || fileUri.trim().isEmpty()) { |
502 | 502 | return new ImportClassContentResult(ImportClassContentErrorReason.INVALID_URI, fileUri); |
503 | 503 | } |
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); |
512 | 504 |
|
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); |
521 | 511 | } |
522 | 512 |
|
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"; |
530 | 517 | return new ImportClassContentResult(ImportClassContentErrorReason.PROJECT_NOT_EXISTS, projectName); |
531 | 518 | } |
532 | 519 |
|
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 | | - |
541 | 520 | // Parse imports and resolve local project files |
542 | 521 | List<ImportClassInfo> classInfoList = new ArrayList<>(); |
543 | 522 |
|
|
0 commit comments