Bug 11529 - ast: missing (?) binding on simpleName in VariableDeclaration
Summary: ast: missing (?) binding on simpleName in VariableDeclaration
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.1 M1   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-03-18 08:28 EST by Adam Kiezun CLA
Modified: 2002-09-19 11:09 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Adam Kiezun CLA 2002-03-18 08:28:19 EST
2 things - not sure if they're bugs 

<source>
package p;
class A {
	private void a(){
		int i= 0;
		int y= i+1;
	}
} 
</source>

calling resolveBinding on SimpleName 'i' (from VariableDeclaration) returns null
calling resolveBinding on the VariableDeclaration returns (correctly) 
VariableBinding

is it ok that i have to go up 1 level here to get any useful information?

one more thing: getKey() returns null - the only way i can access any useful 
info on the binging is getVariableId()
is that the expected bahavior?
Comment 1 Olivier Thomann CLA 2002-03-18 10:06:40 EST
Ask Dirk. He explicitly said that he doesn't need to resolve bindings on name if they are part of a 
declaration. I can add this, but I'd like a consistent view from you guys about what you want.
Comment 2 Olivier Thomann CLA 2002-03-18 10:17:14 EST
The geyKey() method returns null for all local variables. Look at the specification of getKey() 
on IBinding.
If you want the first problem to be solved, let me know, but I won't remove it once 
done.
Comment 3 Adam Kiezun CLA 2002-03-18 10:31:45 EST
Olivier,
Dirk did not consult that with me (before i dig into details i cannot know what 
exactly i will need and i started that new-ast conversion just recently)

i will first try without it and see how much code i have to add to work around 
it

the getKey() returning null - i did have a look at the javaDoc on IBinding 
before (just wanted to confirm the bad news).
i guess i will have to live with this null (the spec says 'may be null' - so i 
can just assume it's always null <g>)
anyway, can i uniquely identify a temp by getVariableId() together with
MethodDeclaration::resolveBinding().getKey() ? (same question asked on the 
mailing list - sorry for duplication)
Comment 4 Olivier Thomann CLA 2002-03-18 10:42:11 EST
Maybe I misunderstood what Dirk wanted. I am not saying you are wrong. It is simple for me to add this 
resolution of name inside local declaration.
For the getVariableId() question, I guess that 
if the two locals are at the same place (same block and same positions compare to other locals), 
then this value should be equals for both.
Comment 5 Olivier Thomann CLA 2002-03-19 14:40:26 EST
I think in order to be consistent we should always be able to resolve a binding for a name even if this 
name is part of a declaration.
Comment 6 Dirk Baeumer CLA 2002-03-28 05:25:14 EST
Regarding returning null for simple names when used inside a declaration. The 
motivation for this one was that we need to distinguish if a name is a 
reference to something or if it is simple a string inside a declaration. For 
refactoring we need this information and in the old AST it was easy since there 
are two different nodes: a reference node and a char[] (which is not really a 
node). 

Returning null from resolveBinding turned out to be a bad mechanism to do this 
since a name also returns null if the binding can't be resolved.

Olivier, can you propose some mechanism to do provide that information. 

Regarding returning null from getKey. I will look into this one and will open a 
spearate PR for it.
Comment 7 Olivier Thomann CLA 2002-03-28 09:01:16 EST
Would it be enough for you to use the findDeclaringNode() on the compilation unit. If this returns 
null, it means that the resolved binding is created in a declaration.
You could test the class of 
the parent as well.
So let me know if you want me to add the name resolution for name inside 
declaration or if I leave it as is.
Comment 8 Dirk Baeumer CLA 2002-04-02 04:41:46 EST
I would like to see a more explicit support to distinguish names that reference 
a Java element from names used inside a declaration. The old AST used char[] 
for declarations and Reference (and subclasses) for references. My proposals 
are:

- we introduce a ReferenceNode to make that difference clear, or
- we add a method isDeclaration() to SimpleName. 
Comment 9 Jim des Rivieres CLA 2002-04-02 11:50:16 EST
I'd favour a solution that did not introduce new node types. 

SimpleName.isDeclaration() would return true iff the nodes was
used in the declaration position of its parent node.
This would include
   name of TypeDeclaration
   name of MethodDeclaration
   name of VariableDeclarationFragment
   name of SingleVariableDeclaration
   name of QualifiedName in name of PackageDeclaration
   name of PackageDeclaration
How does this sound? Are there others? 
Comment 10 Olivier Thomann CLA 2002-04-02 12:14:38 EST
Will you add an API for settings this flags? I am asking this, because during the conversion I know 
that a name is used in a declaration. Once the conversion is done, it is much more painful to 
retrieve this information.
Comment 11 Dirk Baeumer CLA 2002-04-08 12:59:31 EDT
Having the additional method isDeclaration would solve the problem that we have 
to distinguish between references and declarations. If we add this API then 
Name.resolveBinding should return a valid value even if the name is used inside 
a declaration.

But please DON'T change the behaviour of Name.resolveBinding for the M5 build 
since some of out code relies on the fact that the method return null for 
declarations.
Comment 12 Olivier Thomann CLA 2002-04-08 13:11:15 EDT
Then we should defer this change post M5. API and changes in ASTConverter 
should be done post M5.
Comment 13 Olivier Thomann CLA 2002-04-11 17:45:33 EDT
The API isDeclaration() has been added for SimpleName post build 0411. You can 
ask me for an update of JDT/Core if you want to update your code checking for 
null returned by resolveBinding().
I will update the ASTConverter to return a valid binding for such simple names 
as soon as your code has been updated. So please annotate this PR when you're 
done.
Comment 14 Olivier Thomann CLA 2002-04-18 18:18:17 EDT
Can I update the AST/DOM code? I'd like to reintroduce a valid binding for names even if they are in a 
declaration.
Comment 15 Olivier Thomann CLA 2002-04-22 15:01:00 EDT
I'd like to get rid of the current implementation which returns null for names inside 
declarations. Could you please confirm that you are not relying on this anymore?
Comment 16 Dirk Baeumer CLA 2002-04-24 05:59:56 EDT
Adapted flow analyzer to new API
Comment 17 Olivier Thomann CLA 2002-04-24 10:19:10 EDT
The name inside declarations will return a valid binding when using resolveBinding() in the next 
integration build.
Comment 18 Olivier Thomann CLA 2002-04-24 11:58:42 EDT
Fixed and released in HEAD.
Comment 19 mark_dixon CLA 2002-08-30 15:23:58 EDT
I notice that SimpleName.resolveBinding() still returns null for names in 
variable declarations using the latest integration build (I200208270833).  Has 
this fix not made it's way out to us mortals yet or is there something else 
going on?

Thanks

IWorkspace ws = testcasesPlugin.getWorkspace();
IWorkspaceRoot root = ws.getRoot();
IProject project = root.getProjects()[0];
IJavaProject jp = JavaCore.create(project);

CompilationUnit cu = AST.parseCompilationUnit(
   "public class Class {void theMethod() {int local;for (int i = 0; i < 5; ++i)
{} } }".toCharArray(),
   "Class", jp);
cu.accept(new ASTVisitor() {
   public boolean visit(SimpleName node) {
      IBinding binding = node.resolveBinding();
      if (null == binding) {
         System.out.println("Null binding for SimpleName " + node.getIdentifier
());
      }
      return true;
   }

});

Output:
Null binding for SimpleName local
Null binding for SimpleName i
Comment 20 Olivier Thomann CLA 2002-09-03 10:47:10 EDT
I am investigating what could be wrong.
Comment 21 Olivier Thomann CLA 2002-09-03 11:45:35 EDT
Fixed and released in HEAD.
Comment 22 Olivier Thomann CLA 2002-09-03 14:47:27 EDT
Released in 2.1 stream only for now.
Comment 23 David Audel CLA 2002-09-19 04:32:14 EDT
Verified.
Comment 24 Olivier Thomann CLA 2002-09-19 11:09:19 EDT
Regression tests added (test0364, test0365)