Community
Participate
Working Groups
Created attachment 87764 [details] sample plugin project that listens to java element changes Build ID: M20071023-1652 Steps To Reproduce: 1. Register IElementChangedListener which checks for changes in ICompilationUnit's in plugin project. 2. Start runtime workbench and perform following steps in it. 3. Create Java project and a class in package test and add a method to it like this: package test; public class Test { public void test() { } } 4. Copy this class to the root of the project with copy/paste 5. Open the original in java editor one and remove the method 6. Copy the copied in step 3 file to the test package. In element changed listener we are printing the content of the buffer and after the file has been copied we receive event for this change - the printed content is same as the content before copy/paste operation. This is bad in our case becouse we are using AST afterwards to parse the code for changes in annotations and in our case we do not have chance to get the real content. I tried to call reconcile but this doesn't help. More information:
Since the compilation unit is in working copy mode, ICompilationUnit#getBuffer() represents the content of the working copy, it is not the content on disk. At the time the listener is notified, the working copy still has the old content. This is what the listener shows here. The Java editor also listens for Java element changes and updates the content of the working copy after that. Instead of listening for POST_CHANGE events, I would recommend to listen for POST_RECONCILE events to be notified after the Java editor has updated the working copy.
I could not reproduce the bug yet. Will talk to Jérôme.
OK, the key is to follow the scenario ;-). In my former attempts, I mistakenly replaced step 4 by '4bis Copy this class to the *default package* of the project with copy/paste'. Using the real step 4, I could get the expected technical effect: the resource gets modified and the event fired before the editor notices it. I also made some tests with the approach suggested by Jérôme and got it to work pretty fast, hence: Verified for 3.4M5 using I20080204-0010 build.