views:

79

answers:

4

Hi,

How can I call the method of an object that has 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

+5  A: 

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

Ladlestein
How do I find myInstance? It has been already been loaded in JVM and have no access to it from this class
@user: You have to have access to the instance to invoke anything on it - you would need to pass it in or get a reference to it from somewhere.
aperkins
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
@user, there are many reasons to use reflection (e.g. iterating through all methods, calling a method when you have the name as a string, etc.). However, calling an instance method still requires an instance.
Matthew Flaschen
@Matthew: So what I am trying to do is not possible?
@user I can tell you that there are lots of reasons to use reflection when you have the object in question - however, afaik there is no way to get access to random data in the heap; you would need a way to get a handle on it.
aperkins
+1  A: 

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);
Connor M
+1  A: 

I think you need

Class myClass = myObject.GetClass();
Method m = com.test.class.getDeclaredMethod("getValue",new Class[] {});
Object result = m.invoke(myObject,null);
BioBuckyBall
A: 

The thing about invoke, is that you dont have to have the exact instance of the class in question, if the method being called dosnt 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(). } }

Shadow Six