开发者

How do I force a polymorphic call to the super method?

开发者 https://www.devze.com 2023-03-08 19:40 出处:网络
I have an init method that is used and overridden through out an extensive heirarchy. Each init call however extends on the work that the previous did. So naturally, I would:

I have an init method that is used and overridden through out an extensive heirarchy. Each init call however extends on the work that the previous did. So naturally, I would:

@Override public void init() {
   super.init();
}

And naturally this would ensure that everything is called and instantiated. What I'm wondering is: Can I create a way to ensure that the super method was called? If all of the init's are not call, there is a break down in the obejct, so I want to throw an exception or an error if somebody forgets to call su开发者_StackOverflow中文版per.

TYFT ~Aedon


Rather than trying to do that -- I don't think it's achievable btw! -- how about a different approach:

abstract class Base {
 public final void baseFunction() {
   ...
   overridenFunction(); //call the function in your base class
   ...
 }

 public abstract void overridenFunction();
}
...
class Child extends Base {
 public void overridenFunction() {...};
}

...
Base object = new Child();
object.baseFunction(); //this now calls your base class function and the overridenFunction in the child class!

Would that work for you?


Here's one way to raise an exception if a derived class fails to call up to the superclass:

public class Base {
    private boolean called;
    public Base() { // doesn't have to be the c'tor; works elsewhere as well
        called = false;
        init();
        if (!called) {
            // throw an exception
        }
    }
    protected void init() {
        called = true;
        // other stuff
    }
}


Android actually accomplishes this in the Activity class. I'm not sure how or whether they had to build support into the runtime for it, but I'd check out the open source code for the Activity class implementation. Specifically, in any of the lifecycle methods, you have to call the corresponding super class method before you do anything otherwise it throws SuperNotCalledException.

For instance, in onCreate(), the first thing you have to do is call super.onCreate().


I frequently like to use this solution. It wont throw a runtime error, but it will show a syntax error:

 @CallSuper
 public void init() {
     // do stuff
 }

This is a part of Android support annotations.


Make the class at the top of the inheritance tree set a flag on initialization. Then a class in the bottom of the inheritance tree can check for that flag to see if the whole tree has been traversed. I would make documentation that every child of base should include the following init code:

super.init()
if (!_baseIsInitialized) {
    // throw exception or do w/e you wish
}

where base uses

_baseIsInitialized = true;

The other way around, forcing your childs to call super.init() is a lot thougher and would most likely include ugly hacks.


I don't know of any way to do this with a method.

However, note that this is exactly how constructors work. Every constructor must, directly or indirectly, call one of its superclass's constructors. This is statically guaranteed.

I note that you are writing an init method. Could you refactor so that your code uses constructors rather than init methods? That would give you this behaviour right out of the gate. Some people (eg me) prefer constructors to init methods anyway, partly for just this reason.

Note that using constructors rather than init methods might not mean using them on the class you're currently looking at - there might be a refactoring which moves the state needing initialisation out into a parallel class hierarchy which can use proper constructors.


Nowadays you can annotate your method with @CallSuper. This will Lint check that any overrides to that method calls super(). Here's an example:

@CallSuper
protected void onAfterAttached(Activity activity) {
    if (activity instanceof ActivityMain) {
        mainActivity = (ActivityMain) activity;
    }
}

In the example above, any methods in descendant classes that override onAfterAttached but do not call super will make Lint raise an error.

0

精彩评论

暂无评论...
验证码 换一张
取 消