Code Inspection for methods not statically imported

Answered

There are some methods which I always want to statically import (Collectors.toList for example). I would like to be able to configure Intellij to mark as warning or even error the usage of these methods with class name ("Collectors.toList()" should be a warning, "toList()" should not). The suggestion to statically import the method is already available, I just need the warning/error.

I've tried "Structural search inspection" and did not managed to solve it, "$expression$.collect(Collectors.toList())" pattern matches ".collect(toList())" as well, so the warning does not go away after static import.

Is there a way to do this?

Thanks

2 comments
Comment actions Permalink
Official comment

You can use pattern like this:

 

$Collectors$.toList()

 

With a text filter on $Collectors$ to match "Collectors" or "java\.util\.stream\.Collectors". You can also add the same pattern as a replacement and check the "Use static imports" box, to add the static import as a quick fix.

 

Bas

Comment actions Permalink

Thanks a lot! It worked.

This is what I ended with:

0

Please sign in to leave a comment.