Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remove wildcards imports when formating and ordering imports. #649

Open
survivant opened this issue Jul 22, 2020 · 10 comments
Open

Add remove wildcards imports when formating and ordering imports. #649

survivant opened this issue Jul 22, 2020 · 10 comments

Comments

@survivant
Copy link

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
@nedtwigg
Copy link
Member

Currently, all of Spotless' java formatters look at each file in isolation. To implement this feature, Spotless would need to hook into the full classpath and other compiled files. Which is possible, but a lot of work for a small benefit. See #240 for details. Happy to merge a PR, but the level of difficulty is quite high.

@survivant
Copy link
Author

I found this plugin. Could it be integrated to the project ? https://github.com/nxnet/organize-imports-maven-plugin

@nedtwigg
Copy link
Member

nedtwigg commented Aug 4, 2020

Happy to take a PR for it. One important caveat is that there is a ton of code out there which has already been formatted by our old broken importSorter. Some people will want the new behavior, but some projects might prefer to just not have their import ordering change.

So the new step should have a new name, perhaps organizeImportsNxNet() or organizeImportsModern(), so that people who want the old behavior can keep it.

@brendandburns
Copy link

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>

@EricGao888
Copy link
Contributor

EricGao888 commented Aug 13, 2022

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>

@brendandburns @nedtwigg This is a pretty cool way to remove wildcards automatically. May I ask whether there is a way to only block wildcards or throw some error messages when doing mvn spotless:apply instead of directly removing them? One possible scenario is that developers thought their wildcard imports got fixed and pushed their code to the remote but actually they lost some imports.

@nedtwigg
Copy link
Member

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.

@guilhemferr
Copy link

In case anyone is interested in the removal approach. The regex below also works for static imports.

<searchRegex>import\s+(?:static\s+)?[^\*\s]+\*;(\r\n|\r|\n)</searchRegex>

Note that the non-capturing group ?: here is important for it to work.

@lipiridi
Copy link

I have added an option to the plugin below that executes IntelliJ IDEA's 'optimize imports' task while disallowing the use of asterisk imports. This means that asterisk imports will be replaced with fully qualified names before applying the Spotless task.
https://github.com/lipiridi/spotless-applier
https://plugins.jetbrains.com/plugin/22455-spotless-applier

@joquijada
Copy link

https://github.com/lipiridi/spotless-applier

Do you have something that integrates with Gradle itself? In our team we want to automatically fix wildcard imports when we run Gradle spotlessApply task.

@lipiridi
Copy link

https://github.com/lipiridi/spotless-applier

Do you have something that integrates with Gradle itself? In our team we want to automatically fix wildcard imports when we run Gradle spotlessApply task.

Unfortunately, I don't.

Xtansia added a commit to Xtansia/opensearch-java that referenced this issue Sep 3, 2024
Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649

Signed-off-by: Thomas Farr <tsfarr@amazon.com>
dblock pushed a commit to opensearch-project/opensearch-java that referenced this issue Sep 4, 2024
* Forbid wildcard imports

Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Make conventions plugin

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fix wildcard imports

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Allow overriding eclipse formatter config file

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fix message formatting

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

---------

Signed-off-by: Thomas Farr <tsfarr@amazon.com>
Xtansia added a commit to Xtansia/opensearch-java that referenced this issue Sep 4, 2024
* Forbid wildcard imports

Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Make conventions plugin

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fix wildcard imports

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Allow overriding eclipse formatter config file

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fix message formatting

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

---------

Signed-off-by: Thomas Farr <tsfarr@amazon.com>
(cherry picked from commit e856ecb)
reta pushed a commit to opensearch-project/opensearch-java that referenced this issue Sep 5, 2024
* Forbid wildcard imports

Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Make conventions plugin

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fix wildcard imports

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Allow overriding eclipse formatter config file

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

* Fix message formatting

Signed-off-by: Thomas Farr <tsfarr@amazon.com>

---------

Signed-off-by: Thomas Farr <tsfarr@amazon.com>
(cherry picked from commit e856ecb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants