From 7ab38906ceef2b686b20489df7fc462557523918 Mon Sep 17 00:00:00 2001 From: aboyko Date: Wed, 29 Mar 2023 10:30:59 -0400 Subject: [PATCH] Batch compiler AST --- .../spring-boot-language-server/pom.xml | 13 ++++++ .../boot/java/utils/CompilationUnitCache.java | 45 +++++++++++++++++++ .../boot/java/utils/test/AstParserTest.java | 41 ++++++++++++++++- 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/headless-services/spring-boot-language-server/pom.xml b/headless-services/spring-boot-language-server/pom.xml index 3c0e4a36c..510084a84 100644 --- a/headless-services/spring-boot-language-server/pom.xml +++ b/headless-services/spring-boot-language-server/pom.xml @@ -16,6 +16,7 @@ ${project.version} 3.33.0 + 3.7.0 0.24.0 @@ -49,6 +50,11 @@ false + + + jabylon + https://www.jabylon.org/maven/ + @@ -115,6 +121,13 @@ + + + org.eclipse.jdt.core.compiler + batch + ${jdt.core.compiler.batch.version} + + org.eclipse.platform diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/utils/CompilationUnitCache.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/utils/CompilationUnitCache.java index 8165daffb..c86459a32 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/utils/CompilationUnitCache.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/utils/CompilationUnitCache.java @@ -278,6 +278,45 @@ public final class CompilationUnitCache implements DocumentContentProvider { return cu; } + public static CompilationUnitDeclaration parse3(char[] source, String docURI, String unitName, IJavaProject project) throws Exception { + List classpaths = createClasspath(getClasspathEntries(project)); + return parse3(source, docURI, unitName, classpaths, null); + } + + public static CompilationUnitDeclaration parse3(char[] source, String docURI, String unitName, List classpaths, INameEnvironmentWithProgress environment) { + Map options = JavaCore.getOptions(); + String apiLevel = JavaCore.VERSION_19; + JavaCore.setComplianceOptions(apiLevel, options); + if (environment == null) { + environment = CUResolver.createLookupEnvironment(classpaths.toArray(new Classpath[classpaths.size()])); + } + + BasicCompilationUnit sourceUnit = new BasicCompilationUnit(source, null, unitName, (IJavaElement) null); + + int flags = 0; + flags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY; + flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY; + CompilationUnitDeclaration unit = null; + try { + unit = CUResolver.resolve(sourceUnit, classpaths, options, flags, environment); + } catch (Exception e) { + flags &= ~ICompilationUnit.ENABLE_BINDINGS_RECOVERY; + unit = CUResolver.parse(sourceUnit, options, flags); + } + return unit; + } + + public static Map createCompilerOptions() { + Map options = JavaCore.getOptions(); + String apiLevel = JavaCore.VERSION_19; + JavaCore.setComplianceOptions(apiLevel, options); + return options; + } + + public static List createClasspath(IJavaProject jp) throws Exception { + return createClasspath(getClasspathEntries(jp)); + } + private static List createClasspath(String[] classpathEntries) { ASTParser parser = ASTParser.newParser(AST.JLS19); String[] sourceEntries = new String[] {}; @@ -298,6 +337,12 @@ public final class CompilationUnitCache implements DocumentContentProvider { } } + public static Tuple2, INameEnvironmentWithProgress> createLookupEnvTuple(IJavaProject jp) throws Exception { + List classpaths = createClasspath(getClasspathEntries(jp)); + INameEnvironmentWithProgress environment = CUResolver.createLookupEnvironment(classpaths.toArray(new Classpath[classpaths.size()])); + return Tuples.of(classpaths, environment); + } + private static String[] getClasspathEntries(IJavaProject project) throws Exception { if (project == null) { return new String[0]; diff --git a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/utils/test/AstParserTest.java b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/utils/test/AstParserTest.java index 6c1f94c8e..5fa5018c7 100644 --- a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/utils/test/AstParserTest.java +++ b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/utils/test/AstParserTest.java @@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.net.URI; import java.net.URL; +import java.util.List; import org.apache.commons.io.IOUtils; import org.eclipse.jdt.core.dom.ASTVisitor; @@ -28,6 +29,13 @@ import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.NormalAnnotation; import org.eclipse.jdt.core.dom.SingleMemberAnnotation; import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; +import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope; +import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment; +import org.eclipse.jdt.internal.compiler.lookup.MethodScope; +import org.eclipse.jdt.internal.core.INameEnvironmentWithProgress; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.ide.vscode.boot.java.links.SourceLinks; @@ -35,6 +43,8 @@ import org.springframework.ide.vscode.boot.java.utils.CompilationUnitCache; import org.springframework.ide.vscode.commons.maven.java.MavenJavaProject; import org.springframework.ide.vscode.project.harness.ProjectsHarness; +import reactor.util.function.Tuple2; + public class AstParserTest { @@ -119,5 +129,34 @@ public class AstParserTest { }); } - + + @Test + void testCuDeclaration() throws Exception { + URL sourceUrl = SourceLinks.source(jp, "org.springframework.boot.SpringApplication").get(); + + URI uri = sourceUrl.toURI(); + + String unitName = "SpringApplication"; + + char[] content = IOUtils.toString(uri).toCharArray(); + + Tuple2, INameEnvironmentWithProgress> envTuple = CompilationUnitCache.createLookupEnvTuple(jp); + + + CompilationUnitDeclaration cu = CompilationUnitCache.parse3(content, uri.toASCIIString(), unitName, envTuple.getT1(), envTuple.getT2()); + + assertNotNull(cu); + + cu.traverse(new org.eclipse.jdt.internal.compiler.ASTVisitor() { + + @Override + public boolean visit(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration fieldDeclaration, + MethodScope scope) { + // TODO Auto-generated method stub + return super.visit(fieldDeclaration, scope); + } + + }, new CompilationUnitScope(cu, new CompilerOptions(CompilationUnitCache.createCompilerOptions())/*new LookupEnvironment(null, new CompilerOptions(CompilationUnitCache.createCompilerOptions()), null, envTuple.getT2())*/)); + } + }