4

The objective is simple, I want to create a method which load a class dynamically, access its method and passing their parameters value and getting the return value at run-time.

Class which will be called

class MyClass {

    public String sayHello() {

        return "Hello";
    }

    public String sayGoodbye() {

        return "Goodbye";
    }

    public String saySomething(String word){
        return word;
    }
}

Main Class

public class Main {


    public void loadClass() {
        try {

            Class myclass = Class.forName(getClassName());

            //Use reflection to list methods and invoke them
            Method[] methods = myclass.getMethods();
            Object object = myclass.newInstance();

            for (int i = 0; i < methods.length; i++) {
                if (methods[i].getName().startsWith("saySome")) {
                    String word = "hello world";

                    //**TODO CALL OBJECT METHOD AND PASS ITS PARAMETER**
                } else if (methods[i].getName().startsWith("say")) {

                    //call method
                    System.out.println(methods[i].invoke(object));
                }

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private String getClassName() {

        //Do appropriate stuff here to find out the classname

        return "com.main.MyClass";
    }

    public static void main(String[] args) throws Exception {

        new Main().loadClass();
    }
}

My question is how to invoke method with parameters and passing its value? also getting the return value and its type.

2
  • viralpatel.net/blogs/… Commented Dec 10, 2012 at 7:19
  • System.out.println(methods[i].invoke(object, word)); Commented Dec 10, 2012 at 7:23

3 Answers 3

4

I think you're just missing the fact that you can pass in arguments to invoke, as an Object[]:

Object result = methods[i].invoke(object, new Object[] { word });

Or using varargs, if you prefer:

Object result = methods[i].invoke(object, word);

(The above two calls are equivalent.)

See the documentation for Method.invoke for more details.

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

1 Comment

that's it, yes my mistake.. the second param of methods[i].invoke is for the arguments..thanks Jon
1

simply create the object of MyClass invoke the function like this

MyClass mc = new MyClass();
String word = "hello world";
String returnValue = mc.saySomething(word);
System.out.println(returnValue);//return hello world here

or do this

Class myclass = Class.forName(getClassName());
Method mth = myclass.getDeclaredMethod(methodName, params);
Object obj = myclass.newInstance();
String result = (String)mth.invoke(obj, args);

Comments

0

Try ::

Class c = Class.forName(className); 
Method m = c.getDeclaredMethod(methodName, params);
Object i = c.newInstance();
String result = (String)m.invoke(i, args);

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.