Summary
The Abstract Syntax Tree is the base framework for many powerful tools of the Eclipse IDE, including refactoring, Quick Fix and Quick Assist. The Abstract Syntax Tree maps plain Java source code in a tree form. This tree is more convenient and reliable to analyse and modify programmatically than text-based source. This article shows how you can use the Abstract Syntax Tree for your own applications.
By Thomas Kuhn, Eye Media GmbHOlivier Thomann, IBM Ottawa Lab
Complete Article
Hi Deepak,
ReplyDeleteHow can i change an Abstract syntax tree of simple java source code and reprint the modified java source code.? Kindly help me.
@Sagar: The article gives the complete details, are you stuck at a particular point ?
ReplyDeleteHey Hi deepak.
ReplyDeleteThanks for reply. as per the article, I can create abstract syntax tree (ast) for a java source file, and modify ast. But, the reflecting the changes back to the original source code file is not working.
Kindly refer "Write it down" section of article . It says to declare:
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
// get the buffer manager
But I am getting following Exception at this line.
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.(FileBuffers.java:52)
... 1 more
What can be this error? everything else works fine. But for making the changes of ast reflected back to the original source file is not working?
Kindly help
thanks
regards
sagar
Hi deepak, In continuation of the previous problem, I have got a link :
ReplyDeletehttp://www.programcreek.com/2011/05/java-lang-illegalstateexception-workspace-is-closed/#comment-1939
It says that: " 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. "
So, doest it mean I have to create a plug in project , instead of simple java project for using FileBuffers.getTextFileBufferManager() method? But article doesnt point out this issue
waiting for reply.
Thanks
Yup, all JDT apis can be used easily from a plug-in. If you have a simple java project, you can use some apis but not all and have to jump through more hoops.
ReplyDeleteWhile the article does not say that use a plug-in, the example project is a plug-in.
Alright.thanks deepak. I have come across another issue. I want to insert code inside a method's body using JDT.
ReplyDeleteEx:
String code= " a = a -1;
val = false;
return a; "
Now , above code string, I want to insert into a method's body.
for ex: Method b4 inserting is
public int decrement(int a){
}
and method after inserting should be:
public int decrement(int a){
a = a -1;
val = false;
return a; "
}
----------------
How this can be done? I am able to find out a particular method, code string is also ready.
Is there any way I can create block of statements and directly add to a method body?
or do I have to create individually 3 expressions (for above case) and then add it individually to the method?
Kindly help
Thanks
regards
sagar