Methods for calling java reflection of an already loaded object

How can I call the method of an object that has already been loaded into 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: the object is not an instance of a class declaration . The method I want to call is invalid, i.e. Does not accept parameters

UPDATE I have an application that has already loaded class "A". Another class β€œB” will be created using the framework. When class "B" is initialized, class "A" is already loaded into the JVM. I want to call a method from a loaded instance of class "A", but without reference to "A" in class "B". In the answers, it seems that I should create a new instance of β€œA” in class β€œB”, but I want to access an already loaded object. If I create a new instance of "A" in "B", why do I want to use reflection? Did I miss something?

thanks

+4
source share
6 answers

You pass an instance of the class as the first parameter to Method.invoke(..) , but this is wrong; You want to pass the instance you are interested in.

 result = m.invoke(myInstance, null); 
+11
source

I think you need

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

Instead:

 Object result = m.invoke(myClass, null); 

You should pass an instance of myClass. An invalid argument exception is associated with a call to get an argument of type Class instead of type myClass:

 Object result = m.invoke(myInstance, null); 
+2
source

If I have a reference to an object, why should I use reflection? I want to call a method of an object that is already loaded into the JVM from another object without reference to it.

Of course, you need a link to call the method, you can use it like:

 Object result = m.invoke(myClass.newInstance(),null); 

But the life span of an instance matters depending on how you create it (usually by reflection).

+1
source

The thing about invoke is that you do not need to have an exact instance of the class in question if the called method does not require any instance variables of the parent class. In fact, you can add a static modifier to Method and just call it 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(). } } 
0
source

I have the same problem, and for those who know this, it is very simple, if you are in the same class of place you want to call, do this:

 Object result = m.invoke(this, null); 

with this, you will give a copy of the selfie and not lose your values.

0
source

All Articles