Bug 36772 - AST: CompilationUnit.findDeclaringNode: Spec/Impl not same
Summary: AST: CompilationUnit.findDeclaringNode: Spec/Impl not same
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.0 M1   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: api, readme
Depends on:
Blocks:
 
Reported: 2003-04-22 15:14 EDT by Martin Aeschlimann CLA
Modified: 2003-06-06 06:17 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Aeschlimann CLA 2003-04-22 15:14:09 EDT
2.1

The Javadoc of CompilationUnit.findDeclaringNode says
	 * The following table indicates the expected node type for the various
	 * different kinds of bindings:
...
	 *    <code>ClassInstanceCreation</code> (for anonymous classes) </li>

The implementation is different:
When called with a anonyomous type binding, a 'AnonymousClassDeclaration' is
returned.
Comment 1 Jim des Rivieres CLA 2003-04-22 16:11:29 EDT
Since ClassInstanceCreation and AnonymousClassDeclaration are in one-to-one 
correspondence and bi-directionally linked, it could be spec'd either way.
I recommend bringing the implementations of both 
CompilationUnit.findDeclaringNode methods into line with the existing specs.
Comment 2 Olivier Thomann CLA 2003-04-22 16:13:35 EDT
It looks awkward to me that the expected node is a class instance creation in
case of an anonymous class. The type binding used to get this node is retrieved
using resolveTypeBinding() on the AnonymousTypeDeclaration node. So it would
make sense to me that the reverse operation (starting from the binding to the
declaring node) returns the AnonymousTypeDeclaration. Then getting the parent is
good enough to get the corresponding class instance creation node.
Any comment?
Comment 3 Martin Aeschlimann CLA 2003-04-22 16:33:37 EDT
I agree with Olivier. The node that contains the .resolveTypeBinding should be
returned.
Changing the spec instead of the implementation also has the advantage of not
breaking existing code.
Comment 4 Jim des Rivieres CLA 2003-04-22 16:56:31 EDT
I could go either way, but not for a bogus argument.

The only client code it doesn't break is existing code replying on the buggy 
behavior rather than what's spec'd. We need to be more careful at detecting 
and fixing these mistakes before we ship them. Flame off ;-)

If we do change the spec, we would need to document it in the compatibility 
section of the release notes because it is a breaking API change. On the other 
hand, if we fix the bug in the implementation, and JDT UI fixes their bug, and 
we're done.

Comment 5 Dirk Baeumer CLA 2003-04-23 02:52:03 EDT
I opt to change the spec since I would expect to get the type declaration node 
not the instance creation node when call findDeclaration(...). This makes it 
consistent with the rest of the return values of findDeclaringNode.
Comment 6 Olivier Thomann CLA 2003-04-23 08:38:49 EDT
I agree that we should have found this one prior to the release. The point is
that the spec is inconsistent, because in one case the node is not the one that
was used to resolve the binding.
Changing the spec makes more sense to me even if we need to document it. For me
this is the same problem than bug 25330. The spec could be improved to clean up
the user code.
Comment 7 Jim des Rivieres CLA 2003-04-23 10:48:04 EDT
Agreed. It is more consistent if cu.findDeclaringNode(binding) returns a node 
with the property that node.resolveBinding() == binding. Changing the spec.

For the API compatibility section of the release notes:

The API specs for both CompilationUnit.findDeclaringNode methods were changed
to state the AnonymousClassDeclaration node is returned for anonymous class 
bindings, rather than the parent ClassInstanceCreation node. This makes the 
spec consistent in that cu.findDeclaringNode(binding) == node always implies 
node.resolveBinding() == binding. This brings the spec into line with what was 
implemented all along, so there is low risk of negatively impacting existing 
clients.

Comment 8 Olivier Thomann CLA 2003-04-23 11:52:54 EDT
Fixed and released in HEAD.
Regression tests added.
Comment 9 Olivier Thomann CLA 2003-04-23 11:54:20 EDT
Change milestone.
Comment 10 David Audel CLA 2003-06-06 06:17:21 EDT
Verified.