0

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")

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

0

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().

TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • thanks, but why does the super.onMeasure() has to be in the overriden method?super call my parent instance either way. no? what did you mean `super.whatever() is the same as whatever()` it does but i call `super` in foo as well – Elad Benda Sep 30 '18 at 19:25
  • It doesn't have to be. IDE warnings are just warnings. And I meant that `super.whatever()` and `whatever()` are equivalent if you don't override `whatever()`. – TheWanderer Sep 30 '18 at 19:38
  • can you explain? if i call `super.foo()` and override `foo()`. it won't call my parent's `foo()`? and if i call `super.foo()` and won't override `foo()`. it won't call my parent's `foo()`? – Elad Benda Sep 30 '18 at 20:02
  • @EladBenda no, I said *if you aren't overriding the method* – TheWanderer Sep 30 '18 at 20:03
  • yes, but i still don't understand what would be the different execution path of calling super.onMeasure() when you override or when you *don't* override `onMeasure`? – Elad Benda Sep 30 '18 at 20:24
  • @EladBenda super calls the method in the class you extend. No super calls your override method, if it exists. – TheWanderer Sep 30 '18 at 21:25