[英]Invoke a method in Singleton class using reflection in java
I have a following class: 我有以下课程:
Public class ABCinfo {
private static ABCinfo instance = null;
Public static ABCinfo getInstance(Param param); // get instance
Private ABCinfo(Param); // making a Singleton class
Public void methodA(Param1 param1); // need to invoke this method
}
using reflection in java, how can i invoke the methodA()
. 在Java中使用反射,如何调用
methodA()
。 Basically I am writing an Android Application, i want to use method in already existing(assume it will be present all the time) application which is in the phone. 基本上,我正在编写一个Android应用程序,我想在手机中已经存在的应用程序中使用方法(假定它将一直存在)。
Below are the things I have tried: 以下是我尝试过的事情:
String apkName = activity.getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
PathClassLoader myPathClassLoader =
new dalvik.system.PathClassLoader(
apkName,
ClassLoader.getSystemClassLoader());
Class<?> handler = Class.forName(className, true, myPathClassLoader);
Method m0 = handler.getDeclaredMethod("getInstance", new Class[] { Param.class });
m0.setAccessible(true);
Object b = m0.invoke(null, new Object[]{ Param});
Method m = handler.getMethod("methodA", new Class[] { Param1.class});
Param1 methodParameter = "asdf";
Object b1 = m.invoke(b, new Object[] { methodParameter });
But its still not working. 但是它仍然无法正常工作。
我在下面找到了解决方案及其类似内容
Object b1 = m.invoke(handler.newInstance(), new Object[] { methodParameter });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.