Community
Participate
Working Groups
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?
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); } }
Reproduced on Eclipse 3.1M3 and Eclipse 3.1M3 with plugins org.eclipse.jdt.core and org.eclipse.jdt.ui from HEAD
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.
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...
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()
Works for me, thanks