Using Java Reflection we can inspect and invoke methods at runtime. This is achieved using a class named java.lang.reflect.Method.
There are basically three steps involved in invoking a method using reflection.
- Create a instance of the given class
- Prepare the parameters to invoke the method
- Invoke method using the java.lang.reflect.Method
Create a instance of the given class.
1 |
Object object = Class.forName(<<class_name>>).newInstance(); |
Prepare the parameters to invoke the method.
1 2 3 4 5 |
final String param = "sampleParam"; final List<Class<?> > paramTypes = new ArrayList<Class<?> >(params.length); final List<Object> paramInstances = new ArrayList<Object>(params.length); paramTypes.add(param.getClass()); paramInstances.add(param); |
Invoke method using the java.lang.reflect.Method.
1 2 3 |
final Object object = Class.forName((<<class_name>>).newInstance(); final Method method = object.getClass().getMethod((<<method_name>>, (Class[]) paramTypes.toArray(new Class[paramTypes.size()])); return method.invoke(object, paramInstances.toArray()); |
Complete Example Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.ourownjava.corejava.reflection; import static org.junit.Assert.assertEquals; import org.junit.Test; /** * * @author ourownjava.com * * Example program to demonstrate the power of reflection. * How to invoke a method in java using reflection. * Java method invocation using reflection. * Creating an instance of a class using reflection. * */ public class MethodReflectionBehavior { @Test public void shouldInvokeSumMethodUsingReflection() throws Exception{ assertEquals(new Integer(3), Invoker.invoke("com.ourownjava.corejava.reflection.Calculator", "sum", 1, 2)); } @Test public void shouldInvokeMultiplyMethodUsingReflection() throws Exception{ assertEquals(new Integer(4), Invoker.invoke("com.ourownjava.corejava.reflection.Calculator", "multiply", 2, 2)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.ourownjava.corejava.reflection; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * * @author ourownjava.com * */ public class Invoker { public static Object invoke(String className, String methodName, Object... params) throws Exception{ final List<Class<?> > paramTypes = new ArrayList<Class<?> >(params.length); final List<Object> paramInstances = new ArrayList<Object>(params.length); for(Object param : params){ paramTypes.add(param.getClass()); paramInstances.add(param); } final Object object = Class.forName(className).newInstance(); final Method method = object.getClass().getMethod(methodName, (Class[]) paramTypes.toArray(new Class[paramTypes.size()])); return method.invoke(object, paramInstances.toArray()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.ourownjava.corejava.reflection; /** * * @author ourownjava.com * */ public class Calculator{ public Number sum(final Integer i, final Integer j){ return i + j; } public Number multiply(final Integer i, final Integer j){ return i * j; } } |
Pingback: Invoking method using java reflection. | Clean Java