Invoking a Static Method Using Reflection

Invoking a static method using reflection

// String.class here is the parameter type, that might not be the case with you
Method method = clazz.getMethod("methodName", String.class);
Object o = method.invoke(null, "whatever");

In case the method is private use getDeclaredMethod() instead of getMethod(). And call setAccessible(true) on the method object.

Invoke static method using reflection

Problem solved:)

ModelA and ModelB implement the same interface, for example Model (not important) ,model.getClass() is an important part of his

public static void insert(Context context, Model model) {
Class<?> clazz = Class.forName(mClass); //Controller A or B
Method method = clazz.getMethod("insert", Context.class, model.getClass());
method.invoke(null, new Object[]{context, model}));
}

Now i can use this :

Controller.insert(this, myModelA);
Controller.insert(this, myModelB);

Thanks all.

Calling a static method of a class using reflection

Your main problem is you need to specify the full namespace of SA010 when using GetType.

Type type = Type.GetType("SomeNamespace.SA010");

However if you are not dynamicly generating the name a easier solution is use typeof, this does not require you to entire the namespace in if the type is already within scope.

Type type = typeof(SA010);

2nd issue you will run in to once you fix the type, if a method is static you don't create a instance of it, you just pass null in for the instance for the Invoke call.

private SA GetData()
{
Type type = typeof(SA010);

MethodInfo methodInfo = type.GetMethod("GetSA");

return (SA)methodInfo.Invoke(null, null);
}

Call static method with reflection

As the documentation for MethodInfo.Invoke states, the first argument is ignored for static methods so you can just pass null.

foreach (var tempClass in macroClasses)
{
// using reflection I will be able to run the method as:
tempClass.GetMethod("Run").Invoke(null, null);
}

As the comment points out, you may want to ensure the method is static when calling GetMethod:

tempClass.GetMethod("Run", BindingFlags.Public | BindingFlags.Static).Invoke(null, null);

Java 8 : Invoke Interface's static method using reflection

I see no problems:

TimeClient.class.getDeclaredMethod("testStatic").invoke(null);

Works without problems and prints "In the Static". The getMethod also works as expected:

TimeClient.class.getMethod("testStatic").invoke(null);

Using reflection to call static method for return value

You don't want null as your argument or parameter list. Instead you can do

Method method = classLoader.loadClass("testClass").getMethod("getInstance", new Class[0]);
Object object = method.invoke(null, new Object[0]);

or the following as they are varargs methods.

Method method = classLoader.loadClass("testClass").getMethod("getInstance");
Object object = method.invoke(null);
// or works but is perhaps confusing.
Object object = method.invoke(null, null);

Invoking a static method with java reflection library

java.lang.IllegalAccessException: Class
org.baiocchi.client.reflection.Game can not access a member of class
ah with modifiers "static"

IllegalAccessException is thrown if you want to invoke a private or otherwise inaccessible method (generally a package private method while you don't invoke it from the package of the class).

You can invoke public void setAccessible(boolean flag) on the Method object to remove this constraint :

initMethod.setAccessible(true);

Set the accessible flag for this object to the indicated boolean
value. A value of true indicates that the reflected object should
suppress Java language access checking when it is used. A value of
false indicates that the reflected object should enforce Java language
access checks.

Java Reflection on Android Studio unable to find public static method to invoke

Turns out Proguard was removing the method from the build since it was not being used elsewhere. Added an exception for the class and the issue went away.



Related Topics



Leave a reply



Submit