Android的方法,默认(包)的知名度压倒一切的(不应该工作,但确实 - 为什么) [英] Android method with default (package) visibility overriding (shouldn't work, but does - why?)

查看:293
本文介绍了Android的方法,默认(包)的知名度压倒一切的(不应该工作,但确实 - 为什么)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩弄于Android的SVG的支持,并提出了这个库,它声称,它支持SVG就像他们是本地。

I've been playing around with SVG support in Android and came up with this library which claims that it supports SVG just as it they were native.

由于我发现,这是不是真的有可能费尽苦心,我去看看这家伙居然做的。所以我就在他的<一个href="https://$c$c.google.com/p/adet/source/browse/trunk/adet/src/cn/mobileww/adet/graphics/MyResources.java"相对=nofollow> 资源衍生他在声明的方法(<$ C C $> loadDrawable ),有< STRONG>默认可见性在基础资源类。

Since I took great pains in discovering that this is not really possible, I went to see how this dude actually managed it. So I came upon his Resources derivative in which he declares a method (loadDrawable) that has default visibility in base Resources class.

有趣的是,通常皮棉也只是报告你可以不写这个方法,因为它会躲在基地的方法,但在这种特殊情况下(注意的由于缺少 @覆盖指令)此方法获得被称为如果它被写在基类。所有调用此方法的方法将调用覆盖,而不是原始的方法。对我来说,从传统的编译器来了,如 C ++ 帕斯卡尔,这是完全超越COM prehension。

The funny thing is, normally lint would just report that you can't write this method since it would hide base method, but in this particular case (note the absense of @Override directive) this method gets to be called as if it were written in the base class. All the methods invoking this method will invoke the override instead of original method. For me coming from classic compilers such as C++ or Pascal, this is totally beyond comprehension.

在此基础上,我设法让我的支持SVG工作完全用单一使用反射和我超级高兴这一点,但是:

Based on this, I managed to get my SVG support working completely with one single use of reflection and am super-happy about this, but:

为什么这项工作?

推荐答案

它看起来像有在Dalvik的跨preTER一个bug,它允许包专用方法被覆盖。很显然,谷歌认识到了这个问题(在果冻豆?),因为Dalvik发出警告,这是不正确的覆盖一个包私有方法在这种情况下,艺术报告为一个错误,并无法进行编译。当然,正确的行为是允许,但不允许从其他包压倒一切的包私有方法,但它看起来像谷歌希望避免破坏依赖于这种行为的现有应用程序。

It looks like there is a bug in the Dalvik interpreter which allows package-private methods to be overridden. Apparently, Google recognized this issue (in Jelly Bean?), as Dalvik reports a warning that it is incorrectly overriding a package-private method in this case, and ART reports it as an error and fails to compile it. The correct behaviour would of course be to allow it but not allow overriding package-private methods from other packages, but it looks like Google want to avoid breaking existing applications that depend on this behaviour.

更新:这是现在正式的确认在6月16日的ART文档更新,但它指出,ART发出一个严重错误,因为velis <一的一个警告href="http://stackoverflow.com/questions/23005656/android-method-with-default-package-visibility-overriding-shouldnt-work-but/23094051#comment35295472_23005656">reported在对这个问题的意见:

UPDATE: This is now officially confirmed in the ART documentation update on June 16, although it states that ART issues a warning instead of a critical error as velis reported in the comments on the question:

Dalvik的错误让子类覆盖包私有方法。技术问题在这种情况下,一个警告:

Dalvik incorrectly allowed subclasses to override package-private methods. ART issues a warning in such cases:

的Andr​​oid 4.1之前,方法无效com.foo.Bar.quux()
    会错误地覆盖在维基,包私有方法     com.quux.Quux

Before Android 4.1, method void com.foo.Bar.quux()
would have incorrectly overridden the package-private method in
com.quux.Quux

如果您打算在不同的包覆盖一个类的方法,声明的方法公开保护

If you intend to override a class's method in a different package, declare the method as public or protected.

这篇关于Android的方法,默认(包)的知名度压倒一切的(不应该工作,但确实 - 为什么)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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