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

Time:08-08

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

CodePudding user response:

Structural search for imports/packages is not supported yet - enter image description here

CodePudding user response:

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

  • Related