How to tell a method has a varargs argument using reflection?

Posted by Anthony Kong on Stack Overflow See other posts from Stack Overflow or by Anthony Kong
Published on 2010-06-15T01:40:41Z Indexed on 2010/06/15 1:42 UTC
Read the original article Hit count: 259

Filed under:
|

Here is a sample code

package org.example;

import java.lang.reflect.Method;

class TestRef {

        public void testA(String ... a) {
                for (String i : a) {
                        System.out.println(i);
                }
        }

        public static void main(String[] args){

                Class testRefClass = TestRef.class;

                for (Method m: testRefClass.getMethods()) {
                        if (m.getName() == "testA") {
                                System.out.println(m);
                        }
                }
        }
}

The output is

public void org.example.TestRef.testA(java.lang.String[])

So the signature of the method is reported to take a array of String.

Is there any mean in the reflection library I can tell that the method is originally declared to take a varargs?

© Stack Overflow or respective owner

Related posts about java

Related posts about varargs