How to call private static method using reflection (Java)?

I would like to call a private static method. I have my own name. I heard that this can be done using the Java reflection engine. How can i do this?

EDIT: One of the problems I encountered while trying to call the method is to specify the type of the argument. My method receives one argument, and its type is Map. Therefore, I cannot execute Map<User, String>.TYPE (at runtime there is no such thing as a Map due to erasure of the Java type). Is there any other way to get the method?

+52
java reflection invoke
Jan 22 '11 at 20:38
source share
5 answers

Suppose 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 
+90
Jan 22 '11 at 20:44
source share

Call the main of the 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(); } } } 
+9
Jan 22 2018-11-21T00:
source share

No, you cannot say Map<K,V>.class . This is due to type erasure. There is no such thing at runtime.

Fortunately, you can say just the old Map.class . It is all the same at runtime.

If warnings bother you, look for other questions related to generics and type wiping, there is plenty of information on this subject.

+2
Jan 22 2018-11-22T00:
source share

I use a single method that encapsulates the receipt of the target method and then calls it. There are probably some limitations, of course. Here is the method placed in the class and its JUnit test:

 public class Invoker { /** * Get method and invoke it. * * @author jbetancourt * * @param name of method * @param obj Object to invoke the method on * @param types parameter types of method * @param args to method invocation * @return return value * @throws Exception for unforseen stuff */ public static final <T> Object invokeMethod(final String name, final T obj, final Class<?>[] types, final Object... args) throws Exception { Method method = obj.getClass().getDeclaredMethod(name, types); method.setAccessible(true); return method.invoke(obj, args); } /** * Embedded JUnit tests. */ @RunWith(JUnit4.class) public static class InvokerTest { /** */ @Test public void testInvoke() throws Exception { class TestTarget { private String hello() { return "Hello world!"; } } String actual = (String) Invoker.invokeMethod("hello", new TestTarget(), new Class<?>[] {}); String expected = "Hello world!"; assertThat(actual, is(expected)); } } 

}

+1
Jan 14 '13 at 1:21
source share
 Object insecure; //This needs to be an initialized reference Class c = insecure.getClass(); Method m = c.getMethod(name, parameterTypes); //Fill the name and types in m.setAccessible(true); m.invoke( insecure, parameters ); //Fill in the parameters you would like 

There are several checked exceptions that can be thrown. Both parameters and parameters are elliptical arguments (variable length), filling them as necessary. The specification JVM has a strongly typed calling convention, so you need to know the parameter types.

With that said, if you are not writing any application container, component component container, system like RMI, or based on JVM langauge, you should avoid this.

0
Jan 22 '11 at 20:45
source share



All Articles