I would like to invoke a private static method. I have its name. I've heard it can be done using Java reflection mechanism. How can I do it?
EDIT: One problem I encountered when trying to invoke the method is how to specify the type of its argument. My method receives one argument and its type is Map. Therefore I cannot do Map<User, String>.TYPE
(In run time there's no such a thing as Map because of Java Type erasure). Is there another way to get the method?
After we have the class instance, we can get the public static method object by calling the getMethod method. Once we hold the method object, we can invoke it simply by calling the invoke method. It's worthwhile to explain the first argument of the invoke method.
We can invoke a static method by using its class reference. An instance method is invoked by using the object reference. 5. We can't access instance methods and instance variables with the help of Static methods in Java.
To access private method Java reflection class Class provides two methods Class.getDeclaredMethod(String name, Class[] parameterTypes) and Class.getDeclaredMethods() by using anyone of these two you can invoke private method(s).
Invoking private method. To access private method Java reflection class Class provides two methods Class.getDeclaredMethod(String name, Class[] parameterTypes) and Class.getDeclaredMethods() by using anyone of these two you can invoke private method(s).
In Java, The Package java.lang.Reflect provides the classes and interface to obtain reflective information about classes and objects. Now, the task is to invoke a Static method using Reflection in Java.
You can call the private method from outside the class by changing the runtime behaviour of the class. With the help of java.lang.Class class and java.lang.reflect.Method class, we can call a private method from any other class.
Let's say you want to call MyClass.myMethod(int x);
Method m = MyClass.class.getDeclaredMethod("myMethod", Integer.TYPE); m.setAccessible(true); //if security settings allow this Object o = m.invoke(null, 23); //use null if the method is static
Invoke main from reflection tutorial
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; public class InvokeMain { public static void main(String... args) { try { Class<?> c = Class.forName(args[0]); Class[] argTypes = new Class[] { String[].class }; Method main = c.getDeclaredMethod("main", argTypes); String[] mainArgs = Arrays.copyOfRange(args, 1, args.length); System.out.format("invoking %s.main()%n", c.getName()); main.invoke(null, (Object)mainArgs); // production code should handle these exceptions more gracefully } catch (ClassNotFoundException x) { x.printStackTrace(); } catch (NoSuchMethodException x) { x.printStackTrace(); } catch (IllegalAccessException x) { x.printStackTrace(); } catch (InvocationTargetException x) { x.printStackTrace(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With