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

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

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

问题

我正在与Spoon Inria合作,用于解析和分析Java应用程序。目前,我正在使用以下代码创建输入项目模型。当项目文件保存在磁盘中时,此代码有效。

  1. Launcher spoonAPI = new Launcher();
  2. spoonAPI.addInputResource(projectDirectory); // 项目根目录的路径
  3. spoonAPI.buildModel();
  4. CtModel ctModel = spoonAPI.getModel();

然而,目前我拥有Java文件(类)的内容存储在内存中,我不想将它们写入磁盘,因为这需要很长时间。

我想知道是否有一种方法可以使用Spoon将文件内容传递给解析器。例如,像下面的代码一样:

  1. // Key是类的名称,Value是其内容
  2. Map<String, String> JavafileContents = new HashMap<String, String>();
  3. for (String filePath : JavafileContents.keySet()) {
  4. spoonAPI.parse(JavafileContents.get(filePath));
  5. }
  6. CtModel ctModel = spoonAPI.getModel(); // 提取的模型应包含所有已解析的文件。

类似于JDT提供的类似解决方案。

  1. ASTParser parser = ASTParser.newParser(AST.JLS14);
  2. parser.setSource(javaFileContents.get(filePath).toCharArray());
  3. 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.

  1. Launcher spoonAPI = new Launcher();
  2. spoonAPI.addInputResource(projectDirectory); //The path to the project root directory
  3. spoonAPI.buildModel();
  4. 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.

  1. //Key is name of class and value is its content
  2. Map&lt;String, String&gt; JavafileContents= new HashMap&lt;String, String&gt;();
  3. for (String filePath : JavafileContents.keySet()) {
  4. spoonAPI.parse(JavafileContents.get(filePath ));
  5. }
  6. CtModel ctModel = spoonAPI.getModel(); //The extracted model should contains all parsed files.

Like a similar solution provided in JDT.

  1. ASTParser parser = ASTParser.newParser(AST.JLS14);
  2. parser.setSource(javaFileContents.get(filePath).toCharArray());
  3. CompilationUnit compilationUnit = (CompilationUnit)parser.createAST(null);

答案1

得分: 1

我也在寻找这个功能,我发现下面的代码可能符合您的要求。

  1. import spoon.support.compiler.VirtualFile;
  2. ...
  3. public static CtClass<?> parseClass(String code) {
  4. Launcher launcher = new Launcher();
  5. launcher.addInputResource(new VirtualFile(code));
  6. launcher.getEnvironment().setNoClasspath(true);
  7. launcher.getEnvironment().setAutoImports(true);
  8. ...

源代码位于 https://github.com/INRIA/spoon/blob/spoon-core-8.2.0/src/main/java/spoon/Launcher.java#L856

或者,您也可以使用像 createCodeSnippetStatement 这样的 API

  1. Factory factory = new Launcher().createFactory();
  2. CtStatement tmpStmt = factory.Code().createCodeSnippetStatement(code);

选择使用哪种 API 取决于您的代码片段的类型(例如,表达式或语句)。

英文:

I'm also looking for the functionality, I found the following code may meet your requirement.

  1. import spoon.support.compiler.VirtualFile;
  2. ...
  3. public static CtClass&lt;?&gt; parseClass(String code) {
  4. Launcher launcher = new Launcher();
  5. launcher.addInputResource(new VirtualFile(code));
  6. launcher.getEnvironment().setNoClasspath(true);
  7. launcher.getEnvironment().setAutoImports(true);
  8. ...

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

  1. Factory factory = new Launcher().createFactory();
  2. 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:

确定