• Post Reply Bookmark Topic Watch Topic
  • New Topic

getMethod(); - reflection problem  RSS feed

 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem with the following code. I'm trying to get a method named "returnShape" whos arguments consist of 1 string parameter into an array of method(s).



I've got a feeling I'm passing the wrong number of parameters, but I'm not sure how to specify just the 1 string parameter.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the signature of the method you want to find? If it has a single String parameter you'll have to put a single entry in that class array with String.getClass() in it.
 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I've got now...



It still isn't working. I couldn't get the "getClass()" method of String working because It's being called from a static method.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Your String.class notation is fine.

The method "getMethod()" will return only public methods implemented in the exact class you ask about. In your situation, you probably want getDeclaredMethod(), which will return all methods, public or not, including inherited ones.
 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm, i'm getting some quite weird errors now:



Is there something wrong with my java runtime?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's just what happens when you name a class the same as something that's already being imported from java.lang. The name "ClassLoader" is already taken -- use "MyClassLoader" or something for your class.
 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm, it's still giving me an error...
This is the compiler error I get:


This is my code...

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
you probably want getDeclaredMethod(), which will return all methods, public or not, including inherited ones.


That should read "*excluding* inherited ones." Only getMethod() returns inherited methods.
 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Its working now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!