java reflection call methods of an already loaded object
How can I call the method of an object that h开发者_开发问答as already been loaded in the JVM using reflection? I tried
Class myClass = Class.forName("myClass");
Method m = com.test.class.getDeclaredMethod("getValue",new Class[] {});
Object result = m.invoke(myClass,null);
but i get java.lang.IllegalArgumentException: object is not an instance of declaring class. The method I want to call is void i.e. does not take parameters
UPDATE I have an application that has already loaded a class "A". Another class "B" will be instantiated by a framework. When class "B" is initialized, class "A" has already been loaded in the JVM. I want to call a method from loaded instance of class "A" BUT without having a reference to "A" in class "B". In the answers, it seems I must create a new instance of "A" in class "B" but I want access to an already loaded object. If I create a new instance of "A" in "B" why would I want to use reflection? Am I missunderstanding something?
Thanks
You're passing the instance of Class as the first parameter to Method.invoke(..)
, but that's wrong; you want to pass the instance you're interested in.
result = m.invoke(myInstance, null);
I think you need
Class myClass = myObject.GetClass();
Method m = com.test.class.getDeclaredMethod("getValue",new Class[] {});
Object result = m.invoke(myObject,null);
Instead of:
Object result = m.invoke(myClass, null);
You should be passing in an instance of myClass. The illegal argument exception is due to invoke getting an argument of type Class instead of type myClass:
Object result = m.invoke(myInstance, null);
If I have reference to the object, why would I need to use reflection? I want to call a method of an object that is already loaded in JVM from another object without having a reference to it.
Of course, you need a reference to invoke a method, you can use it like:
Object result = m.invoke(myClass.newInstance(),null);
But the life period of the instance matters depending on how you create it (normally or by reflection).
The thing about invoke
, is that you dont have to have the exact instance of the class in question, if the method being called doesn't require any instance variables of the parent class. In fact you can add the static
modifier to the Method
and just call invoke with null, null, Object[]
which is the same as:
public void methLab(Method m){
try{
m.invoke(m.getDeclaringClass().newInstance(), new Object[0]);
}catch(IllegalAccessException iae){
// The method or parent class is declared private or protected
}catch(IllegalArgumentException iae){
// unsatisfied input parameters ( in this case, no params were passed)
}catch(InstantiationException ie){
// could be several things
}catch(InvocationTargetException it){
// Method specific, exception chain. call: it.getTargetException().
}
}
I have the same problem, and for who know it, is very simple, if you are on the same place class that you want call, do it:
Object result = m.invoke(this, null);
with this you will pass the selfie instance and do not lost your values.
精彩评论