开发者

How to reflect modified abstract syntax tree in JDT back to original java source file.?

开发者 https://www.devze.com 2023-03-26 06:21 出处:网络
Kindly refer \"write it down\" section of article http://www.eclipse.org/articles/article.php?file=Arti开发者_StackOverflow中文版cle-javaCodeManipulation_AST/index.html

Kindly refer "write it down" section of article http://www.eclipse.org/articles/article.php?file=Arti开发者_StackOverflow中文版cle-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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消