IntelliJ: Preventing Wildcard Imports

PROBLEM

I have been getting the following PMD warning that I can’t seem to suppress with PMD’s @SuppressWarnings

RequestServiceImpl.java:1, UnnecessaryFullyQualifiedName, Priority: Normal

Unnecessary use of fully qualified name 'com.choonchernlim.epicapp.service.impl' 
due to existing import 'com.choonchernlim.epicapp.service.*'.

The reason I’m getting this warning is because IntelliJ will automatically replace all single imports from the same package with a wildcard ‘*’ when the class count hits a certain limit.

By using wildcard imports, it also becomes difficult for my peers to understand where the classes are coming from.

SOLUTION

To fix this permanently in IntelliJ:-

  1. Go to Preferences…Code StyleJava.
  2. Go to Imports tab.
  3. Check Use single class import
  4. Set a ridiculous high number on Class count to use import with ‘*’ and Names count to use static import with ‘*’ because these fields cannot be empty. In my case, I set 100 for each field.
  5. Select OK.

Finally, perform an Optimize Imports… on that Java file in question.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s