|
36 | 36 | import org.apache.commons.codec.digest.DigestUtils; |
37 | 37 | import org.apache.commons.io.FileUtils; |
38 | 38 | import org.apache.commons.lang3.tuple.Pair; |
| 39 | +import org.eclipse.jdt.core.JavaCore; |
39 | 40 | import org.eclipse.jdt.core.dom.ASTVisitor; |
40 | 41 | import org.eclipse.jdt.core.dom.Annotation; |
41 | 42 | import org.eclipse.jdt.core.dom.CompilationUnit; |
@@ -563,36 +564,38 @@ private String[] scanFiles(IJavaProject project, AnnotationHierarchies annotatio |
563 | 564 | PercentageProgressTask progressTask = this.progressService.createPercentageProgressTask(INDEX_FILES_TASK_ID + project.getElementName(), |
564 | 565 | javaFiles.length, "Spring Tools: Indexing Java Sources for '" + project.getElementName() + "'"); |
565 | 566 |
|
566 | | - try { |
567 | | - List<String> nextPassFiles = new ArrayList<>(); |
568 | | - final boolean ignoreMethodBodies = SCAN_PASS.ONE.equals(pass); |
569 | | - |
570 | | - FileASTRequestor requestor = new FileASTRequestor() { |
| 567 | + List<String> nextPassFiles = new ArrayList<>(); |
| 568 | + final boolean ignoreMethodBodies = SCAN_PASS.ONE.equals(pass); |
571 | 569 |
|
572 | | - @Override |
573 | | - public void acceptAST(String sourceFilePath, CompilationUnit cu) { |
574 | | - File file = new File(sourceFilePath); |
575 | | - String docURI = UriUtil.toUri(file).toASCIIString(); |
576 | | - long lastModified = file.lastModified(); |
577 | | - AtomicReference<TextDocument> docRef = new AtomicReference<>(); |
578 | | - |
579 | | - IProblemCollector problemCollector = problemCollectorCreator.apply(docRef, diagnosticsAggregator); |
| 570 | + FileASTRequestor requestor = new FileASTRequestor() { |
580 | 571 |
|
581 | | - SpringIndexerJavaContext context = new SpringIndexerJavaContext(project, cu, docURI, sourceFilePath, |
582 | | - lastModified, docRef, null, generatedSymbols, generatedBeans, problemCollector, pass, nextPassFiles, !ignoreMethodBodies); |
583 | | - |
584 | | - scanAST(context, true); |
585 | | - progressTask.increment(); |
586 | | - } |
587 | | - }; |
588 | | - |
589 | | - ASTParserCleanupEnabled parser = createParser(project, annotations, ignoreMethodBodies); |
590 | | - parser.createASTs(javaFiles, null, new String[0], requestor, null); |
591 | | - parser.cleanup(); |
| 572 | + @Override |
| 573 | + public void acceptAST(String sourceFilePath, CompilationUnit cu) { |
| 574 | + File file = new File(sourceFilePath); |
| 575 | + String docURI = UriUtil.toUri(file).toASCIIString(); |
| 576 | + long lastModified = file.lastModified(); |
| 577 | + AtomicReference<TextDocument> docRef = new AtomicReference<>(); |
| 578 | + |
| 579 | + IProblemCollector problemCollector = problemCollectorCreator.apply(docRef, diagnosticsAggregator); |
| 580 | + |
| 581 | + SpringIndexerJavaContext context = new SpringIndexerJavaContext(project, cu, docURI, sourceFilePath, |
| 582 | + lastModified, docRef, null, generatedSymbols, generatedBeans, problemCollector, pass, nextPassFiles, !ignoreMethodBodies); |
| 583 | + |
| 584 | + scanAST(context, true); |
| 585 | + progressTask.increment(); |
| 586 | + } |
| 587 | + }; |
592 | 588 |
|
| 589 | + ASTParserCleanupEnabled parser = createParser(project, annotations, ignoreMethodBodies); |
| 590 | + try { |
| 591 | + parser.createASTs(javaFiles, null, new String[0], requestor, null); |
593 | 592 | return (String[]) nextPassFiles.toArray(new String[nextPassFiles.size()]); |
| 593 | + } catch (Throwable t) { |
| 594 | + log.error("Failed to index project '%s'".formatted(project.getElementName()), t); |
| 595 | + return new String[0]; |
594 | 596 | } |
595 | 597 | finally { |
| 598 | + parser.cleanup(); |
596 | 599 | progressTask.done(); |
597 | 600 | } |
598 | 601 | } |
@@ -803,8 +806,14 @@ private boolean isJakartaAnnotationWithDefaultSymbol(String qualifiedName) { |
803 | 806 | public static ASTParserCleanupEnabled createParser(IJavaProject project, AnnotationHierarchies annotationHierarchies, boolean ignoreMethodBodies) throws Exception { |
804 | 807 | String[] classpathEntries = getClasspathEntries(project); |
805 | 808 | String[] sourceEntries = getSourceEntries(project); |
| 809 | + String complianceJavaVersion = getComplianceJavaVersion(project.getClasspath().getJavaVersion()); |
806 | 810 |
|
807 | | - return new ASTParserCleanupEnabled(classpathEntries, sourceEntries, annotationHierarchies, ignoreMethodBodies); |
| 811 | + return new ASTParserCleanupEnabled(classpathEntries, sourceEntries, complianceJavaVersion, annotationHierarchies, ignoreMethodBodies); |
| 812 | + } |
| 813 | + |
| 814 | + private static String getComplianceJavaVersion(String javaVersion) { |
| 815 | + // Currently the java version in the classpath seems to be 1.8, 17, 21 etc. |
| 816 | + return javaVersion == null || javaVersion.isBlank() ? JavaCore.VERSION_21 : javaVersion; |
808 | 817 | } |
809 | 818 |
|
810 | 819 | private static String[] getClasspathEntries(IJavaProject project) throws Exception { |
|
0 commit comments