Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 151487 - Variable hover incorrect for members of the same type but different object
Summary: Variable hover incorrect for members of the same type but different object
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Debug (show other bugs)
Version: 3.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.6 M7   Edit
Assignee: JDT-Debug-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 199223 246142 308514 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-07-21 18:18 EDT by Dux CLA
Modified: 2010-04-14 14:39 EDT (History)
4 users (show)

See Also:


Attachments
Fix (2.94 KB, patch)
2010-04-08 14:12 EDT, Markus Keller CLA
no flags Details | Diff
Fix 2 (use SharedASTProvider) (2.12 KB, patch)
2010-04-12 12:09 EDT, Markus Keller CLA
no flags Details | Diff
Fix 3 (corrects condition) (2.16 KB, patch)
2010-04-14 14:21 EDT, Markus Keller CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dux CLA 2006-07-21 18:18:31 EDT
Variable hover displays the value of the this member when hovering over the same member in a different object of the same type.  For example:

public class Foo
{
  private int id;

  public boolean equals(Object other)
  {
    Foo that = (Foo)other;
    return this.id == that.id;
  }
}

Setting a breakpoint at the return statement and hovering over the id in "that.id" displays the value of "this.id".
Comment 1 Darin Wright CLA 2007-02-08 10:59:22 EST
Currently, the debug hover is only resolving the identifier under the cursor (in this case 'id'). If finds a field and resolves it in the context of "this", rather than checking for a qualifier.
Comment 2 Darin Wright CLA 2007-04-27 16:45:57 EDT
Marking as assigned for future consideration. Not planned for 3.3.
Comment 3 Darin Wright CLA 2007-08-08 09:38:53 EDT
*** Bug 199223 has been marked as a duplicate of this bug. ***
Comment 4 Darin Wright CLA 2009-06-16 15:03:51 EDT
*** Bug 246142 has been marked as a duplicate of this bug. ***
Comment 5 Markus Keller CLA 2010-03-30 08:21:48 EDT
This blooper cost me quite some time. I was really perplexed until I found out that it's not me who doesn't understand what's going on, but the Variable Value hover.

JavaDebugHover#resolveLocalVariable(IJavaStackFrame, ITextViewer, IRegion) should at least scan backwards a bit and return null if the tokens in front of the given region are "<identifier><whitespace>?'.'<whitespace>?" where <identifier> is not "this".

Even better would be to use an AST (from SharedASTProvider) and walk up the tree to make sure the variable is really from the current context. Or even resolve the parents as well, such that the hover can show the right value for simple chains such as "that.id".
Comment 6 Darin Wright CLA 2010-04-08 12:37:17 EDT
*** Bug 308514 has been marked as a duplicate of this bug. ***
Comment 7 Markus Keller CLA 2010-04-08 14:12:18 EDT
Created attachment 164263 [details]
Fix

Here's a straightforward fix that creates its own minimal AST and ensures that the variable hover is only shown for field accesses on 'this'.

In the example from comment 0, the wrong variable hover just doesn't show up any more for "id" in "that.id" (but the user can still hover over "that" to the see "id" as a child in the hover).
Comment 8 Darin Wright CLA 2010-04-08 15:41:47 EDT
Applied. Fixed. Thanks, Markus.
Comment 9 Darin Wright CLA 2010-04-08 15:42:15 EDT
Verified.
Comment 10 Markus Keller CLA 2010-04-12 12:09:48 EDT
Created attachment 164578 [details]
Fix 2 (use SharedASTProvider)

If the SharedASTProvider already has an AST for the type root, we could save some work. The patch does that, but still creates its own minimal AST in other cases (since that's still faster than waiting for the full AST).
Comment 11 Darin Wright CLA 2010-04-12 12:20:27 EDT
re-open for improvement.
Comment 12 Darin Wright CLA 2010-04-12 16:49:04 EDT
Applied.
Comment 13 Markus Keller CLA 2010-04-14 14:18:40 EDT
Sorry, I messed up. The check is far too restrictive, since it prevents the hover in all cases where the field is accessed without qualifier.

Here's a test case:

package xy;
public class Foo {
	protected int id;

	public static void main(String[] args) {
		final Foo a = new Foo();
		a.id = 42;
		final Sub b = new Sub();
		b.id = 0;
		System.out.println(a.equals(b));
		System.out.println(a.id);
		System.out.println(new Foo().id);
	}
	
	public Foo() {
		id= 17;
		System.out.println(id);
	}

	public boolean equals(Object other) {
		final Foo that = (Foo) other;
		return this.id == that.id;
	}
}

class Sub extends Foo {
	public Sub() {
		System.out.println(super.id);
	}
}
Comment 14 Markus Keller CLA 2010-04-14 14:21:35 EDT
Created attachment 164865 [details]
Fix 3 (corrects condition)

Luckily, there are only two constructs in the AST where a field name can refer to a field in another instance: FieldAccess and QualifiedName.

This patch only prevents the "remote field access" cases but allows all the others.
Comment 15 Darin Wright CLA 2010-04-14 14:39:58 EDT
Good catch. Applied.