Batch compiler AST
This commit is contained in:
@@ -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];
|
||||
|
||||
@@ -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())*/));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user