我可以覆盖一个隐藏(但公众)方法,并调用其超强的方法? [英] Can I override a hidden (but public) method and call its super method?

查看:167
本文介绍了我可以覆盖一个隐藏(但公众)方法,并调用其超强的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个非公开的API,我需要为了要解决与Android的WebView中一个怪癖覆盖。

API是隐藏的,但它是公共的:

  / **
 * ...
 *
 * @hide未决API国务院批准
 * /
公共布尔selectText(){
    ...
}

所以,我可以通过简单地在自己的WebView类声明它覆盖它,减去@覆盖:

 公共布尔selectText(){
    ...
}

是否有可能调用从我重写超级方法?通常情况下我可以写:

 公共布尔selectText(){
    返回super.selectText();
}

但这种方法是隐藏的,所以 super.selectText()不可用。如果我使用反射:

 公共布尔selectText(){
    回报(布尔)WebView.class.getMethod(selectText)调用(这一点,(对象[])为空)。
}

我得到一个无限循环,因为它叫我重写的方法。

反正是有覆盖这个方法,并能够调用超级方法?

谢谢!


解决方案

  

反正是有覆盖此方法并能够调用超
  方法是什么?


没有,不幸的是,在回答解释的问题<一href=\"http://stackoverflow.com/questions/5411434/how-to-call-a-superclass-method-using-java-reflection\">How使用Java反射来调用父类的方法你不能用反射解决这个问题。

There is a non public api that I need to override in order to workaround a quirk with Android's WebView.

The api is hidden but it is public:

/**
 * ...
 *
 * @hide pending API council approval
 */
public boolean selectText() {
    ...
}

So I can override it by simply declaring it in my own WebView class, minus the @Override:

public boolean selectText() {
    ...
}

Is it possible to call the super method from my override? Normally I could write:

public boolean selectText() {
    return super.selectText();
}

But the method is hidden, so super.selectText() is not available. If I use reflection:

public boolean selectText() {
    return (Boolean) WebView.class.getMethod("selectText").invoke(this, (Object[]) null);
}

I get an infinite loop because it calls my overridden method.

Is there anyway to override this method AND be able to call the super method?

Thanks!

解决方案

Is there anyway to override this method AND be able to call the super method?

No, unfortunately, as is explained in the answer to the question How to call a superclass method using Java reflection you cannot solve this problem with reflection.

这篇关于我可以覆盖一个隐藏(但公众)方法,并调用其超强的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆