有没有办法调用没有类对象的类的反射方法?
我正在尝试访问 SMSDispatcher 的 sendRawPdu 方法。 我能够获取方法,但无法调用它,因为我必须有 SMSDispatcher 的实例才能调用 mSendRawPdu.invoke( /* ??? */ , pdus.encodedScAddress, pdus.encodedMessage, null, null );
从 SmsManager 调用该方法效果很好,因为我可以获得这样的对象 SmsManager sm = SmsManager.getDefault();
并将其传递给 .invoke
try {
@SuppressWarnings("rawtypes")
Class c = Class.forName("com.android.internal.telephony.SMSDispatcher");
Method[] ms = c.getDeclaredMethods();
// List all methods
for (int i = 0; i < ms.length; i++) {
Log.d("ListMethos",ms[i].toString());
}
// Get method "sendRawPdu"
byte[] bb = new byte[1];
Method mSendRawPdu = c.getDeclaredMethod("sendRawPdu",bb.getClass(),bb.getClass(), PendingIntent.class, PendingIntent.class);
Log.d("success","success getting sendRawPdu");
mSendRawPdu.setAccessible(true);
// How to invoke the method not having object of type SMSDispatcher?
mSendRawPdu.invoke( /* ??? */ , pdus.encodedScAddress, pdus.encodedMessage, null, null );
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
此任务有可能的解决方案吗?
I'm trying to get access to sendRawPdu method of SMSDispatcher.
I'm able to get method, but I can't invoke it because I have to have instance of SMSDispatcher to call mSendRawPdu.invoke( /* ??? */ , pdus.encodedScAddress, pdus.encodedMessage, null, null );
Invoking the method from SmsManager works great, because I can get an object like this SmsManager sm = SmsManager.getDefault();
and pass it to .invoke
try {
@SuppressWarnings("rawtypes")
Class c = Class.forName("com.android.internal.telephony.SMSDispatcher");
Method[] ms = c.getDeclaredMethods();
// List all methods
for (int i = 0; i < ms.length; i++) {
Log.d("ListMethos",ms[i].toString());
}
// Get method "sendRawPdu"
byte[] bb = new byte[1];
Method mSendRawPdu = c.getDeclaredMethod("sendRawPdu",bb.getClass(),bb.getClass(), PendingIntent.class, PendingIntent.class);
Log.d("success","success getting sendRawPdu");
mSendRawPdu.setAccessible(true);
// How to invoke the method not having object of type SMSDispatcher?
mSendRawPdu.invoke( /* ??? */ , pdus.encodedScAddress, pdus.encodedMessage, null, null );
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Does this task has a possible solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅文档
Method#invoke(java.lang.Object, java.lang.Object...)
因此,基本上,如果您传递 null 来调用实例方法,那么您会收到 NPE。这意味着无法在没有对象的情况下调用实例方法。这是有道理的,因为为什么您要在没有实例的情况下调用实例方法。
例如,
它抛出一个 NPE,但打印“Hello Java Reflection!!!”如果我将该方法设为静态。
See the doc for
Method#invoke(java.lang.Object, java.lang.Object...)
So, basically if you pass a null to invoke an instance method then you receive a NPE. Which means it's not possible to invoke an instance method without an object. Which makes sense because why would you want to invoke an instance-method without an instance.
e.g.
It throws a NPE, but prints "Hello Java Reflection!!!" if I make the method static.
SMSDispatcher 是一个抽象类,因此您无法获取它的实例。
尝试使用其他人。
SMSDispatcher is an abstract class, so you can't get an instance of it.
Try to use others.