Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invoke a private static method using reflection (Java)?

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?

like image 921
snakile Avatar asked Jan 22 '11 20:01

snakile


People also ask

How do you call a static method in a reflection?

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.

How do you invoke a static 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.

How to access private method in Java reflection class?

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).

How do you invoke a private method in Java?

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).

How to invoke a static method using reflection in Java?

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.

How to call a private method from outside the class?

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.


2 Answers

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 
like image 150
Landei Avatar answered Sep 21 '22 15:09

Landei


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();     }     } } 
like image 38
Cratylus Avatar answered Sep 20 '22 15:09

Cratylus