java 已加载对象的java反射调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3688379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:00:54  来源:igfitidea点击:

java reflection call methods of an already loaded object

javareflectionjvmclassloader

提问by Cratylus

How can I call the method of an object that has already been loaded in the JVM using reflection? I tried

如何使用反射调用已加载到 JVM 中的对象的方法?我试过

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

但我得到java.lang.IllegalArgumentException: object is not an instance of declaring class。我要调用的方法是 void 即不带参数

UPDATEI 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 instanceof 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?

更新我有一个已经加载了“A”类的应用程序。另一个类“B”将由框架实例化。当类“B”初始化时,类“A”已经加载到JVM中。我想从类“A”的加载实例调用一个方法,但没有引用类“B”中的“A”。在答案中,似乎我必须在“B”类中创建一个“A”的新实例,但我想访问已加载的对象。如果我在“B”中创建一个“A”的新实例,我为什么要使用反射?我误解了什么吗?

Thanks

谢谢

回答by Ladlestein

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.

您将 Class 的实例作为第一个参数传递给Method.invoke(..),但这是错误的;你想传递你感兴趣的实例。

result = m.invoke(myInstance, null);

回答by μBio

I think you need

我想你需要

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

回答by Connor M

Instead of:

代替:

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

You should be passing in an instanceof myClass. The illegal argument exception is due to invoke getting an argument of type Class instead of type myClass:

您应该传入myClass 的一个实例。非法参数异常是由于调用获取 Class 类型的参数而不是 myClass 类型:

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

回答by sathesh

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.

如果我有对象的引用,为什么我需要使用反射?我想从另一个对象调用已加载到 JVM 中的对象的方法,而无需对其进行引用。

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

但是实例的生命周期取决于您创建它的方式(通常或通过反射)。

回答by AndreiDeholte

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 thisyou will pass the selfie instance and do not lost your values.

有了这个,您将通过自拍实例并且不会丢失您的值。

回答by Shadow Six

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 staticmodifier to the Methodand just call invoke with null, null, Object[]which is the same as:

关于invoke,的事情是,如果被调用的方法不需要父类的任何实例变量,则您不必拥有相关类的确切实例。事实上,您可以将static修饰符添加到 ,Method然后调用 invoke ,与以下null, null, Object[]内容相同:

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