Results 1 to 3 of 3
  1. #1
    Bill89 is offline Member
    Join Date
    Dec 2009
    Location
    Maine
    Posts
    11
    Rep Power
    0

    Default Passing an ellipsis variable to a method with an ellipsis parameter?

    Example:

    Java Code:
    public static Method getMethod(Class<?> cl, String methodName, Object...args) throws Exception {
          Method method = cl.getDeclaredMethod(methodName, args); // This obviously wouldn't work
          method.setAccessible(true);
    		
          return method;
    }
    I want the above method to call the getDeclaredMethod method, using all the Ellipsis variables('args').

    So if I called getMethod like this:

    Java Code:
    getMethod(myClass, "methodName", int.class, String.class)
    In the getMethod method it would call getDeclaredMethod like:

    Java Code:
    cl.getDeclaredMethod(methodName, int.class, String.class);
    Is it possible to do something like this?

  2. #2
    xerberuz is offline Member
    Join Date
    Apr 2010
    Posts
    8
    Rep Power
    0

    Default Re: Passing an ellipsis variable to a method with an ellipsis parameter?

    You just have to declare the args parameter as Class<?> and the code compiles.

    Java Code:
    public static Method getMethod(final Class<?> cl, final String methodName, final Class<?>... args) throws Exception
       {
          final Method method = cl.getDeclaredMethod(methodName, args);
          method.setAccessible(true);
    
          return method;
       }
    At compile time an ellipsis is converted to an array.
    Last edited by xerberuz; 09-15-2011 at 05:37 PM.

  3. #3
    Bill89 is offline Member
    Join Date
    Dec 2009
    Location
    Maine
    Posts
    11
    Rep Power
    0

    Default Re: Passing an ellipsis variable to a method with an ellipsis parameter?

    Works great. Thanks.

Similar Threads

  1. Replies: 18
    Last Post: 04-18-2011, 06:39 AM
  2. Replies: 10
    Last Post: 08-05-2010, 06:27 PM
  3. passing parameter to a thread
    By adammyth in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-02-2010, 08:58 PM
  4. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 06:46 AM
  5. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 06:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •