java reflection call methods of an already loaded object

15,419

Solution 1

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

Solution 2

I think you need

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

Solution 3

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

Solution 4

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

Share:
15,419
Cratylus
Author by

Cratylus

Updated on June 05, 2022

Comments

  • Cratylus
    Cratylus almost 2 years

    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

  • Cratylus
    Cratylus over 13 years
    How do I find myInstance? It has been already been loaded in JVM and have no access to it from this class
  • aperkins
    aperkins over 13 years
    @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.
  • Cratylus
    Cratylus over 13 years
    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
  • Matthew Flaschen
    Matthew Flaschen over 13 years
    @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.
  • Cratylus
    Cratylus over 13 years
    @Matthew: So what I am trying to do is not possible?
  • aperkins
    aperkins over 13 years
    @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.
  • Tinus Jackson
    Tinus Jackson about 5 years
    Where do you use myClass ?