英文:
Build AST Model Using Spoon when input project files are located in memory and not saved as files in disk
问题
我正在与Spoon Inria合作,用于解析和分析Java应用程序。目前,我正在使用以下代码创建输入项目模型。当项目文件保存在磁盘中时,此代码有效。
Launcher spoonAPI = new Launcher();
spoonAPI.addInputResource(projectDirectory); // 项目根目录的路径
spoonAPI.buildModel();
CtModel ctModel = spoonAPI.getModel();
然而,目前我拥有Java文件(类)的内容存储在内存中,我不想将它们写入磁盘,因为这需要很长时间。
我想知道是否有一种方法可以使用Spoon将文件内容传递给解析器。例如,像下面的代码一样:
// Key是类的名称,Value是其内容
Map<String, String> JavafileContents = new HashMap<String, String>();
for (String filePath : JavafileContents.keySet()) {
spoonAPI.parse(JavafileContents.get(filePath));
}
CtModel ctModel = spoonAPI.getModel(); // 提取的模型应包含所有已解析的文件。
类似于JDT提供的类似解决方案。
ASTParser parser = ASTParser.newParser(AST.JLS14);
parser.setSource(javaFileContents.get(filePath).toCharArray());
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
英文:
I am working with Spoon Inria to parse and analyze Java applications. Currently, I am using the below code to create input project model. This code works when project files are saved in the disk.
Launcher spoonAPI = new Launcher();
spoonAPI.addInputResource(projectDirectory); //The path to the project root directory
spoonAPI.buildModel();
CtModel ctModel = spoonAPI.getModel();
However, currently I have contents of Java files (classes) in the memory and I do not want to write them in the disk as it takes ages.
I was wondering if there is a way using Spoon that I can pass file contents to the parser. For example, a code as below.
//Key is name of class and value is its content
Map<String, String> JavafileContents= new HashMap<String, String>();
for (String filePath : JavafileContents.keySet()) {
spoonAPI.parse(JavafileContents.get(filePath ));
}
CtModel ctModel = spoonAPI.getModel(); //The extracted model should contains all parsed files.
Like a similar solution provided in JDT.
ASTParser parser = ASTParser.newParser(AST.JLS14);
parser.setSource(javaFileContents.get(filePath).toCharArray());
CompilationUnit compilationUnit = (CompilationUnit)parser.createAST(null);
答案1
得分: 1
我也在寻找这个功能,我发现下面的代码可能符合您的要求。
import spoon.support.compiler.VirtualFile;
...
public static CtClass<?> parseClass(String code) {
Launcher launcher = new Launcher();
launcher.addInputResource(new VirtualFile(code));
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setAutoImports(true);
...
源代码位于 https://github.com/INRIA/spoon/blob/spoon-core-8.2.0/src/main/java/spoon/Launcher.java#L856。
或者,您也可以使用像 createCodeSnippetStatement
这样的 API
Factory factory = new Launcher().createFactory();
CtStatement tmpStmt = factory.Code().createCodeSnippetStatement(code);
选择使用哪种 API 取决于您的代码片段的类型(例如,表达式或语句)。
英文:
I'm also looking for the functionality, I found the following code may meet your requirement.
import spoon.support.compiler.VirtualFile;
...
public static CtClass<?> parseClass(String code) {
Launcher launcher = new Launcher();
launcher.addInputResource(new VirtualFile(code));
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setAutoImports(true);
...
The source is https://github.com/INRIA/spoon/blob/spoon-core-8.2.0/src/main/java/spoon/Launcher.java#L856.
Alternatively, one can use APIs like createCodeSnippetStatement
Factory factory = new Launcher().createFactory();
CtStatement tmpStmt = factory.Code().createCodeSnippetStatement(code);
The way of choosing an API depends on the type of your code snippet (e.g., expression or statement).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论