Summary: | modified abstract syntax tree using JDT not getting reflected back to original source code file | ||
---|---|---|---|
Product: | [Eclipse Project] JDT | Reporter: | sagar <sagarfree> |
Component: | Core | Assignee: | Stephan Herrmann <stephan.herrmann> |
Status: | VERIFIED INVALID | QA Contact: | |
Severity: | major | ||
Priority: | P3 | CC: | Olivier_Thomann, stephan.herrmann |
Version: | 3.8 | ||
Target Milestone: | 3.8 M2 | ||
Hardware: | PC | ||
OS: | Windows Vista | ||
Whiteboard: | |||
Attachments: |
Description
sagar
2011-08-07 15:16:23 EDT
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. |