I just found curious how my andorid studio is able to notify me when in my onCreate()
method of Activity when I fail to call super.onCreate()
then came to know that Android (too smart) implemented annotation CallSuper
to make sure that the overridden method is calling the super method.
I just went into CallSuper
to see the magic of what's happening internally,
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 {
}
Source code reference : https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/annotation/CallSuper.java But to my surprise it does nothing. Where does it evaluate the method against this annotation? How compiler is able to do that? I'm quite curious.
It works via a lint rule bundled into the android developer tools.
It's not just the @CallSuper
annotation acting alone. The annotation works as an identifier for the accompanying lint rule. Check out the source of the CallSuperDetector
.