Bug 78757

Summary: MethodBinding.getJavaElement() returns null
Product: [Eclipse Project] JDT Reporter: Juan omínguez <jdomigon>
Component: CoreAssignee: Jerome Lanneluc <jerome_lanneluc>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: markus.kell.r
Version: 3.1   
Target Milestone: 3.1 M4   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Modified ASTView showing "Caller" Class IMethodBindings none

Description Juan omínguez CLA 2004-11-16 12:57:33 EST
Just two example classes:

public class Callee 
{
	public void methodInCallee()
	{
		return;
	}
}

public class Caller 
{
	
	public void methodInCaller()
	{
		return;
	}
	
public static void main(String[] args) 
{
	Caller caller = new Caller();
	Callee callee = new Callee();
	
 	// IMethodBinding.getJavaElement() returns IMethod 
	caller.methodInCaller();
	// IMethodBinding.getJavaElement() returns null
	callee.methodInCallee();
}

When I call "getJavaElement()" on a "IMethodBinding" obtained from a
MethodInvocation AST, I get the correct IMethod in the call
"caller.methodInCaller()", but null in the call "callee.methodInCalle()"

The method "Bindings.findMethod(IMethodBinding, IJavaProject)" suggests that
IMethodBinding.getJavaElement() is no reliable. If this is a bug, shoudn't that
method be removed?
Comment 1 Juan omínguez CLA 2004-11-16 13:10:59 EST
Created attachment 15900 [details]
Modified ASTView showing "Caller" Class IMethodBindings 

You can add the following method to class
"org.eclipse.jdt.astview.views.Binding" to show an error image when 
IBinding.getJavaElement()==null


public Image getImage() 
{
	if(getBinding()==null)
	{
		return new
SharedImages().getImage(ISharedImages.IMG_OBJS_WARN_TSK);
	}
	if(getBinding().getKind()==IBinding.TYPE)
	{
		ITypeBinding typeBinding = (ITypeBinding) getBinding();
		
		if(typeBinding.isPrimitive())
		{
			return new
SharedImages().getImage(ISharedImages.IMG_OBJS_INFO_TSK);
		}
	}
	if(getBinding().getJavaElement()==null)
	{
		return new
SharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
	}
	else
	{
		return new
SharedImages().getImage(ISharedImages.IMG_OBJS_INFO_TSK);
	}
}
Comment 2 Juan omínguez CLA 2004-11-16 13:19:52 EST
Reproduced on Eclipse 3.1M3 and Eclipse 3.1M3 with plugins org.eclipse.jdt.core
and org.eclipse.jdt.ui from HEAD
Comment 3 Markus Keller CLA 2004-11-18 06:57:34 EST
Simpler examples where method bindings don't know their java element (I20041117):
    new Integer(1);
    Integer.parseInt("1");

Juan: IBinding#getJavaElement() is under construction, but will eventually
replace Bindings.findMethod(IMethodBinding, IJavaProject). Note that class
Bindings is internal and findMethod(..) may be removed at any time.
Comment 4 Juan omínguez CLA 2004-11-18 07:16:21 EST
Ooops, I hadn't realized the tag "@since 3.1" in "getJavaElement()" javadoc, and
I thought it was and old and stable method (I'm not used to non-workin api in
eclipse ;-)

I've seen that "Bindings" is internal, but I'm developing a new refactoring
(extract enum from public static final constants) and I'm using lots of internal
refactoring API, anyway. I will use getJavaElement eventually (as it's much more
"elegant"), but I don't intend to make a internals-free plugin...
Comment 5 Jerome Lanneluc CLA 2004-11-22 12:37:13 EST
Changed MethodBinding#getJavaElement() to find a similar method to the binding
in the parent if there is no AST for it.

Added regression test ASTModelBridgeTets#testMethod3()
Comment 6 Juan omínguez CLA 2004-11-23 07:27:15 EST
Works for me, thanks