• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reflection : invoke a method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I try to invoke with reflection a method :
public void execute( MyObject o) {
System.out.println("execute");
}
but my parameter is not of class MyObject but of a subclass of MyObject.
Class.getMethod(String name, Class[] parameterTypes) doesn't find it.
Is there a simple way of doing this ?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should work - can you show the code?
Does it work when you specify Object.class as the parameter type? If so, you could use that to get the method and then invoke with the subclass.
 
huguette santos
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code :
import java.lang.reflect.Method;

And here the output :
execute
java.lang.NoSuchMethodException: TestReflection.execute(TestReflection$B)
at java.lang.Class.getMethod(Class.java:978)
at com.lge.test.TestReflection.invokeMethod(TestReflection.java:33)
at com.lge.test.TestReflection.main(TestReflection.java:28)
Exception in thread "main"

}
[ September 12, 2003: Message edited by: Cindy Glass ]
 
Gary Mann
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right - I thought this would work but it doesn't. Obviously the Java API looks for an exact match. I tried to check out the code but it's a native method.

The following code works for your specific example. It would be more complex if there were multiple params or if we needed to consider interface references.
 
huguette santos
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gary,
I was already considering this solution which is just fine for me.
But I still wonder if there is no other way to do it, especially when having more than one parameter !
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's none that I can find. This does seem to be an unfortunate limitation on using Reflection. For my uses of reflection, however, I can describe the method separetly of the actual parameters.
Implementing the above for multiple parameters would be a nightmare to get the correct Java functionality. You would have to find all available methods, figure out which one is correct, and (if there are more than one that is the "most specific") throw some sort of runtime "ambiguous method call" error. Bleh.
I won't dissuade you from using Reflection, but anytime you do use it, you should stop and ask yourself, "Self, is there a better way of doing this?" Most of the time there is--however, if you are working with generic JavaBeans, then there might not be.
 
Gary Mann
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't think of a way either. It's a shame though, because the code to match the params and find the best fitting method must exist within the JVM because it occurs at runtime when you call a method directly. It wouldn't be too hard to write an algorithm - I bet someone's already done it.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
There is one good tutorial on Java Reflection
http://developer.java.sun.com/developer/technicalArticles/ALT/Reflection/
It might be of some help.
Cheers,
Gaya3
 
reply
    Bookmark Topic Watch Topic
  • New Topic