Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reflection and varargs

843793Jun 15 2004 — edited Jun 17 2004
I have a question on the combination of reflection and varargs. Out of pure curiosity, I would like to know how I can obtain a reference to a Method that has a varargs parameter. I've tried looking for some information on this subject, but couldn't find any posts in this forum (keyword used in forum search box: reflection). I did find an [url http://today.java.net/pub/a/today/2004/03/08/reflect.html?page=last]article on java.net that explained a few things, but it left me puzzled on this particular issue:
class Foo {
  public void methodOne(String param) {
    // whatever...
  }
  
  public void methodTwo(String ... params) {
    // whatever...
  }
  
  public void methodTwo(Number param) {
    // whatever...
  }
}
If I want to get a reference to Method object methodOne, I would do this:
Foo.getClass().getDeclaredMethod("methodOne", String.class);
Without having to iterate over the methods until I find the one that is called "methodTwo" and returns true on invocation of isVarargs, how do I get a reference to the methodTwo object that takes the String varargs as parameters?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 15 2004
Added on Jun 15 2004
8 comments
305 views