Checker CallSuper

belongs to group Basic
Identify possibly missing calls to super()

Frameworks supported by this checker

  • java up to 11
  • android up to API level 28
  • dotnet

Warnings generated by this checker

  • CallSuperWarning: a call to super() is missing [ CWE573 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • none


Description

Java allows methods to call their implementation in the superclass, through the super.m(pars) syntax. It is sometimes the case that redefinitions are expected to call their implementation in the superclass. This checker identifies situations where a call to super.m(pars) seems to have been forgotten.

Action: Check if a call to super.m(pars) has been forgotten and add it consequently.

.Net allows methods to call their implementation in the superclass, through the base.M(pars); syntax. It is sometimes the case that redefinitions are expected to call their implementation in the superclass. This checker identifies situations where a call to super.m(pars) seems to have been forgotten.

Action: Check if a call to base.M(pars); has been forgotten and add it consequently.

Examples

Consider the following classes:

public class TestSuper implements Cloneable {
  private final long time = System.currentTimeMillis();

  public void m() {}

  public long getTime() {
    return time;
  }

  @Override
  protected Object clone() {
    return this;
  }
}
public class A extends TestSuper {

  @Override
  public void m() {
    super.m();
  }
}
public class B extends TestSuper {

  @Override
  public void m() {
    super.m();
  }
}
public class C extends TestSuper {

  @Override
  public void m() {
    super.m();
  }
}
public class D extends TestSuper {

  @Override
  public void m() {
  }
}

This checker issues the following warning:

D.java:5: [CallSuper: CallSuperWarning] Should you probably call super.m here? 3 redefinitions call super and only 1 does not
TestSuper.java:12: [CallSuper: CallSuperWarning] Should you probably call super.clone here?

since method m() of class D does not call super.m(), differently from the other two redefinitions of m() in classes A, B and C. Moreover, method clone() should normally call super.clone(), but this does not happen at line 12 of class TestSuper.

Consider the following classes:

namespace DocumentationExamples
{

    public class CallSuper
    {
        public static void Main(string[] args)
        { }

        public virtual void M() { }


    }
    public class CallSuperA : CallSuper
    {
        public override void M()
        {
            base.M();
        }
    }
    public class CallSuperB : CallSuper
    {
        public override void M()
        {
            base.M();
        }
    }
    public class CallSuperC : CallSuper
    {
        public override void M()
        {
            base.M();
        }
    }
    public class CallSuperD : CallSuper
    {
        public override void M()
        {
        }
    }
}

This checker issues the following warning:

DocumentationExamples.cs:39: [CallSuper: CallSuperWarning] Should you probably call "super.M" here? 3 redefinitions call "super" and only 1 does not

since method M() of class CallSuperD does not call base.M(), differently from the other two redefinitions of M() in classes CallSuperA, CallSuperB and CallSupeC.