Skip to content

Commit 98f2a4b

Browse files
authored
Add a replaceStaticImports step for Java (#3185)
2 parents e3350d7 + c94003c commit 98f2a4b

32 files changed

Lines changed: 4431 additions & 34 deletions

‎CHANGES.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
- Add a `groupArtifact` option to `PalantirJavaFormatStep`. ([#3179](https://github.com/diffplug/spotless/pull/3179))
15+
- Add `ReplaceStaticImportsStep`, which replaces static imports with regular imports and qualified references, such as `Assertions.assertTrue(x)`. Static imports to keep can be excluded. ([#1379](https://github.com/diffplug/spotless/issues/1379))
1516
### Fixed
1617
- GrEclipse no longer leaves a `file%3A...` directory in the working directory. ([#2019](https://github.com/diffplug/spotless/issues/2019))
1718
- DBeaver SQL formatter no longer prints an `IndexOutOfBoundsException` when a file starts with a keyword like `SET`. ([#1552](https://github.com/diffplug/spotless/issues/1552))

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ lib('java.ExpandWildcardImportsStep') +'{{yes}} | {{no}}
9090
lib('java.ForbidWildcardImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
9191
lib('java.ForbidModuleImportsStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
9292
lib('java.ShortenFullyQualifiedTypesStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
93+
lib('java.ReplaceStaticImportsStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
9394
extra('java.EclipseJdtFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
9495
lib('java.FormatAnnotationsStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
9596
lib('java.CleanthatJavaStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
@@ -151,6 +152,7 @@ lib('yaml.JacksonYamlStep') +'{{yes}} | {{yes}}
151152
| [`java.ForbidWildcardImportsStep`](lib/src/main/java/com/diffplug/spotless/java/ForbidWildcardImportsStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
152153
| [`java.ForbidModuleImportsStep`](lib/src/main/java/com/diffplug/spotless/java/ForbidModuleImportsStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
153154
| [`java.ShortenFullyQualifiedTypesStep`](lib/src/main/java/com/diffplug/spotless/java/ShortenFullyQualifiedTypesStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
155+
| [`java.ReplaceStaticImportsStep`](lib/src/main/java/com/diffplug/spotless/java/ReplaceStaticImportsStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
154156
| [`java.EclipseJdtFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
155157
| [`java.FormatAnnotationsStep`](lib/src/main/java/com/diffplug/spotless/java/FormatAnnotationsStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
156158
| [`java.CleanthatJavaStep`](lib/src/main/java/com/diffplug/spotless/java/CleanthatJavaStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |

‎lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@
3030
import java.util.TreeSet;
3131
import java.util.function.Function;
3232
import java.util.regex.Pattern;
33-
import javassist.ClassPool;
3433

3534
import com.github.javaparser.JavaParser;
3635
import com.github.javaparser.ParseResult;
37-
import com.github.javaparser.ParserConfiguration;
38-
import com.github.javaparser.ParserConfiguration.LanguageLevel;
3936
import com.github.javaparser.ast.CompilationUnit;
4037
import com.github.javaparser.ast.ImportDeclaration;
4138
import com.github.javaparser.ast.Node;
@@ -56,10 +53,6 @@
5653
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
5754
import com.github.javaparser.resolution.types.ResolvedType;
5855
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
59-
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
60-
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
61-
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
62-
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
6356

6457
import com.diffplug.spotless.FormatterFunc;
6558
import com.diffplug.spotless.LineEnding;
@@ -68,26 +61,10 @@
6861
public class ExpandWildcardsFormatterFunc implements FormatterFunc.NeedsFile {
6962

7063
private final JavaParser parser;
71-
static {
72-
// If ClassPool is allowed to cache class files, it does not free the file-lock
73-
ClassPool.cacheOpenedJarFile = false;
74-
}
7564

7665
public ExpandWildcardsFormatterFunc(Collection<File> typeSolverClasspath) throws IOException {
77-
// JavaParser defaults to Java 11 and rejects newer syntax such as records; align with JavaParserTypeSolver's default.
78-
this.parser = new JavaParser(new ParserConfiguration().setLanguageLevel(LanguageLevel.BLEEDING_EDGE));
79-
80-
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
81-
combinedTypeSolver.add(new ReflectionTypeSolver(ReflectionTypeSolver.JCL_ONLY));
82-
for (File element : typeSolverClasspath) {
83-
if (element.isFile() && element.getName().endsWith(".jar")) {
84-
combinedTypeSolver.add(new JarTypeSolver(element));
85-
} else if (element.isDirectory()) {
86-
combinedTypeSolver.add(new JavaParserTypeSolver(element));
87-
} // gracefully ignore non-existing src-directories
88-
}
89-
90-
SymbolResolver symbolSolver = new JavaSymbolSolver(combinedTypeSolver);
66+
this.parser = SymbolResolution.parser();
67+
SymbolResolver symbolSolver = new JavaSymbolSolver(SymbolResolution.typeSolver(typeSolverClasspath));
9168
parser.getParserConfiguration().setSymbolResolver(symbolSolver);
9269
}
9370

0 commit comments

Comments
 (0)