如何将JDT中修改后的抽象语法树反映回原始java源文件?
请参阅文章的“写下来”部分 http://www.eclipse.org/articles/article .php?file=Article-javaCodeManipulation_AST/index.html
我正在解析一个java源代码文件,该文件具有使用cofoja编写的合约的方法。现在,当我创建输入文件的抽象语法树(ast)并修改它时。它可以显示Document文档,正在修改的对象。但是,当我尝试将此文档反映回原始源文件时,以下声明会引发异常:
// get the buffer manager
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
MyVisitor.java 中的 ITextFileBufferManager bufferManager
声明引发以下异常
Exception in thread "main" java.lang.ExceptionInInitializerError
at ASTModifier.main(ASTModifier.java:205)
Caused by: java.lang.IllegalStateException: Workspace is closed.
at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:340)
at org.eclipse.core.filebuffers.FileBuffers.<clinit>(FileBuffers.java:52)
... 1 more
因此,我无法更改原始java文件。我在网上找到的链接之一: http://www.programcreek .com/2011/05/java-lang-illegalstateexception-workspace- is-close/#comment-1939
它说:简单来说,这是由于简单地将依赖的jar文件添加到常规java项目。要使用 JDT,您需要让程序作为插件(或者至少是支持 OSGi 的应用程序)运行,而不是将其作为 jar 使用。
由于我正在创建一个简单的 java 项目,那么使用 FileBuffers 类是否存在问题?我需要创建插件吗?
Kindly refer "write it down" section of article
http://www.eclipse.org/articles/article.php?file=Article-javaCodeManipulation_AST/index.html
I am parsing a java source code file which has method with contracts written using cofoja. Now when I create abstract syntax tree (ast) of the input file, and modify it. It can show me that Document document, object being modified. But when I try to reflect this document back to the original source file, the following declaration throws an exception:
// get the buffer manager
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
The following exception gets thrown for ITextFileBufferManager bufferManager
declaration in MyVisitor.java
Exception in thread "main" java.lang.ExceptionInInitializerError
at ASTModifier.main(ASTModifier.java:205)
Caused by: java.lang.IllegalStateException: Workspace is closed.
at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:340)
at org.eclipse.core.filebuffers.FileBuffers.<clinit>(FileBuffers.java:52)
... 1 more
Because of this, I am not able to change the original java file. One of the link I found on net : http://www.programcreek.com/2011/05/java-lang-illegalstateexception-workspace- is-closed/#comment-1939
It says: In brief, this is caused by simply adding dependent jar files to regular java project. To use JDT, you need to have the program running as a plug-in (or at least, an OSGi- enabled application) rather than using it as a jar.
Since I am creating a simple java project, is that a problem for using FileBuffers class? Do I need to create plug in instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答:是的。如果您正在使用打开的工作区运行(即,您已经编写了 Eclipse 插件),则只能使用 JDT API。
如果您想编写一个使用 Eclipse API 的简单程序,您可能想编写一个 RCP 应用程序。这允许您使用 sib0set Eclipse 插件来创建一些功能。
关于 RCP 的一个很好的教程在这里:
http://www.vogella.de/articles/EclipseRCP /article.html
Short answer: yes. You can only use JDT API if you are running with a workspace that is opened (i.e., you have written an Eclipse plugin).
If you want to write a simple program that uses Eclipse API, you probably want to write an RCP application. This allows you to use a sib0set of Eclipse plugins to create some functionality.
A good tutorial on RCP is here:
http://www.vogella.de/articles/EclipseRCP/article.html