English 中文(简体)
java 装满物体的反射方法
原标题:java reflection call methods of an already loaded object

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?

增 编

问题回答

您将“级”作为第一个参数转过<条码>。

result = m.invoke(myInstance, null);

我认为你需要你帮助。

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

而不是:

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

你应该通过我的地图册的instance。 非法的论点例外是,援引的是一类类别而不是一类我的类别:

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

如果我提到这个目标,我为什么需要思考? 我想说的是,一种在证书上已经从另一个物体装载的物体的方法,而没有提及。

当然,你需要提及一种方法,你可以使用这种方法:

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

但是,案例的寿命取决于你是如何创造的(通常或思考)。

关于<代码>invoke的内容是,如果所称作的方法不要求父母类别的任何实例变量,则你必须掌握有关类别的确切例子。 事实上,请在<条码>上添加 modifier至Method,并仅以<条码>null、0、目标[]为标题,与:

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

我也存在同样的问题,因为谁知道这个问题,如果你在你想要呼吁的同一级别上,就非常简单:

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

随同<>,你将自审,不丧失你的价值观。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签