Bug 140318 - AST: Invalid annotation binding for incomplete code
Summary: AST: Invalid annotation binding for incomplete code
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.2 RC4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-05-05 05:20 EDT by Martin Aeschlimann CLA
Modified: 2006-05-11 22:04 EDT (History)
6 users (show)

See Also:


Attachments
Proposed fix (1.07 KB, patch)
2006-05-05 14:42 EDT, Olivier Thomann CLA
no flags Details | Diff
Regression test (4.20 KB, patch)
2006-05-05 14:43 EDT, Olivier Thomann CLA
no flags Details | Diff
Better patch (1.67 KB, patch)
2006-05-09 10:24 EDT, Olivier Thomann CLA
no flags Details | Diff
Last patch (1.71 KB, patch)
2006-05-09 21:02 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Aeschlimann CLA 2006-05-05 05:20:45 EDT
RC2

In the following code, @Test is not imported, but the SimpleName 'Test' has still a binding.
binding.getName() throws an NPE, see below. See also bug 139889.

By design, bindings should be either complete or not there.

----

package p1;

import java.util.List;

public class C {
	/**
	 * @category fo
	 */
	@Test private int fXoo;
}

--
java.lang.NullPointerException
	at org.eclipse.jdt.core.dom.AnnotationBinding.getName(AnnotationBinding.java:104)
	at org.eclipse.jdt.astview.views.Binding.getChildren(Binding.java:78)
	at org.eclipse.jdt.astview.views.ASTViewContentProvider.getChildren(ASTViewContentProvider.java:95)
	at org.eclipse.jdt.astview.views.ASTViewContentProvider.hasChildren(ASTViewContentProvider.java:245)
	at org.eclipse.jface.viewers.AbstractTreeViewer.isExpandable(AbstractTreeViewer.java:1860)
	at org.eclipse.jface.viewers.TreeViewer.isExpandable(TreeViewer.java:830)
	at org.eclipse.jface.viewers.AbstractTreeViewer.isExpandable(AbstractTreeViewer.java:1883)
	at org.eclipse.jface.viewers.AbstractTreeViewer.updatePlus(AbstractTreeViewer.java:2454)
	at org.eclipse.jface.viewers.AbstractTreeViewer.createTreeItem(AbstractTreeViewer.java:739)
Comment 1 Olivier Thomann CLA 2006-05-05 11:25:14 EDT
Reproduced.
The problem comes from the resolution of the annotation.
We have this code in:
TypeBinding typeBinding = this.type.resolveType(scope);
if (typeBinding == null) {
	if (initializeCompilerAnnotation)
		this.compilerAnnotation = new AnnotationBinding(this);
	return null;
}
So even if the type binding is null, we still have a annotation binding. We can either fix this in the resolution of the annotation to return null or we fix the binding resolver in the ast world.
Comment 2 Olivier Thomann CLA 2006-05-05 11:45:16 EDT
What do you expect in the case where Test is a class for example ?
Do you expect null?
I would say so.
Comment 3 Olivier Thomann CLA 2006-05-05 12:04:59 EDT
If Test is a class, I would expect:
@Test .resolveAnnotationBinding() => null
but Test.resolveBinding() or resolveTypeBinding() => Type Test.

Do you agree with this?
Comment 4 Olivier Thomann CLA 2006-05-05 13:23:12 EDT
Theodora,

Do you have any idea why an annotation binding is set for an annotation even if the type cannot be resolved or is not an annotation?
I would expect that nothing is set in the compilerAnnotation field in these cases.
Comment 5 Jess Garms CLA 2006-05-05 13:27:22 EDT
CC'ing Tim Hanson on this one for some insight.
Comment 6 Tim Hanson CLA 2006-05-05 13:46:54 EDT
(In reply to comment #4)
> Do you have any idea why an annotation binding is set for an annotation even if
> the type cannot be resolved or is not an annotation?

I think this is a mistake. I believe Theodora was emulating the behaviour of our compiler which would create partial bindings.

> I would expect that nothing is set in the compilerAnnotation field in these
> cases.

I agree.

Comment 7 Olivier Thomann CLA 2006-05-05 14:35:08 EDT
So I will propose a fix by changing the resolution of the compiler annotation. There is no need to make any change on the DOM side.
Comment 8 Olivier Thomann CLA 2006-05-05 14:42:43 EDT
Created attachment 40509 [details]
Proposed fix
Comment 9 Olivier Thomann CLA 2006-05-05 14:43:03 EDT
Created attachment 40510 [details]
Regression test
Comment 10 Olivier Thomann CLA 2006-05-05 14:43:32 EDT
This patch prevents incomplete bindings from being returned when resolveAnnotationBinding() is called.
Comment 11 Olivier Thomann CLA 2006-05-05 14:50:21 EDT
Tim,

Could you please test that patch in APT to be sure that you don't have any assumption made on the fact that compilerAnnotation is not null?
Thanks.
All existing DOM tests are passing as well as the two regression tests in the patch.
Comment 12 Tim Hanson CLA 2006-05-05 15:21:00 EDT
All APT tests pass.
Comment 13 Olivier Thomann CLA 2006-05-05 15:36:39 EDT
Philippe,

Do you want it as a candidate for RC4?
Comment 14 Philipe Mulet CLA 2006-05-09 10:14:34 EDT
+1 for 3.2RC4.

Martin & Tim/Jess: please cast your votes.
Comment 15 Olivier Thomann CLA 2006-05-09 10:24:50 EDT
Created attachment 40727 [details]
Better patch

Tim,

This patch is not setting a value in the compilerAnnotation field in case the binding cannot be resolved or the type binding is not an annotation.
Could you please check that the APT tests are still ok with it? Thanks.
Comment 16 Martin Aeschlimann CLA 2006-05-09 10:49:07 EDT
Olivier, the last added line in the change, is it necessary? Don't you want to just eleminate the line
   if (initializeCompilerAnnotation)?
Comment 17 Olivier Thomann CLA 2006-05-09 10:51:43 EDT
In case the type cannot be resolved or the type is not an annotation, there is nothing to do even if initializeCompilerAnnotation is set to true.
Comment 18 Olivier Thomann CLA 2006-05-09 10:53:50 EDT
I simply moved the code that initialize the initializeCompilerAnnotation temp variable down to where it is used.
Right now it is set to true, but I wanted to preserve the comment behind.
Unclear why this is always set to true.
Comment 19 Olivier Thomann CLA 2006-05-09 11:52:24 EDT
Tim,

Would you know why the initializeCompilerAnnotation is set to true? It looks better to use:
boolean initializeCompilerAnnotation = scope.compilerOptions().storeAnnotations;,
doesn't it?
Comment 20 Tim Hanson CLA 2006-05-09 12:14:22 EDT
All APT test pass.

+1 for RC4.

I don't know why the options aren't being consulted for this. I thought it was there because we didn't want to waste the space for annotations if they weren't needed.
Comment 21 Olivier Thomann CLA 2006-05-09 21:02:24 EDT
Created attachment 40896 [details]
Last patch

I removed the if (true) to use the option instead.
All APT, JDT/UI and JDT/Core tests passed. For JDT/UI, I got some failures, but I got the same failures without the patch. So the patch doesn't seem to be the cause of the failures.
Comment 22 Martin Aeschlimann CLA 2006-05-10 04:09:19 EDT
As in RC3 we had 'boolean initializeCompilerAnnotation = true;'
I would rather be conservative and not change that.
So I would rather prefer the previous patch from comment 15.
Comment 23 Olivier Thomann CLA 2006-05-10 09:51:47 EDT
I disagree.
We don't want users to rely on bindings that should not be there. The option should have been used and it was not the case.
Philippe, Tim, any thoughts?
Comment 24 Philipe Mulet CLA 2006-05-10 10:10:05 EDT
The theory is that annotation bindings are an optional feature since they slow down the compiler. Why did they get added inconditionnally during RC3 ? Was this a mistake or intended ?

I would also favour making the code conditional again... but for RC4, I could live with inconditional mode (still fix it for 3.2.1 or 3.3).
Comment 25 Olivier Thomann CLA 2006-05-10 10:22:18 EDT
Fixed and released in HEAD.
I released the version that does the unconditional initialization of the annotation binding to minimise the impact of the change.
I opened bug 141030 to track the second issue with the usage of the compiler option.
Regression tests added in org.eclipse.jdt.core.tests.dom.ASTConverter15Test.test0218/0219
Comment 26 Martin Aeschlimann CLA 2006-05-10 10:28:32 EDT
+1 for 3.2 RC4
Comment 27 Olivier Thomann CLA 2006-05-11 22:04:54 EDT
Verified with I20060511-2000 for 3.2RC4