why is @SuppressLint("WrongCall") required only after method extraction

176 Views Asked by At

I have this andorid code:

      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       //some code
          super.onMeasure(widthMeasureSpec,heightMeasureSpec);
   ..
      }

no errors.

but when I extract the last lines into a private method I get this warning which requires to add a suppression annotation to the private method.

specious method call, should probably call measure

      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       ...
        foo(textMaxWidth);
      }

      private void foo(int textMaxWidth) {
...
          super.onMeasure(widthMeasureSpec,heightMeasureSpec);
   ..
      }

why is the suppression needed only after extraction?

@SuppressLint("WrongCall")

1

There are 1 best solutions below

6
TheWanderer On

Because usually you only call super methods from their overrides, not from separate methods. And if you do call them from separate methods, you might not be overriding them (so super.whatever() is the same as whatever());

Your IDE is warning you of this in case you accidentally copied some code over and included that call by accident.

In your example code at least, I don't really see a reason to do the super call in a separate method though. If you need to contents of foo() to be called before the super method, just put it after you invoke foo().