8

In one of my Android projects (but not necessarily tied to Android) I have quite a few method calls that really do nothing but blow up the code and could be automatically removed. Examples:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

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

I couldn't find any inspections that help me in automatically removing these expressions, so I tried structural search. My attempt so far: I copied the template of 'annotated methods' and made 2 small changes.

  1. $Annotation$ changed to occurs=1, text=Override
  2. Added a $Statement$ variable with occurs=1

The template code:

class $Class$ {
    @$Annotation$( )
    $MethodType$ $MethodName$($ParameterType$ $ParameterName$) {
        $Statement$;
    }
}

So far, so good - it's only finding methods with a single line in the body. But now I want to explicitely search for exact statements calling the super method (kind of like a back reference to $MethodName$), but which also return the super value (when not void). Any ideas?

I believe this would be a really useful inspection that could be integrated into the main IntelliJ codebase as well. :-)

mreichelt
  • 12,359
  • 6
  • 56
  • 70

2 Answers2

9

So I recently found out that IntelliJ's 'Empty method' inspection actually looks for this. Simply:

Double Shift -> Run Inspection By Name -> Empty method

method only calls its super

The synopsis is 'Method only calls its super', but the inspection actually looks for more than just this, for example:

  • The method and all its derivables are empty
  • All implementations of this method are empty
  • The method is empty
  • Empty method overrides empty method

Depending on your situation it might find more than you want - and the refactoring tried to remove more than I actually wanted. But with a quick manual review you should be good to go. :-)

mreichelt
  • 12,359
  • 6
  • 56
  • 70
1

Using Structural Search you will have to use two separate searches. One for finding methods with void return type:

class $Class$ {
    $MethodType$ $MethodName$($ParameterType$ $ParameterName$) {
        super.$MethodName$($ParameterName$);
    }
}

and a second for methods which return a value:

class $Class$ {
    $MethodType$ $MethodName$($ParameterType$ $ParameterName$) {
        return super.$MethodName$($ParameterName$);
    }
}

Specifying the @Override annotation is unnecessary in this case.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
  • No, this does not work. This template finds much more methods - not only those which call super. Though I don't know why... Also, it ignores the parameters which have to be passed to the super methods. – mreichelt Aug 04 '15 at 15:56
  • 1
    Oops, I should have tried this out first. It's probably a bug. I will try to get this fixed. – Bas Leijdekkers Aug 04 '15 at 20:12
  • Searching for super methods in this way works correctly in IntelliJ IDEA 2016.3. – Bas Leijdekkers Nov 19 '16 at 16:04