Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotate interface function that must call super

I'm creating interface and some function in it has a body. It's required, that class that implements this interface must call super in overriding function before executing other code. How can I do this?

interface Watcher {
    fun funWithoutBody()

    fun startWatching() {
        //do some important stuff which must be called
    }
}
like image 978
Dima Rostopira Avatar asked Oct 20 '16 11:10

Dima Rostopira


2 Answers

I've accidentally found, what I was looking for. It's a @CallSuper annotation available in androidx.annotation package. Docs

Use the @CallSuper annotation to validate that an overriding method calls the super implementation of the method. The following example annotates the onCreate() method to ensure that any overriding method implementations call super.onCreate():

@CallSuper
protected fun onCreate(savedInstanceState: Bundle?) {
}
like image 109
Dima Rostopira Avatar answered Sep 28 '22 08:09

Dima Rostopira


Android Studio / IntelliJ IDEA does this sort of thing in some cases but it isn't done through annotations but through code inspection.

e.g. MissingSuperCall is an Android Lint Check for which IntelliJ IDEA supports (Integration with Android Lint tool in IntelliJ IDEA 11.1 | IntelliJ IDEA Blog).

You can create your own custom inspection if you are using Android Studio or IntelliJ IDEA: IntelliJ IDEA 2016.2 Help :: Creating Custom Inspections.

like image 29
mfulton26 Avatar answered Sep 28 '22 08:09

mfulton26