CallSuper 注释如何评估重写方法是否正在调用 super 方法?
How CallSuper annotation evaluates whether overridden method is calling the super method or not?
我只是很好奇我的 andorid studio 如何在我的 Activity 的 onCreate()
方法中通知我,当我无法调用 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 开发人员工具中的 lint 规则工作。
不只是 @CallSuper
annotation acting alone. The annotation works as an identifier for the accompanying lint rule. Check out the source of the CallSuperDetector
。
我只是很好奇我的 andorid studio 如何在我的 Activity 的 onCreate()
方法中通知我,当我无法调用 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 开发人员工具中的 lint 规则工作。
不只是 @CallSuper
annotation acting alone. The annotation works as an identifier for the accompanying lint rule. Check out the source of the CallSuperDetector
。