Bug 210310 - IJavaElementDelta contains wrong data after APT processor is enabled
Summary: IJavaElementDelta contains wrong data after APT processor is enabled
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P2 critical with 1 vote (vote)
Target Milestone: 3.4 M4   Edit
Assignee: Jerome Lanneluc CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-19 17:45 EST by Leho Nigul CLA
Modified: 2007-12-12 09:41 EST (History)
5 users (show)

See Also:


Attachments
Proposed fix and regression test (2.48 KB, patch)
2007-11-22 06:24 EST, Jerome Lanneluc CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Leho Nigul CLA 2007-11-19 17:45:04 EST
Build ID: I20071101-2000

Steps To Reproduce:
1.Create java Class as follows

public class Test {

	private void test()
	{
		
	}
}

2. Add APT processor to the Java project (I used APTDemo processor from here --> http://www.eclipse.org/jdt/apt/index.html)

3. Change "private" to "public" in method declaration above

On this change ElementChangedEvent is fired. When I get delta by calling
IJavaElementDelta delta = event.getDelta();

I expect the array of affected children in this delta to be non-empty, however IT IS empty.

If I remove annotation processor from the Java project, behavior is back to expected.

This happens to ALL the deltas for all the changes that I tried. This is a BIG problem, since as far as I can see, when the APT processor is enabled on Java project, the delta is pretty much looses it's meaning. 



More information:
Comment 1 Walter Harley CLA 2007-11-19 18:21:12 EST
Can you attach the code that you're using to register the element listener and read the delta?
Comment 2 Leho Nigul CLA 2007-11-20 10:40:18 EST
Hi Walter

I am using EXTREMELY simple code to detect it

public class Test implements IObjectActionDelegate, IElementChangedListener {

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub

	}

	public void run(IAction action) {
		
		JavaCore.addElementChangedListener(this);
		

	}

	public void selectionChanged(IAction action, ISelection selection) {
		

	}

	public void elementChanged(ElementChangedEvent event) {
		IJavaElementDelta delta = event.getDelta();
		IJavaElementDelta[] affectedChildren = delta.getAffectedChildren();
		
	}

}

Just invoke it in my selfhost environment
Comment 3 Walter Harley CLA 2007-11-20 11:55:24 EST
I'll check it out.
Comment 4 Walter Harley CLA 2007-11-20 21:13:02 EST
I am able to reproduce this in HEAD and in the 3.3.x maintenance branch.  It does not occur in Eclipse 3.2.2.  Still trying to narrow in exactly when it got broken.

The problem seems to only affect deltas sent during reconcile; the delta sent on a save/build is not affected.  It occurs even though the file being edited contains no annotations.  It occurs even when running under a 1.5 JRE, in which case none of the JSR269 plugins are loaded.

Comment 5 Andrew Mak CLA 2007-11-21 16:22:32 EST
This is probably already known, but just to get it on record, I am running into this problem also with IJavaElementDelta.getAnnotationDeltas() returning empty.
Comment 6 Walter Harley CLA 2007-11-21 17:57:46 EST
If one of the commenters has the time and capability to help with narrowing down exactly which release this got broken in, that will help with troubleshooting.  It works in 3.2.2, but not in the current tip of the 3.3.x maintenance branch; I haven't yet determined whether it works in 3.3.0 or 3.3.1.
Comment 7 Walter Harley CLA 2007-11-21 19:49:57 EST
I can also reproduce the bug in the 3.3.1 release.  

If processing is enabled and there are processors on the factory path, but all the processors are disabled, the bug does not occur.  If a processor is enabled, the bug occurs, even though there are no annotations being processed.

Adding Olivier and Jerome for early warning, since the fix to this may involve JDT Core changes.
Comment 8 Walter Harley CLA 2007-11-21 20:05:14 EST
Behavior in Eclipse 3.3.0 is the same as 3.3.1 and later.
Comment 9 Walter Harley CLA 2007-11-21 20:40:02 EST
The problem seems to be the call to resetAST(), in org.eclipse.jdt.apt.core.internal.env.ReconcileEnv.close().  Commenting out that line, while it will cause problems with type generation during reconcile, solves this problem.

As a temporary workaround it is possible to turn off reconcile-time APT processing.  This option is settable in the "Annotation Processing" preferences; it's called "enable processing in editor".

I'll have to investigate further to figure out what the right "real" fix is.
Comment 10 Walter Harley CLA 2007-11-21 21:18:19 EST
What ReconcileContext.resetAST() does is set this.operation.ast = null.  I am guessing this also causes the Java element delta to be discarded, and then recomputed; but when it is recomputed, the difference is not a difference any more.  That is, the file goes from A to B, causing it to be reconciled and causing the A->B delta; then resetAST() is called, deleting the old delta and creating an empty delta from B->B.

The resetAST() method is called when a compilationParticipant has changed the compilation context, e.g. by generating a new type.  For instance, the type being reconciled might have had a reference to a missing type, but performing APT could cause that type to be created, so we want to recompile to see if the missing type reference is fixed.  But the file being reconciled is not, itself, allowed to be changed.  It is always some other type that is changed or created.  So we really don't want to throw out the original delta.

Jerome, do you have any thoughts on whether there is another approach to signaling the need to recompile, that would not cause the Java element delta to be discarded?
Comment 11 Jerome Lanneluc CLA 2007-11-22 03:49:45 EST
Thanks for investigating Walter. This is indeed a problem in ReconcileWorkingCopyOperation that forgets the delta when the ast is reset.
Taking over as I believe the fix must be done in JDT/Core (I hope you don't mind :-) )
Comment 12 Jerome Lanneluc CLA 2007-11-22 06:24:09 EST
Created attachment 83515 [details]
Proposed fix and regression test
Comment 13 Jerome Lanneluc CLA 2007-11-22 06:25:14 EST
Fix and test released for 3.4M4
Comment 14 Andrew Mak CLA 2007-11-22 09:29:11 EST
Wow, thanks for the quick turnaround guys!  My problem is solved :)
Comment 15 Walter Harley CLA 2007-11-23 01:29:07 EST
(In reply to comment #11)
> I hope you don't mind :-)

Thanks, Jerome.  Much appreciated.  Happy Thanksgiving, from the USA :-)
Comment 16 Maxime Daniel CLA 2007-12-12 09:41:12 EST
Verified for 3.4 M4 using build I20071211-0010.