Bug 77645 - [1.5][dom] navigate from local var binding to declaring method binding
Summary: [1.5][dom] navigate from local var binding to declaring method binding
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: 3.1 M4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 67460 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-11-03 10:21 EST by Tobias Widmer CLA
Modified: 2006-01-12 11:48 EST (History)
4 users (show)

See Also:


Attachments
Apply on HEAD (1.72 KB, patch)
2004-12-14 10:43 EST, 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 Tobias Widmer CLA 2004-11-03 10:21:45 EST
Given only a variable binding, it is currently cumbersome to tell whether the 
variable binding resolves to a method argument declared as a vararg. As far as 
I understand, this can only be answered using an AST. Bindings do not seem to 
provide this information.
Comment 1 Olivier Thomann CLA 2004-11-03 10:37:40 EST
The method binding should provide this information.
Comment 2 Tobias Widmer CLA 2004-11-03 10:41:01 EST
Sure, but how do I tell based only on a variable binding whether it resolves 
to a varargs argument?
Comment 3 Olivier Thomann CLA 2004-11-03 11:54:37 EST
I don't see how this is relevant without the context of a method. The var args
information is relevant only to invoke the method. So the question should be
asked to the method binding, not the variable binding.
Comment 4 Tobias Widmer CLA 2004-11-03 12:12:22 EST
For the new move method implementation, I have to construct a method signature 
based on an array of existing IVariableBindings corresponding to existing 
method arguments. In order to correctly sort the arguments for the new method 
signature (additional arguments occur, but varargs are always last) and to 
generate the proper variable declaration nodes (VARARGS property), this 
information must be available from the binding
Comment 5 Olivier Thomann CLA 2004-11-03 16:43:23 EST
When you get the array of existing method arguments, you can get the information
taht this method has a variable argument. I really don't see the need for an API
on IVariableBinding.

Jim, any comment?
Comment 6 Tobias Widmer CLA 2004-11-08 09:40:33 EST
An option to get the declaring method from a IVariableBinding (if it resolves 
to a method argument or to a local variable) would be sufficient as well.
Comment 7 Jim des Rivieres CLA 2004-11-08 11:09:39 EST
Tobias,  There's lots of info about variable bindings that is not surfaced on 
IVariableBinding; this has never come up as a problem in the past; I'd like to 
know what is different now. If I understand your comment #4 correctly, I would 
agree with Olivier's observation that you have the MethodDeclaration or 
IMethodBinding in hand and can get order and varags info from it rather than 
the IVariableBindings. Perhaps you could give us a snippet to show why that is 
not sufficient.
Comment 8 Tobias Widmer CLA 2004-11-08 11:28:55 EST
The new implementation of MoveInstanceMethodProcessor is a good example. To 
handle both types of method targets (method argument types and instance field 
types) in an uniform way, both are represented by an IVariableBinding.
Coming from an AST to a particular variable binding is kind of a one way: 
Variable bindings for local variables and method arguments (obviously) do not 
return a java element, but neither can you easily get their declaring method 
bindings. It's true that the existing facilities are sufficient to determine 
whether a variable binding resolves to a field or a local variable/method 
argument.
One can argue that IVariableBindings do not have to deliver the information 
about varargs, but then there has to be an easy/easier way to get the 
declaring method from a variable binding, if applicable.
Comment 9 Jim des Rivieres CLA 2004-11-08 13:09:46 EST
As far as I can see, the grief stems from the decision to represent the target 
as an IVariableBinding, which provides only limited information. Your code 
clearly need more information that what the binding provides. So why use 
IVariableBinding at all in your algorithm? Why not use the VariableDeclaration 
node where the binding is introduced?

Note that there has never been a direct way to navigate from any IBinding to 
the AST node that gives rise to it (if it comes from source code at all). The 
assumption has always been that the client would figure out this connection by 
themselves if needed (e.g., by searching and checking IBinding keys).

Adding Dirk so that he can comment on how these kinds of problems have been 
dealt with in the past.
Comment 10 Dirk Baeumer CLA 2004-11-09 09:57:53 EST
Looking at it there are two distinct problems:

- having IVariableBinding.isVarArgs(). This is indeed questionable to add. If we
  do this then we have to add other methods like this as well. Currently non
  of the bindings answer context related informations.

- the ability to navigate from a binding to its enclosing element. This is 
  possible for all elements except variable bindings for locals. So to make
  this consistent there should be a method #getDeclaringMethod on IVariableBinding
  which returns the IMethodBinding for locals and null for fields.
Comment 11 Jim des Rivieres CLA 2004-11-09 11:06:51 EST
The navigation from local variable binding to declaring method binding seems 
reasonable. 

Olivier, does this look implementable?

	/**
	 * Returns the method binding representing the method containing the 
scope
	 * in which this local variable is declared.
	 * <p>
	 * The declaring method of a method formal parameter is the method 
itself.
	 * For a local variable declared somewhere within the body of a method,
	 * the declaring method is the enclosing method. When local or 
anonymous
	 * classes are involved, the declaring method is the innermost such 
method.
	 * There is no declaring method for a field, or for a local variable
	 * declared in a static or instance initializer; this method returns
	 * <code>null</code> in those cases.
	 * </p>
	 * 
	 * @return the binding of the method or constructor that declares this
	 * local variable, or <code>null</code> if none
	 * @since 3.1
	 */
	public IMethodBinding getDeclaringMethod();
Comment 12 Olivier Thomann CLA 2004-11-19 12:22:50 EST
This is not trivial since we don't have that link in the compiler world.
Comment 13 Olivier Thomann CLA 2004-11-19 15:00:35 EST
Fixed and released in HEAD.
Regression tests added in ASTConverterTestAST3_2.test0578 and
ASTConverter15Test.test0072.

Let me know asap if this is what you wanted.
Comment 14 Jerome Lanneluc CLA 2004-12-14 10:33:45 EST
Test ASTConverter15Test.test0072 is using project "Converter" instead of project
"Converter15". Thus it is testing a compilation unit that doesn't exist.
Reopening to fix the test in 3.1 M5.
Comment 15 Olivier Thomann CLA 2004-12-14 10:43:23 EST
Created attachment 16581 [details]
Apply on HEAD

Patch ready to be applied on HEAD.
Comment 16 Olivier Thomann CLA 2004-12-14 13:43:31 EST
Fixing boggus test.
Comment 17 Frederic Fusier CLA 2004-12-15 10:02:04 EST
Verified for 3.1 M4 using build I200412142000 + ASTView 1.0.2.
Comment 18 Jerome Lanneluc CLA 2006-01-12 11:48:12 EST
*** Bug 67460 has been marked as a duplicate of this bug. ***