Add remove wildcards imports when formating and ordering imports.

See original GitHub issue

I have some java classes that have imports with wildcards like : import java.util.*;

I want to replace that with the appropriate import list like

import java.util.List;

I’m using this configuration to format my java code

<plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
                <version>2.0.1</version>
                <executions>
                    <execution>
                        <!-- Runs in compile phase to fail fast in case of formatting issues.-->
                        <id>format java</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>apply</goal>
                        </goals>
                        <configuration>
                            <formats>
                                <!-- you can define as many formats as you want, each is independent -->
                                <format>
                                    <!-- define the files to apply to -->
                                    <includes>
                                        <include>src/main/java/**/*.java</include>
                                        <include>src/test/java/**/*.java</include>
                                    </includes>
                                    <indent>
                                        <spaces>true</spaces>
                                        <spacesPerTab>4</spacesPerTab>
                                    </indent>
                                </format>
                            </formats>
                            <java>
                                <eclipse>
                                    <file>${basedir}/formatter/eclipse-format.xml</file>
                                </eclipse>
                                <removeUnusedImports/>
                                <importOrder>
                                    <file>${basedir}/formatter/eclipse.importorder</file>
                                </importOrder>
                            </java>
                        </configuration>
                    </execution>
                    
                </executions>
            </plugin>

I have this import order

#Organize Import Order
#Wed Jul 22 13:29:56 EDT 2020
0=java
1=javax
2=org
3=com

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
brendandburnscommented, Dec 3, 2020

fwiw, I added the following replaceRegexp to remove wildcards while formatting:

              <replaceRegex>
                <name>Remove wildcard imports</name>
                <searchRegex>import\s+[^\*\s]+\*;(\r\n|\r|\n)</searchRegex>
                <replacement>$1</replacement>
              </replaceRegex>
1reaction
nedtwiggcommented, Aug 21, 2022

whether there is a way to only block wildcards or throw some error message

With the Gradle plugin, you can add a custom step that throws an Exception. We don’t have a way to do that using the maven syntax.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auto import | IntelliJ IDEA Documentation - JetBrains
Disable wildcard imports to always import single classes · In the Settings/Preferences dialog ( Ctrl+Alt+S ), select Editor | Code Style | Java ......
Read more >
How to disable wildcard imports in IntelliJ IDEA - Marc Nuri
How to disable wildcard imports in IDEA? ; Settings dialog ( Ctrl+Alt+S ) and navigate to the ; Editor | ; Code Style...
Read more >
Essential tools to manage import statements in Eclipse
Adding import statements for missing references and removing ... (Essential); Let Eclipse collapse imports in the same package a wildcard (.
Read more >
Java static code analysis: Wildcard imports should not be used
Blindly importing all the classes in a package clutters the class namespace and could lead to conflicts between classes in different packages with...
Read more >
How to sort imports in alphabeticall order in Intellij?
2 Answers 2 · go into File > Settings · Go into Editor > Code Style > Java, Imports tab · Delete all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found