Skip to content

removeWildcardImports: throw new AssertionError instead of removing #2557

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,8 @@
*/
package com.diffplug.spotless.generic;

import static com.diffplug.spotless.Lint.atUndefinedLine;

import java.io.Serializable;
import java.util.Objects;
import java.util.regex.Pattern;
Expand All @@ -35,6 +37,15 @@ public static FormatterStep create(String name, String regex, String replacement
State::toFormatter);
}

public static FormatterStep lint(String name, String regex, String error) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(regex, "regex");
Objects.requireNonNull(error, "error");
return FormatterStep.createLazy(name,
() -> new State(Pattern.compile(regex, Pattern.UNIX_LINES | Pattern.MULTILINE), error),
State::toLinter);
}

private static final class State implements Serializable {
private static final long serialVersionUID = 1L;

Expand All @@ -49,5 +60,14 @@ private static final class State implements Serializable {
FormatterFunc toFormatter() {
return raw -> regex.matcher(raw).replaceAll(replacement);
}

FormatterFunc toLinter() {
return raw -> {
if (regex.matcher(raw).find()) {
throw atUndefinedLine("", replacement).shortcut();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would like to determine the line. But adding with line number 444 it was not mentioned in the error log. So i dont know how to test it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Looks like ReplaceRegexStep is missing its step-level test. It should have one called ReplaceRegexStepTest which should be a peer of this
  • We use selfie to do snapshot tests of the lint results, here is an example how that looks
  • void works0_49_0() {
    FormatterStep step = KtLintStep.create("0.49.0", TestProvisioner.mavenCentral());
    StepHarnessWithFile.forStep(this, step)
    .testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic-old.clean")
    .expectLintsOfResource("kotlin/ktlint/unsolvable.dirty").toBe("L1 ktlint(standard:no-wildcard-imports) Wildcard import");
    }
  • don't write out what's inside of toBe, just put toBe_TODO() and then run the test, it will write the string literal for you
  • KtLint needs the path to work, thus StepHarnessWithFile, you can use just StepHarness which is easier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not understand the code, but the output does not show the line number given.

Expecting actual:
  "[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Spotless Maven Plugin Tests 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) @ spotless-maven-plugin-tests ---
[INFO] Index file does not exist. Fallback to an empty index
[ERROR] Step 'removeWildcardImports' found problem in 'test.java':
Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
com.diffplug.spotless.Lint$ShortcutException: Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
	at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
	at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
	at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
	at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
	at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
	at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
	at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
	at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
	at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.440 s
[INFO] Finished at: 2025-07-18T21:55:53+02:00
[INFO] Final Memory: 11M/40M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java: Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
	at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
	at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
Caused by: org.apache.maven.plugin.MojoExecutionException: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
	at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:73)
	at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
	... 22 more
Caused by: com.diffplug.spotless.Lint$ShortcutException: Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
	at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
	at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
	at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
	at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
	at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
	at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
	... 25 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
"
to contain:
  "11111" 
java.lang.AssertionError: 
Expecting actual:
  "[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Spotless Maven Plugin Tests 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) @ spotless-maven-plugin-tests ---
[INFO] Index file does not exist. Fallback to an empty index
[ERROR] Step 'removeWildcardImports' found problem in 'test.java':
Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
com.diffplug.spotless.Lint$ShortcutException: Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
	at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
	at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
	at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
	at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
	at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
	at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
	at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
	at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
	at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.440 s
[INFO] Finished at: 2025-07-18T21:55:53+02:00
[INFO] Final Memory: 11M/40M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java: Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
	at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
	at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
Caused by: org.apache.maven.plugin.MojoExecutionException: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
	at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:73)
	at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
	... 22 more
Caused by: com.diffplug.spotless.Lint$ShortcutException: Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this
	at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
	at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
	at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
	at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
	at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
	at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
	at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
	... 25 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
"
to contain:
  "11111" 
	at com.diffplug.spotless.maven.java.RemoveWildcardImportsStepTest.testRemoveWildcardImports(RemoveWildcardImportsStepTest.java:48)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

}
return raw;
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@

/** Removes any wildcard import statements. */
public final class RemoveWildcardImportsStep {

/**
* Matches lines like 'import foo.*;' or 'import static foo.*;'.
*/
private static final String REGEX = "(?m)^import\\s+(?:static\\s+)?[^;\\n]*\\*;\\R?";
private static final String NAME = "removeWildcardImports";
private static final String ERROR = "Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.";

private RemoveWildcardImportsStep() {}

public static FormatterStep create() {
// Matches lines like 'import foo.*;' or 'import static foo.*;'.
return ReplaceRegexStep.create(
"removeWildcardImports",
"(?m)^import\\s+(?:static\\s+)?[^;\\n]*\\*;\\R?",
"");
return ReplaceRegexStep.lint(NAME, REGEX, ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import com.diffplug.spotless.ResourceHarness;

public class MavenIntegrationHarness extends ResourceHarness {

protected static final String PATH = "src/main/java/test.java";
/**
* To run tests in the IDE, run {@code gradlew :plugin-maven:changelogPrint}, then
* put the last version it prints into {@code SPOTLESS_MAVEN_VERSION_IDE}. From now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,40 @@
*/
package com.diffplug.spotless.maven.java;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

class RemoveWildcardImportsStepTest extends MavenIntegrationHarness {

private static final String ERROR = "Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.";

@BeforeEach
void init() throws Exception {
writePomWithJavaSteps("<removeWildcardImports/>");
}

@Test
void testRemoveWildcardImports() throws Exception {
writePomWithJavaSteps("<removeWildcardImports/>");
setFile(PATH).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
assertFile(PATH).sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
assertThat(mavenRunner().withArguments("spotless:apply").runHasError().stdOutUtf8()).contains(ERROR);
}

String path = "src/main/java/test.java";
setFile(path).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
@Test
void testRemoveStaticWildcardImports() throws Exception {
setFile(PATH).toResource("java/removewildcardimports/JavaCodeStaticWildcardsUnformatted.test");
assertFile(PATH).sameAsResource("java/removewildcardimports/JavaCodeStaticWildcardsFormatted.test");
assertThat(mavenRunner().withArguments("spotless:apply").runHasError().stdOutUtf8()).contains(ERROR);
}

@Test
void testRemoveWildcardImportsNoError() throws Exception {
setFile(PATH).toResource("java/removewildcardimports/JavaCodeNoWildcardsUnformatted.test");
assertFile(PATH).sameAsResource("java/removewildcardimports/JavaCodeNoWildcardsUnformatted.test");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile(path).sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import java.util.List;
import mylib.Helper;

public class Test {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import java.util.List;
import mylib.Helper;

public class Test {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.util.List;
import mylib.Helper;
import static io.quarkus.vertx.web.Route.HttpMethod.*;
import static org.springframework.web.reactive.function.BodyInserters.*;

public class Test {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.util.List;
import mylib.Helper;
import static io.quarkus.vertx.web.Route.HttpMethod.*;
import static org.springframework.web.reactive.function.BodyInserters.*;

public class Test {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import java.util.*;
import static java.util.Collections.*;
import java.util.List;
import mylib.Helper;
import io.quarkus.maven.dependency.*;

public class Test {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ import static java.util.Collections.*;
import java.util.List;
import mylib.Helper;
import io.quarkus.maven.dependency.*;
import static io.quarkus.vertx.web.Route.HttpMethod.*;
import static org.springframework.web.reactive.function.BodyInserters.*;

public class Test {}
Loading