Bug 216430 - ICompilationUnit.getBuffer().getContent() returns incosistent contents
Summary: ICompilationUnit.getBuffer().getContent() returns incosistent contents
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3.1   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.4 M5   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-01-24 08:37 EST by Georgi Vachkov CLA
Modified: 2008-02-05 07:44 EST (History)
2 users (show)

See Also:


Attachments
sample plugin project that listens to java element changes (8.75 KB, application/x-zip-compressed)
2008-01-24 08:37 EST, Georgi Vachkov CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Georgi Vachkov CLA 2008-01-24 08:37:40 EST
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:
Comment 1 Jerome Lanneluc CLA 2008-02-01 11:20:45 EST
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.
Comment 2 Maxime Daniel CLA 2008-02-05 02:52:15 EST
I could not reproduce the bug yet. Will talk to Jérôme.
Comment 3 Maxime Daniel CLA 2008-02-05 07:44:24 EST
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.