简体   繁体   English

Eclipse JDT AST:如何将生成的AST写入java文件?

[英]Eclipse JDT AST: How to write generated AST to java file?

I am currently working with eclipse AST to generate source code. 我目前正在使用eclipse AST来生成源代码。 Other than in most examples, I am generating the source code from scratch and in a stand-alone application, as opposed to an eclipse plug-in. 除了在大多数示例中,我从头开始在独立应用程序中生成源代码,而不是eclipse插件。

When read in from an ASTParser, you can activate modifications by calling recordModifications() , but this doesn't work when the AST is created from scratch, eg by calling newCompilationUnit() . 从ASTParser读入时,您可以通过调用recordModifications()来激活修改,但是当从头开始创建AST时,例如通过调用newCompilationUnit() ,这不起作用。

Consequently, writing the source to file via a Document and TextEdit is not possible - there is an exception saying that modification recording hasn't been enabled. 因此,无法通过DocumentTextEdit将源写入文件 - 有一个例外,即尚未启用修改记录。
Any experiences in generating AST from scratch and writing to file? 从头开始生成AST并写入文件的经验? Thanks! 谢谢!

Why not create the file first, then generate an AST from it, like this: 为什么不首先创建文件,然后从中生成AST,如下所示:

ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
parser.setSource(unit);
// Parse the source code and generate an AST.
CompilationUnit ast = (CompilationUnit) parser.createAST(null);

If the file is newly created and blank, then presumably the AST will be empty, You can then replace the root of the ast object and write it to the file. 如果文件是新创建的并且是空白的,那么可能AST将为空,然后您可以替换ast对象的根并将其写入文件。 Also, if you are not tied to Eclipse, you could just do the same thing using the JSR199 standard and write that AST to a file in the normal way. 此外,如果您不依赖于Eclipse,您可以使用JSR199标准执行相同的操作,并以正常方式将该AST写入文件。 See here for an introduction. 请看这里的介绍。

Thank you for your suggestions! 谢谢你的建议! In the meantime I found a way to emit the source code via an internal class "ASTFlattener", which creates a String representation of an AST. 与此同时,我找到了一种通过内部类“ASTFlattener”发出源代码的方法,该类创建了AST的String表示。 This works pretty well for me.... 这对我很有用....

I've just been trying to find the answer to the same question. 我一直试图找到同一个问题的答案。 Based on the answers above, the most elegant solution I've found so far looks like: 基于上面的答案,到目前为止我找到的最优雅的解决方案如下:

ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setCompilerOptions(....);

Document document = new Document("");
parser.setSource(document.get().toCharArray());
CompilationUnit unit = (CompilationUnit)parser.createAST(null);
unit.recordModifications();

TextEdit edits = unit.rewrite(document, null);
edits.apply(document);

// now write document.get() to file

If you're generating the source from scratch, maybe you could write the minimal source able to be parsed to a temporary file, apply your modifications to the Ast, then write it back to that file as a text edit as you do otherwise. 如果您从头开始生成源代码,也许您可​​以编写能够解析为临时文件的最小源代码,将修改应用于Ast,然后将其作为文本编辑将其写回该文件。 Then read the source. 然后阅读来源。 You could then transfer this to wherever you want. 然后,您可以将其传输到您想要的任何位置。

There is probably a more elegant solution, but this may work. 可能有更优雅的解决方案,但这可能有效。

After created a compilation unit from AST I'm doing: 从AST创建了一个编译单元后我正在做:

AST ast = AST.newAST(AST.JLS4);    
CompilationUnit unit = ast.newCompilationUnit();

... fill Compilation Unit and then ... ...填写编译单元然后......

package.createCompilationUnit(className + ".java", unit.toString(), true, null);

Where: 哪里:
package = IPackageFragment package = IPackageFragment
className = String (generated class name) className = String (生成的类名)
unit => CompilationUnit ast unit => CompilationUnit ast
true => force replace true =>强制替换
null => progress monitor null =>进度监视器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM