Build AST Model Using Spoon when input project files are located in memory and not saved as files in disk

huangapple go评论75阅读模式
英文:

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&lt;String, String&gt; JavafileContents= new HashMap&lt;String, String&gt;();

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&lt;?&gt; 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).

huangapple
  • 本文由 发表于 2020年9月7日 14:37:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63772488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定