Community
Participate
Working Groups
Build Identifier: Version: Helios Service Release 2 Build id: 20110301-1815 I can create the abstract syntax tree, modify it, but when I try to reflect the modified ast back to original source java file, it is not working because one declaration in program generates exception. Declaration is: ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); // to get the buffer manager Kindly refer to "write it down" section of following article: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html It says:" Within Eclipse, Java editors do not write directly on a file resource, but on a shared working copy instead." But declaration generates exception ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager(); Kindly let me know, what is the problem? Reproducible: Always Steps to Reproduce: 1.Load all the files MyVisitor.java, ASTModifier.java, Input2.java in eclipse3.6.1 2. Add all the required jar files provided in compressed zip folder to build path 3. Execute ASTModifier.java 4. You will see the method "display1() from Input2.java being modified to "NewMethod(int newVar)" in the resulting document object and being displayed. 5. However, for making this document reflected back to original source file Input2.java, is not possible, because of the exception being generated.
Created attachment 201050 [details] ASTModifier is main program which parses Input2.java file using MyVisitor.java code file ASTModifier.java file parses Input2.java file. It creates AST, modifies it, but changes are not getting reflected back to original source file Input2.java, becasue Exception is generated. Following exception gets generated for ITextFileBufferManager bufferManager declaration in ASTModifier.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
Created attachment 201051 [details] MyVisitor extends ASTVisitor class to parse methods, annotations
Created attachment 201052 [details] Input2.java is the file to be parsed .
Kindly refer this link to get all the require jar files to be added to project build path: https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0BzL4Sq459P5-Y2NlMWMxNzktZDZlNS00MjdiLWEwZGEtNTIwZmY5ZGJiYjQ5&hl=en_US
Sagar, as you suggested in your email the problem is indeed that your project is a plain Java application, not a Plug-in. The Java application cannot access the Eclipse workspace, which, however, it is required for using the full approach described in the mentioned article. I see two options: * convert your program into a plug-in + pro: it can be smoothly integrated with the IDE including a menu action or s.t. like that - con: you'll have to learn how to launch a runtime workbench and deploy a plug-in. * don't use AST rewriting but simply construct the AST and then convert the AST to a String and do the file writing yourself. + pro: less configuration - con: after writing the files you'll have to synchronize the workspace so that Eclipse will see the new/modified file. And I'm not sure about formatting in this variant. Just pick your choice :) The behavior you describe is expected.
It is possible to use the ASTRewrite outside of a headless Eclipse, but the buffer manager cannot be used in that context.
Verified for 3.8M2.