Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » From java.lang.reflect.Method to org.eclipse.jdt.core.IMethod(In a dynamic environment I have a java.lang.reflect.Method, I want to get the equivalent org.eclipse.jdt.core.IMethod in the Eclipse environment)
From java.lang.reflect.Method to org.eclipse.jdt.core.IMethod [message #895323] Thu, 12 July 2012 08:57 Go to next message
Eclipse UserFriend
Hello,

I'm developing Counterclockwise, an Eclipse plugin for the Clojure lang.

I'm implementing code completion, especially related to displaying potential instance methods.

I'm doing this dynamically, e.g. there is a started JVM somewhere, to which my plugin is connected somehow (does not matter), and from within this JVM, I manage to get a bunch of java.lang.reflect.Method instances.

My problem is: how to serialized them so that, back inside my Eclipse plugin, I can match each of them to org.eclipse.jdt.core.IMethods.

I already have the context of the current IJavaProject.

What I'm missing to find (I hope it already exists) is a way to find an IMethod if I can extract information from an java.lang.reflect.Method.

Would you please provide some guidance to me ?

Thanks,

--
Laurent Petit
Re: From java.lang.reflect.Method to org.eclipse.jdt.core.IMethod [message #895639 is a reply to message #895323] Fri, 13 July 2012 19:10 Go to previous messageGo to next message
Eclipse UserFriend
I don't know about a one stop solution, but if you extract getDeclaringClass() and getParameterTypes() from the java.lang.reflect.Method, you should be able to navigate from the IJavaProject (using findType(String)) to the IType and from there (getMethod(String,String[])) to the IMethod.
Alternatively, by searching getMethods() you are sure to look at existing methods, only (getMethod() may return a handle to an inexistent method).

HTH,
Stephan
Re: From java.lang.reflect.Method to org.eclipse.jdt.core.IMethod [message #895640 is a reply to message #895639] Fri, 13 July 2012 19:18 Go to previous messageGo to next message
Eclipse UserFriend
Thanks for the tip.

My concern is : how to determine the argument String[] of the getMethod(String, String[]) method of IMethod, from the output of Method.getParameterTypes().

Said otherwise, when I have a Class instance representing a formal type, as obtained from java.lang.reflect.Method.getParameterTypes(), how do I transform it into a String with the right format as expected by IMethod.getMethod() ?

Thanks in advance,

Laurent
Re: From java.lang.reflect.Method to org.eclipse.jdt.core.IMethod [message #896007 is a reply to message #895640] Mon, 16 July 2012 16:57 Go to previous message
Eclipse UserFriend
Laurent Petit wrote on Sat, 14 July 2012 01:18
My concern is : how to determine the argument String[] of the getMethod(String, String[]) method of IMethod, from the output of Method.getParameterTypes().


Yea, the different encodings of types and signatures can be confusing.

Quote:
Said otherwise, when I have a Class instance representing a formal type, as obtained from java.lang.reflect.Method.getParameterTypes(), how do I transform it into a String with the right format as expected by IMethod.getMethod() ?


Actually, the Javadoc of IType.getMethod(String,String[]) contains exactly the link that I was going to suggest:
	 * The type signatures may be either unresolved (for source types)
	 * or resolved (for binary types), and either basic (for basic types)
	 * or rich (for parameterized types). See {@link Signature} for details.

where Signature is org.eclipse.jdt.core.Signature.

S.t. like this should get you started:
Class<?> clazz = method.getParameterTypes()[i];
String paramType = Signature.createTypeSignature(clazz.getName(), true);

You may need to replace all '.' by '/' or vice versa, haven't tried it right now.

Also watch out for arrays, primitive types, but the documentation of Signature should have all you need.

HTH,
Stephan

Previous Topic:Copy Paste not working in Eclipse Juno
Next Topic:Should I raise a ticket for this StatefulCommandExecutor error in bugzilla?
Goto Forum:
  


Current Time: Tue Jul 08 09:02:34 EDT 2025

Powered by FUDForum. Page generated in 0.07672 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top