0

I am able invoke a method with arguments by a variable name if number of arguments and argument types are known, But how to get the declared method if no if arguments and argument type are known only at the time of search[search for method ].

public static void invokeMethod (String myClass, 
                                 String myMethod,  
                                 Class[] params, Object[] args)
                           throws Exception {
   Class c = Class.forName(myClass);
   Method m = c.getDeclaredMethod(myMethod, params);
   Object i = c.newInstance();
   Object r = m.invoke(i, args);

}

invokeMethod("myLib", "sampleMethod", new Class[] {String.class, String.class},
       new Object[]
         {new String("Hello"), new String("World")});

What if I am not aware of the count and type of Class[]? How to manage this dynamically? I will get the arguments and method through the command line or a socket. So I am not aware that which method will be receiving.

Edit- I tried below things-

Class[] css = new Class[10] ;
Object[] obj = new Object[10];
                int argLn = params.length;
            if (argLn > 1) {

                func = params[0].trim();
                for (int Idx = 1; Idx < argLn; ++Idx) {

                    arg.add(params[Idx]);
                    try {
                        Integer.parseInt((params[Idx]));
                        css[Idx-1] = String.class;
                    } catch (NumberFormatException ne) {
                        css[Idx-1] = int.class;

                    }
                }

But ended up in exception- NoSuchMethodException.

3
  • 1
    Do you know how to create and fill an array dynamically (without just writing it in the source code)? Commented Jan 8, 2015 at 10:51
  • Now that you've posted some code I think this Stack Overflow guideline applies: "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself." The code snippet you have there doesn't demonstrate very well exactly what you're doing. Commented Jan 8, 2015 at 10:57
  • sorry, I am new here, thanks i will keep up guidlines :) Commented Jan 8, 2015 at 13:13

1 Answer 1

0

This is dealt with on the Oracle tutorials website - see "Obtaining Method Type Information" part - of the general tutorial on reflection.

In summary - after you call

Method m = c.getDeclaredMethod(myMethod, params);

you need something like this:

Class<?>[] pType  = m.getParameterTypes();

and/or (depending whether your methods might use generics in their parameter types)

Type[] gpType = m.getGenericParameterTypes();

The length of the returned array will give you the number of parameters, the members their class or type. You can pass the pType array straight to your invokeMethod() method

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.