Bug 354094 - modified abstract syntax tree using JDT not getting reflected back to original source code file
Summary: modified abstract syntax tree using JDT not getting reflected back to origina...
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows Vista
: P3 major (vote)
Target Milestone: 3.8 M2   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-08-07 15:16 EDT by sagar CLA
Modified: 2011-09-13 11:47 EDT (History)
2 users (show)

See Also:


Attachments
ASTModifier is main program which parses Input2.java file using MyVisitor.java code file (8.17 KB, application/octet-stream)
2011-08-07 15:20 EDT, sagar CLA
no flags Details
MyVisitor extends ASTVisitor class to parse methods, annotations (3.49 KB, application/octet-stream)
2011-08-07 15:21 EDT, sagar CLA
no flags Details
Input2.java is the file to be parsed . (675 bytes, application/octet-stream)
2011-08-07 15:22 EDT, sagar CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description sagar CLA 2011-08-07 15:16:23 EDT
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.
Comment 1 sagar CLA 2011-08-07 15:20:00 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
Comment 2 sagar CLA 2011-08-07 15:21:02 EDT
Created attachment 201051 [details]
MyVisitor extends ASTVisitor class to parse methods, annotations
Comment 3 sagar CLA 2011-08-07 15:22:32 EDT
Created attachment 201052 [details]
Input2.java is the file to be parsed .
Comment 4 sagar CLA 2011-08-07 15:29:47 EDT
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
Comment 5 Stephan Herrmann CLA 2011-08-07 17:50:09 EDT
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.
Comment 6 Olivier Thomann CLA 2011-08-15 13:18:43 EDT
It is possible to use the ASTRewrite outside of a headless Eclipse, but the buffer manager cannot be used in that context.
Comment 7 Olivier Thomann CLA 2011-09-13 11:47:34 EDT
Verified for 3.8M2.