Skip to content

Commit 16f79da

Browse files
authored
[openrewrite] add SanityCheck featuring CommonStaticAnalysis (#2651)
2 parents 66b34c7 + 188e683 commit 16f79da

File tree

166 files changed

+629
-610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+629
-610
lines changed

gradle/rewrite.gradle

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,10 @@
11
apply plugin: 'org.openrewrite.rewrite'
22

33
rewrite {
4-
activeRecipe(
5-
'org.openrewrite.gradle.GradleBestPractices',
6-
'org.openrewrite.java.RemoveUnusedImports',
7-
'org.openrewrite.java.format.RemoveTrailingWhitespace',
8-
'org.openrewrite.java.migrate.UpgradeToJava17',
9-
'org.openrewrite.java.recipes.JavaRecipeBestPractices',
10-
'org.openrewrite.java.recipes.RecipeTestingBestPractices',
11-
'org.openrewrite.java.security.JavaSecurityBestPractices',
12-
'org.openrewrite.staticanalysis.EqualsAvoidsNull',
13-
'org.openrewrite.staticanalysis.JavaApiBestPractices',
14-
'org.openrewrite.staticanalysis.LowercasePackage',
15-
'org.openrewrite.staticanalysis.MissingOverrideAnnotation',
16-
'org.openrewrite.staticanalysis.ModifierOrder',
17-
'org.openrewrite.staticanalysis.NoFinalizer',
18-
'org.openrewrite.staticanalysis.NoToStringOnStringType',
19-
'org.openrewrite.staticanalysis.NoValueOfOnStringType',
20-
'org.openrewrite.staticanalysis.RemoveUnusedLocalVariables',
21-
'org.openrewrite.staticanalysis.RemoveUnusedPrivateFields',
22-
'org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods',
23-
'org.openrewrite.staticanalysis.UnnecessaryCloseInTryWithResources',
24-
'org.openrewrite.staticanalysis.UnnecessaryExplicitTypeArguments',
25-
'org.openrewrite.staticanalysis.UnnecessaryParentheses',
26-
'org.openrewrite.staticanalysis.UnnecessaryReturnAsLastStatement',
27-
'tech.picnic.errorprone.refasterrules.BigDecimalRulesRecipes',
28-
'tech.picnic.errorprone.refasterrules.CharSequenceRulesRecipes',
29-
'tech.picnic.errorprone.refasterrules.ClassRulesRecipes',
30-
'tech.picnic.errorprone.refasterrules.CollectionRulesRecipes',
31-
'tech.picnic.errorprone.refasterrules.ComparatorRulesRecipes',
32-
'tech.picnic.errorprone.refasterrules.FileRulesRecipes',
33-
'tech.picnic.errorprone.refasterrules.MicrometerRulesRecipes',
34-
'tech.picnic.errorprone.refasterrules.PatternRulesRecipes',
35-
'tech.picnic.errorprone.refasterrules.PreconditionsRulesRecipes',
36-
'tech.picnic.errorprone.refasterrules.PrimitiveRulesRecipes',
37-
'tech.picnic.errorprone.refasterrules.StreamRulesRecipes',
38-
'tech.picnic.errorprone.refasterrules.TimeRulesRecipes'
39-
//'org.openrewrite.staticanalysis.CodeCleanup', bug
40-
//'org.openrewrite.staticanalysis.CommonStaticAnalysis', bug
41-
//'org.openrewrite.staticanalysis.UnnecessaryThrows', bug
42-
)
4+
activeRecipe('com.diffplug.spotless.openrewrite.SanityCheck')
435
exclusions.addAll(
6+
'**.dirty.java',
7+
'**FormatterProperties.java',
448
'**_gradle_node_plugin_example_**',
459
'**gradle/changelog.gradle',
4610
'**gradle/java-publish.gradle',
@@ -50,7 +14,9 @@ rewrite {
5014
'**lib/build.gradle',
5115
'**package-info.java',
5216
'**plugin-maven/build.gradle',
53-
'**settings.gradle'
17+
'**settings.gradle',
18+
'**special-tests.gradle',
19+
'**testlib/src/main/resources**'
5420
)
5521
exportDatatables = true
5622
failOnDryRunResults = true

lib-extra/src/groovy/java/com/diffplug/spotless/extra/glue/groovy/GrEclipseFormatterStepImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -130,15 +130,15 @@ public boolean errorsDetected() {
130130
synchronized (GroovyLogManager.manager) {
131131
GroovyLogManager.manager.removeLogger(this);
132132
}
133-
return 0 != errors.size();
133+
return !errors.isEmpty();
134134
}
135135

136136
@Override
137137
public String toString() {
138138
StringBuilder string = new StringBuilder();
139139
if (1 < errors.size()) {
140140
string.append("Multiple problems detected during step execution:");
141-
} else if (0 == errors.size()) {
141+
} else if (errors.isEmpty()) {
142142
string.append("Step sucesfully executed.");
143143
}
144144
for (Throwable error : errors) {

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/DefaultJavaElementComparator.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,26 @@ private int category(BodyDeclaration bodyDeclaration) {
164164
return CONSTRUCTORS_INDEX;
165165
}
166166
int flags = method.getModifiers();
167-
if (Modifier.isStatic(flags))
167+
if (Modifier.isStatic(flags)) {
168168
return STATIC_METHODS_INDEX;
169-
else
169+
} else {
170170
return METHOD_INDEX;
171+
}
171172
}
172173
case ASTNode.FIELD_DECLARATION: {
173-
if (JdtFlags.isStatic(bodyDeclaration))
174+
if (JdtFlags.isStatic(bodyDeclaration)) {
174175
return STATIC_FIELDS_INDEX;
175-
else
176+
} else {
176177
return FIELDS_INDEX;
178+
}
177179
}
178180
case ASTNode.INITIALIZER: {
179181
int flags = bodyDeclaration.getModifiers();
180-
if (Modifier.isStatic(flags))
182+
if (Modifier.isStatic(flags)) {
181183
return STATIC_INIT_INDEX;
182-
else
184+
} else {
183185
return INIT_INDEX;
186+
}
184187
}
185188
case ASTNode.TYPE_DECLARATION:
186189
case ASTNode.ENUM_DECLARATION:

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/EclipseJdtSortMembers.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 DiffPlug
2+
* Copyright 2024-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@
3333
import org.eclipse.jdt.core.dom.AST;
3434
import org.eclipse.jdt.internal.core.SortElementsOperation;
3535

36-
public class EclipseJdtSortMembers {
36+
public final class EclipseJdtSortMembers {
3737

3838
private static final Pattern PATTERN_DO_NOT_SORT_FIELDS = Pattern.compile("@SortMembers:doNotSortFields\\s*=\\s*(false|true)");
3939
private static final Pattern PATTERN_ENABLED = Pattern.compile("@SortMembers:enabled\\s*=\\s*(false|true)");
@@ -244,4 +244,6 @@ static SortProperties from(Map<String, String> properties) {
244244
return new SortProperties(enabled, membersOrder, doNotSortFields, sortByVisibility, visibilityOrder);
245245
}
246246
}
247+
248+
private EclipseJdtSortMembers() {}
247249
}

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/JdtFlags.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,26 @@
2828
/**
2929
* This class is derived and adapted code from the Eclipse JDT project (Derivative Works according to EPL 2.0 license).
3030
*/
31-
class JdtFlags {
31+
final class JdtFlags {
3232

3333
static final int VISIBILITY_CODE_INVALID = -1;
3434

3535
static boolean isStatic(BodyDeclaration bodyDeclaration) {
36-
if (isNestedInterfaceOrAnnotation(bodyDeclaration))
36+
if (isNestedInterfaceOrAnnotation(bodyDeclaration)) {
3737
return true;
38+
}
3839
int nodeType = bodyDeclaration.getNodeType();
3940
if (nodeType != ASTNode.METHOD_DECLARATION
4041
&& nodeType != ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION
41-
&& isInterfaceOrAnnotationMember(bodyDeclaration))
42+
&& isInterfaceOrAnnotationMember(bodyDeclaration)) {
4243
return true;
43-
if (bodyDeclaration instanceof EnumConstantDeclaration)
44+
}
45+
if (bodyDeclaration instanceof EnumConstantDeclaration) {
4446
return true;
45-
if (bodyDeclaration instanceof EnumDeclaration && bodyDeclaration.getParent() instanceof AbstractTypeDeclaration)
47+
}
48+
if (bodyDeclaration instanceof EnumDeclaration && bodyDeclaration.getParent() instanceof AbstractTypeDeclaration) {
4649
return true;
50+
}
4751
return Modifier.isStatic(bodyDeclaration.getModifiers());
4852
}
4953

@@ -60,8 +64,9 @@ private static boolean isProtected(BodyDeclaration bodyDeclaration) {
6064
}
6165

6266
private static boolean isPublic(BodyDeclaration bodyDeclaration) {
63-
if (isInterfaceOrAnnotationMember(bodyDeclaration))
67+
if (isInterfaceOrAnnotationMember(bodyDeclaration)) {
6468
return true;
69+
}
6570
return Modifier.isPublic(bodyDeclaration.getModifiers());
6671
}
6772

@@ -80,15 +85,18 @@ private static boolean isNestedInterfaceOrAnnotation(BodyDeclaration bodyDeclara
8085
}
8186

8287
static int getVisibilityCode(BodyDeclaration bodyDeclaration) {
83-
if (isPublic(bodyDeclaration))
88+
if (isPublic(bodyDeclaration)) {
8489
return Modifier.PUBLIC;
85-
else if (isProtected(bodyDeclaration))
90+
} else if (isProtected(bodyDeclaration)) {
8691
return Modifier.PROTECTED;
87-
else if (isPackageVisible(bodyDeclaration))
92+
} else if (isPackageVisible(bodyDeclaration)) {
8893
return Modifier.NONE;
89-
else if (isPrivate(bodyDeclaration))
94+
} else if (isPrivate(bodyDeclaration)) {
9095
return Modifier.PRIVATE;
96+
}
9197
Assert.isTrue(false);
9298
return VISIBILITY_CODE_INVALID;
9399
}
100+
101+
private JdtFlags() {}
94102
}

lib-extra/src/main/java/com/diffplug/spotless/extra/EclipseBasedStepBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Pr
7878

7979
/** Returns the FormatterStep (whose state will be calculated lazily). */
8080
public FormatterStep build() {
81-
var roundtrippableState = new EclipseStep(formatterVersion, formatterStepExt, FileSignature.promise(settingsFiles), JarState.promise(() -> {
82-
return JarState.withoutTransitives(dependencies, jarProvisioner);
83-
}));
81+
var roundtrippableState = new EclipseStep(formatterVersion, formatterStepExt, FileSignature.promise(settingsFiles), JarState.promise(() -> JarState.withoutTransitives(dependencies, jarProvisioner)));
8482
return FormatterStep.create(formatterName + formatterStepExt, roundtrippableState,
8583
EclipseStep::state, stateToFormatter);
8684
}

lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public abstract class EquoBasedStepBuilder {
6161
private File cacheDirectory;
6262

6363
/** Initialize valid default configuration, taking latest version */
64-
public EquoBasedStepBuilder(
64+
protected EquoBasedStepBuilder(
6565
String formatterName,
6666
Provisioner mavenProvisioner,
6767
@Nullable String defaultVersion,

lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ public String endingFor(File file) {
177177

178178
static class RuntimeInit {
179179
/** /etc/gitconfig (system-global), ~/.gitconfig (each might-not exist). */
180-
final FileBasedConfig systemConfig, userConfig;
180+
final FileBasedConfig systemConfig;
181+
final FileBasedConfig userConfig;
181182

182183
/** Repository specific config, can be $GIT_COMMON_DIR/config, project/.git/config or .git/worktrees/<id>/config.worktree if enabled by extension */
183184
final Config repoConfig;
184185

185186
/** Global .gitattributes file pointed at by systemConfig or userConfig, and the file in the repo. */
186-
final @Nullable File globalAttributesFile, repoAttributesFile;
187+
final @Nullable File globalAttributesFile;
188+
final @Nullable File repoAttributesFile;
187189

188190
/** git worktree root, might not exist if we're not in a git repo. */
189191
final @Nullable File workTree;
@@ -235,7 +237,7 @@ private Runtime atRuntime() {
235237
}
236238

237239
/** https://github.com/git/git/blob/1fe8f2cf461179c41f64efbd1dc0a9fb3b7a0fb1/Documentation/gitattributes.txt */
238-
static class Runtime {
240+
static final class Runtime {
239241
/** .git/info/attributes (and the worktree with that file) */
240242
final List<AttributesRule> infoRules;
241243

lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public boolean isClean(Project project, ObjectId treeSha, File file) throws IOEx
6262
return isClean(project, treeSha, relativePath);
6363
}
6464

65-
private Map<Repository, DirCache> dirCaches = new HashMap<>();
65+
private final Map<Repository, DirCache> dirCaches = new HashMap<>();
6666

6767
/**
6868
* This is the highest-level method, which all the others serve. Given the sha

lib-extra/src/main/java/com/diffplug/spotless/extra/GitWorkarounds.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 DiffPlug
2+
* Copyright 2020-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -202,7 +202,7 @@ private static IOException emptyFile(File commonDir) {
202202
return new IOException("Empty 'commondir' file: " + commonDir.getAbsolutePath());
203203
}
204204

205-
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
205+
@SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
206206
@Override
207207
public FileRepositoryBuilder readEnvironment(SystemReader sr) {
208208
super.readEnvironment(sr);

0 commit comments

Comments
 (0)