简体   繁体   中英

how to add a warning for not calling super() in Java/eclipse

I would expect subclass always call super() for some method. How can I enforce or at least give a warning at compile time?

Thanks

You could engineer this a little differently to enforce the behaviour:

The super class should be abstract, or at least define the method final. Then define a protected method that the subclass will have to implement and finally have the super class call the method after completing whatever code needs to be run beforehand:

public abstract class SuperClass {
    // final so it can't be overriden
    public final void superMethod() {
        // required code here

        // then delegate to implMethod
        implMethod();
    }

    protected abstract() void implMethod();
}

public class SubClasss extends SuperClass {
    protected void implMethod() {
        // sub class logic
    }
}

Of course, SuperClass doesn't have to be abstract, you could implement the implMethod and then allow subclasses to override it

I think that Chris White 's answer is best in the general case. But chen ying's comment "I know enforcing to call super is not good. But I did not own the super class. And SDK documentation requires to call super. for example link" suggests that it is not suitable in this particular instance.

I would thus suggest modifying Chris White's answer to meet the particulars.

class ChenYingTestCase extends ServiceTestCase
{
       /**
        * Gets the current system context and stores it.
        * You can not extend this method.
        * If you want to achieve the effect of extending this method,
        * you must override chenYingSetupMethod.
        **/
       public final void setUp ( )
       {
             super.setUp ( ) ;
             chenYingSetup ( ) ;
       }

       /**
        * Does nothing (unless you extend it)
        *
        * Extend this method to do your 
        * own test initialization. If you do so, there is no need to call super.setUp() 
        * Hint:  calling super.setUp() is probably a bad idea.
        * as the first statement in your override.
        * Just put your test initialization here.
        * The real SetUp method will call super.setUp() and then this method.
        **/
       protected void chenYingSetUp ( )
       {
       }
}

Then if a subclass is under your control make it a subclass of ChenYingTestCase. If the subclass is not under you control, well you can't really force it to call super().

Long time, stuff changed. By now (almost 9 years later) there is a @CallSuper annotation you can add the base method to enfore that any class overwriting this method has to call super . See here for more info.

If the base class has a default (parameterless) constructor, it will always be called automatically if no explicit super() call is given. If you have control over the base class, you can make the default constructor private:

public abstract class Whatever {
    private Whatever() {
        // not visible to subclasses
    }

    public Whatever(A a, B b, ...) {
        // this constructor must always be explicitly called by subclasses
    }
}

Barring that, your IDE may allow you to turn on a warning for this. It would be somewhere in the options menus. If you don't see it, it's not there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM