【发布时间】:2020-03-10 13:01:43
【问题描述】:
我只是好奇我的 andorid 工作室是如何在我的 onCreate() Activity 方法中当我未能调用 super.onCreate() 时通知我的,然后才知道 Android(太聪明)实现了注释 CallSuper 来制作确保被覆盖的方法正在调用超级方法。
我刚刚进入 CallSuper 看看内部发生的事情的魔力,
package androidx.annotation;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.CLASS;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Denotes that any overriding methods should invoke this method as well.
* <p>
* Example:
* <pre><code>
* @CallSuper
* public abstract void onFocusLost();
* </code></pre>
*/
@Documented
@Retention(CLASS)
@Target({METHOD})
public @interface CallSuper {
}
源码参考:https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/annotation/CallSuper.java 但令我惊讶的是,它什么也没做。它在哪里根据这个注释评估方法?编译器如何能够做到这一点?我很好奇。
【问题讨论】:
标签: android android-support-library androidx