I have a problem using the invoke
method in java.
I have a method to use to provide me a Method
object and it looks like:
public static Method provideMethod(String methodName, Class targetClass) throws NoSuchMethodException {
Method method = targetClass.getDeclaredMethod(methodName,null);
//Set accessible provide a way to access private methods too
method.setAccessible(true);
return method;
}
Ok this method works perfectly when I'm trying to access methods, from any context (static, or non static), that have no arguments.
Now the problem is that I can't call invoke and pass arguments to a method that have arguments, for instance:
I have the following method :
private static boolean createDirectory(String path, String fileName) {
...
}
And I want to invoke it like this:
Boolean created = (Boolean) DataUtils.provideMethod("createDirectory", FileUtils.class).
invoke(null, String.class, String.class);
But I'm getting java.lang.NoSuchMethodException: createDirectory []
.
Does somebody knows how can I invoke a private static method that have parameters ?
And, how can I pass values to that methods arguments?
Thanks, Arkde
You can access the private methods of a class using java reflection package.
In the example above, we first obtain the instance of the class we want to test, which is GreetingAndBye. 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.
If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.
Accessing private fields in Java using reflection In order to access a private field using reflection, you need to know the name of the field than by calling getDeclaredFields(String name) you will get a java. lang. reflect. Field instance representing that field.
You're explicitly calling a reflection method which looks for a method declared with the given parameter types - but you're not providing any parameter types.
If you want to find any method with the given name, use getDeclaredMethods()
and just filter by name... but then when you call invoke
, you need to provide the string values, not the parameter types.
Alternatively, change your provideMethod
call to also accept the parameter types, so you can use:
DataUtils.provideMethod("createDirectory", FileUtils.class,
String.class, String.class)
.invoke(null, "foo", "bar")
You're specifically only looking up methods with no arguments when you call
Method method = targetClass.getDeclaredMethod(methodName,null)
In order to find the createDirectory method, you'd need to call
targetClass.getDeclaredMethod("createDirectory", String.class, String.class)
but at present your provideMethod
method has no way of doing this.
I would suggest that you change the signature of provideMethod
so that it allows the caller to pass in the classes of the arguments that they're looking for, like so:
public static Method provideMethod(String methodName, Class targetClass, Class... parameterTypes) throws NoSuchMethodException {
Method method = targetClass.getDeclaredMethod(methodName, parameterTypes);
//Set accessible provide a way to access private methods too
method.setAccessible(true);
return method;
}
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