javaintellij-ideareplacestructural-search

How do I create a custom IntelliJ warning/error for import statements?


I want to add a custom warning/error to IntelliJ that triggers if my Java project contains imports to a certain package.

I have looked into Search Templates/Replace Templates, but whenever I try to type code with an "import", it gives me an "Unexpected Token" error and refuses to be created.

This is fine:

"class MyCLass {}" causes no errors, and Ok button is selectable

This is not:

"import System.out; class MyClass {}" causes an "Unexpected token" popup, and Ok button is greyed out

I also can't find examples of search templates that match on import statements. The closest thing I can find is an example in the above link where a "use static imports" checkbox is available that will make the replacement's output contain a static import.

Do search templates just not support matching on import statements for some reason, and if so why? Is there another way to do what I want to do?

Thanks in advance.

My IntelliJ version is 2022.2


Solution

  • I think "illegal package dependencies" inspection is what you are looking for. It can be found under "Settings/Editor/Inspections/JVM languages/Illegal package dependencies". You can create a custom scope for all the dependencies that you don't want in your project and apply them to the predefined "All" scope. Example