Batch compiler AST

This commit is contained in:
aboyko
2023-03-29 10:30:59 -04:00
parent 0c06718dbf
commit 7ab38906ce
3 changed files with 98 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
<properties>
<dependencies.version>${project.version}</dependencies.version>
<jdt.core.version>3.33.0</jdt.core.version>
<jdt.core.compiler.batch.version>3.7.0</jdt.core.compiler.batch.version>
<lsp4xml.version>0.24.0</lsp4xml.version>
</properties>
@@ -49,6 +50,11 @@
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>jabylon</id>
<url>https://www.jabylon.org/maven/</url>
</repository>
</repositories>
<dependencies>
@@ -115,6 +121,13 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>batch</artifactId>
<version>${jdt.core.compiler.batch.version}</version>
</dependency>
<!-- the 3.10.1 org.eclipse.platform fixed the pb -->
<dependency>
<groupId>org.eclipse.platform</groupId>

View File

@@ -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<Classpath> classpaths = createClasspath(getClasspathEntries(project));
return parse3(source, docURI, unitName, classpaths, null);
}
public static CompilationUnitDeclaration parse3(char[] source, String docURI, String unitName, List<Classpath> classpaths, INameEnvironmentWithProgress environment) {
Map<String, String> 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<String, String> createCompilerOptions() {
Map<String, String> options = JavaCore.getOptions();
String apiLevel = JavaCore.VERSION_19;
JavaCore.setComplianceOptions(apiLevel, options);
return options;
}
public static List<Classpath> createClasspath(IJavaProject jp) throws Exception {
return createClasspath(getClasspathEntries(jp));
}
private static List<Classpath> 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<List<Classpath>, INameEnvironmentWithProgress> createLookupEnvTuple(IJavaProject jp) throws Exception {
List<Classpath> 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];

View File

@@ -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<List<Classpath>, 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())*/));
}
}