Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 389787

Summary: Expression#resolveTypeBinding returns wrong type for SimpleName inside QualifiedName
Product: [Eclipse Project] JDT Reporter: Andreas Thies <andreas.thies>
Component: CoreAssignee: Jesper Moller <jesper>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: manoj.palat, srikanth_sankaran, stephan.herrmann
Version: 4.2Flags: jesper: documentation-
stephan.herrmann: review-
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard: stalebug
Attachments:
Description Flags
Patch for JavaDoc
none
New patch suggestion none

Description Andreas Thies CLA 2012-09-18 03:19:48 EDT
Steps to reproduce:

1) Generate AST (with bindings) for

package a;

public class A {
  int i;
  B b = new B();
  int x = b.i;
}
class B extends A {
  public int x;
}

2) Find the SimpleName 'b' inside the QualifiedName 'b.i'

3) Call simpleName.resolveTypeBinding() on the SimpleName 'b'

4) This returns the type Binding 'public class a.A ...'

Since b is declared with type B, I would expect the type Binding for the SimpleName 'b' to be 'class B ...'.

For the case that this behavior is intended (e.g., because the type A would be sufficient as a receiver for the reference to i in b.i), the JavaDoc of Expression#resolveTypeBinding should clarify this.
Comment 1 Srikanth Sankaran CLA 2013-02-27 01:58:10 EST
Jesper, please take a look, TIA.
Comment 2 Jesper Moller CLA 2013-03-05 09:04:15 EST
How about an explanation such as this:

Note that 'resolveTypeBinding' returns a binding based on how the construct is is resolved, in context. For instance, if the expression 'obj.field' is a field reference to 'field' inside the class A, the type binding returned for 'obj' would return a binding for 'A', even if the declared type of obj was a subclass of A. In this case, fieldAccess.getExpression().resolveTypeBinding() == fieldAccess.resolveFieldBinding().getDeclaringClass().
Comment 3 Jesper Moller CLA 2013-03-13 20:09:40 EDT
Created attachment 228391 [details]
Patch for JavaDoc

Explanation about how the of binding context influences type binding results.
Comment 4 Srikanth Sankaran CLA 2013-03-14 03:41:09 EDT
Stephan, can you look over the changes ? TIA.
Comment 5 Stephan Herrmann CLA 2013-03-21 09:46:05 EDT
I have two comments:

(1) "even if <code>obj</code> was declared as a subclass of <code>A</code>."
obj cannot *be* (declared as) a subclass of A. 
The original proposal was more accurate:
    "even if the declared type of obj was a subclass of A"
Alternatively we could say "obj resolves to a variable/value(?) of a type B,
where B is a subclass of A".

(2) " ... is resolved based on the context .... For example "
This leaves me puzzling what other examples exist where context is relevant.
From the text I'd guess that all "receiver expressions", left of the dot
in a field access or method invocation are affected, and that the effect
is always to use the declaringClass of the feature right of the dot.

Is this guess correct? The javadoc should leave no doubt about this.

What kind of expressions 'obj' are affected? Only names or arbitrary
expressions?
Comment 6 Stephan Herrmann CLA 2013-07-09 06:31:17 EDT
ping :)
Comment 7 Stephan Herrmann CLA 2013-08-29 11:04:45 EDT
ping' :)
Comment 8 Jesper Moller CLA 2013-10-17 10:07:26 EDT
Created attachment 236601 [details]
New patch suggestion

Wow, those ping show horrible lagging...

This wording is closer to the original comment, is it better that way?
Comment 9 Stephan Herrmann CLA 2015-09-15 14:55:24 EDT
Talk about lagging, OMG!

Yes, the proposed javadoc looks good per se.

I'm only surprised about the facts, which we are documenting here: What you write seems to apply to field access only, replace b.i with b.getI() (with the obvious decl. of getI() in A), and b will have type B. Is this inconsistency intended?

Manoj, have you seen such behavior? This seems to be implemented in DefaultBindingResolver:1081 ff, see:

// in this case we want to get the next field declaring's class

A little down that code we do use
   ((FieldBinding) qualifiedNameReference.binding).type
which assumably would let us answer B instead of A, but this is done under the premise "no capture needed for array type". Maybe we should just use the qnr.binding.type and perform capture manually?

(This region has not been touched since 2005).

I don't know if clients might by now rely on the current semantics, or if we should consider this as a bug and fix the behavior, not the javadoc?
Comment 10 Stephan Herrmann CLA 2016-05-09 16:45:01 EDT
Closing review as -1 based on comment 9.
Comment 11 Eclipse Genie CLA 2019-02-02 19:09:44 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.