Code Inspection for methods not statically imported


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?


Comment actions Permalink
Official comment

You can use pattern like this:




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.



Comment actions Permalink

Thanks a lot! It worked.

This is what I ended with:


Please sign in to leave a comment.