Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

### Fixed
- `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997))

## [4.11.0] - 2026-11-02
### Added
- Add a `cleanup` option to `GoogleJavaFormatStep` to disable google-java-format's non-whitespace cleanups. ([#3157](https://github.com/diffplug/spotless/pull/3157))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package com.diffplug.spotless.java;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.generic.ReplaceRegexStep;
import com.diffplug.spotless.Lint;

/** Forbids any wildcard import statements. */
public final class ForbidWildcardImportsStep {
Expand All @@ -31,6 +37,137 @@ public final class ForbidWildcardImportsStep {
private ForbidWildcardImportsStep() {}

public static FormatterStep create() {
return ReplaceRegexStep.lint(NAME, REGEX, ERROR);
return FormatterStep.create(NAME, Pattern.compile(REGEX, Pattern.UNIX_LINES | Pattern.MULTILINE), ForbidWildcardImportsStep::toFormatter);
}

private static FormatterFunc toFormatter(Pattern regex) {
return new FormatterFunc() {
@Override
public String apply(String raw) {
return raw;
}

@Override
public List<Lint> lint(String raw, File file) {
var matcher = regex.matcher(raw);
if (!matcher.find()) {
return List.of();
}
List<Lint> lints = new ArrayList<>();
LexicalInput input = new LexicalInput(raw);
int scanOffset = 0;
int lineOffset = 0;
int line = 1;
do {
// A skipped literal or comment may extend past this apparent import.
while (input.rawOffset(scanOffset) < matcher.start()) {
scanOffset = skipLiteralOrComment(input.text, scanOffset);
}
if (input.rawOffset(scanOffset) > matcher.start()) {
continue;
}
while (lineOffset < matcher.start()) {
if (raw.charAt(lineOffset++) == '\n') {
++line;
}
}
String errorCode = matcher.group().trim();
int firstNewline = errorCode.indexOf('\n');
if (firstNewline != -1) {
errorCode = errorCode.substring(0, firstNewline);
}
lints.add(Lint.atLine(line, errorCode, ERROR));
} while (matcher.find());
return lints;
}
};
}

/** Skips a complete literal or comment, or advances one character through ordinary code. */
private static int skipLiteralOrComment(String raw, int offset) {
if (raw.startsWith("//", offset)) {
int end = raw.indexOf('\n', offset + 2);
return end == -1 ? raw.length() : end;
}
if (raw.startsWith("/*", offset)) {
int end = raw.indexOf("*/", offset + 2);
return end == -1 ? raw.length() : end + 2;
}
char c = raw.charAt(offset);
if (c == '"' || c == '\'') {
String delimiter = raw.startsWith("\"\"\"", offset) ? "\"\"\"" : String.valueOf(c);
int end = offset + delimiter.length();
while (end < raw.length()) {
if (raw.charAt(end) == '\\') {
// An escaped quote cannot close a literal or text block.
end += 2;
} else if (raw.startsWith(delimiter, end)) {
return end + delimiter.length();
} else {
++end;
}
}
return raw.length();
}
return offset + 1;
}

/** Translates Unicode escapes for lexing, while retaining raw offsets for diagnostics. */
private static final class LexicalInput {
private final String text;
private final int[] rawOffsets;

LexicalInput(String raw) {
if (!raw.contains("\\u")) {
text = raw;
rawOffsets = new int[0];
return;
}
StringBuilder translated = new StringBuilder(raw.length());
rawOffsets = new int[raw.length() + 1];
int backslashes = 0;
boolean previousWasEscape = false;
for (int offset = 0; offset < raw.length();) {
char c = raw.charAt(offset);
int next = offset + 1;
boolean isEscape = false;
// JLS 3.3: eligibility depends on the translated backslashes, not just the raw input.
if (c == '\\' && (previousWasEscape || backslashes % 2 == 0)) {
int digits = next;
while (digits < raw.length() && raw.charAt(digits) == 'u') {
++digits;
}
if (digits > next && digits + 4 <= raw.length()) {
int value = 0;
for (int i = digits; i < digits + 4; ++i) {
char digit = raw.charAt(i);
int hex = Character.digit(digit, 16);
if (digit > 127 || hex == -1) {
value = -1;
break;
}
value = value * 16 + hex;
}
if (value != -1) {
c = (char) value;
next = digits + 4;
isEscape = true;
}
}
}
rawOffsets[translated.length()] = offset;
// Unicode escapes can introduce either kind of Java line terminator.
translated.append(c == '\r' ? '\n' : c);
backslashes = c == '\\' ? backslashes + 1 : 0;
previousWasEscape = isEscape;
offset = next;
}
rawOffsets[translated.length()] = raw.length();
text = translated.toString();
}

int rawOffset(int offset) {
return rawOffsets.length == 0 ? offset : rawOffsets[offset];
}
}
}
3 changes: 3 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

### Fixed
- `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997))

## [8.11.0] - 2026-11-02
### Added
- Add `cleanup(false)` to `googleJavaFormat()` to disable google-java-format's non-whitespace cleanups. ([#3157](https://github.com/diffplug/spotless/pull/3157))
Expand Down
3 changes: 3 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

### Fixed
- `forbidWildcardImports` no longer reports import statements inside Java text blocks or comments. ([#2997](https://github.com/diffplug/spotless/issues/2997))

## [3.11.0] - 2026-11-02
### Added
- Add `<cleanup>false</cleanup>` to `<googleJavaFormat>` to disable google-java-format's non-whitespace cleanups. ([#3157](https://github.com/diffplug/spotless/pull/3157))
Expand Down
Loading
Loading