• 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

getMethod(); - reflection problem

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm, it's still giving me an error...
This is the compiler error I get:


This is my code...

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Its working now
 
reply
    Bookmark Topic Watch Topic
  • New Topic