有没有办法调用没有类对象的类的反射方法?

发布于 2024-12-26 11:10:35 字数 1799 浏览 1 评论 0原文

我正在尝试访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流绪微梦 2025-01-02 11:10:35

请参阅文档 Method#invoke(java.lang.Object, java.lang.Object...)

如果底层方法是实例方法,则使用动态方法查找来调用它,如《Java 语言规范》第二版第 15.12.4.4 节中所述;特别是,将会发生基于目标对象的运行时类型的覆盖。

抛出:
NullPointerException - 如果指定的对象为 null 并且该方法是实例方法。

因此,基本上,如果您传递 null 来调用实例方法,那么您会收到 NPE。这意味着无法在没有对象的情况下调用实例方法。这是有道理的,因为为什么您要在没有实例的情况下调用实例方法。

例如,

class MyClass {
    public void myMethod() {
        System.out.println("Hello Java Reflection!!!");
    }
}

Method theMethod = MyClass.class.getDeclaredMethod("myMethod");
System.out.println(theMethod);
theMethod.setAccessible(true);
theMethod.invoke(null);

它抛出一个 NPE,但打印“Hello Java Reflection!!!”如果我将该方法设为静态。

See the doc for Method#invoke(java.lang.Object, java.lang.Object...)

If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, Second Edition, section 15.12.4.4; in particular, overriding based on the runtime type of the target object will occur.

Throws:
NullPointerException - if the specified object is null and the method is an instance method.

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.

class MyClass {
    public void myMethod() {
        System.out.println("Hello Java Reflection!!!");
    }
}

Method theMethod = MyClass.class.getDeclaredMethod("myMethod");
System.out.println(theMethod);
theMethod.setAccessible(true);
theMethod.invoke(null);

It throws a NPE, but prints "Hello Java Reflection!!!" if I make the method static.

我不咬妳我踢妳 2025-01-02 11:10:35

SMSDispatcher 是一个抽象类,因此您无法获取它的实例。

public abstract class SMSDispatcher extends Handler {...}

尝试使用其他人。

SMSDispatcher is an abstract class, so you can't get an instance of it.

public abstract class SMSDispatcher extends Handler {...}

Try to use others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文