Skip to content

Commit 231b67c

Browse files
committed
Fix spotbugs issues
1 parent 9ea2d8c commit 231b67c

File tree

3 files changed

+108
-76
lines changed

3 files changed

+108
-76
lines changed

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

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
/**
3838
* This class is derived and adapted code from the Eclipse JDT project (Derivative Works according to EPL 2.0 license).
3939
*/
40+
@SuppressFBWarnings(value = "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE", justification = "this comparator is not meant to be serialized")
4041
class DefaultJavaElementComparator implements Comparator<BodyDeclaration> {
4142

4243
static final int TYPE_INDEX = 0;
@@ -96,62 +97,60 @@ static DefaultJavaElementComparator of(
9697
this.visibilityOffsets = visibilityOffsets;
9798
}
9899

100+
@SuppressFBWarnings(value = "SF_SWITCH_NO_DEFAULT", justification = "we only accept valid tokens in the order string, otherwise we fall back to default value")
99101
static boolean fillVisibilityOffsets(String preferencesString, int[] offsets) {
100102
StringTokenizer tokenizer = new StringTokenizer(preferencesString, ",");
101103
int i = 0;
102104
while (tokenizer.hasMoreTokens()) {
103105
String token = tokenizer.nextToken();
104-
if (token != null) {
105-
switch (token) {
106-
case "B":
107-
offsets[PUBLIC_INDEX] = i++;
108-
break;
109-
case "D":
110-
offsets[DEFAULT_INDEX] = i++;
111-
break;
112-
case "R":
113-
offsets[PROTECTED_INDEX] = i++;
114-
break;
115-
case "V":
116-
offsets[PRIVATE_INDEX] = i++;
117-
}
106+
switch (token) {
107+
case "B":
108+
offsets[PUBLIC_INDEX] = i++;
109+
break;
110+
case "D":
111+
offsets[DEFAULT_INDEX] = i++;
112+
break;
113+
case "R":
114+
offsets[PROTECTED_INDEX] = i++;
115+
break;
116+
case "V":
117+
offsets[PRIVATE_INDEX] = i++;
118118
}
119119
}
120120
return i == N_VISIBILITIES;
121121
}
122122

123-
static boolean fillMemberCategoryOffsets(String preferencesString, int[] offsets) {
124-
StringTokenizer tokenizer = new StringTokenizer(preferencesString, ",");
123+
@SuppressFBWarnings(value = "SF_SWITCH_NO_DEFAULT", justification = "we only accept valid tokens in the order string, otherwise we fall back to default value")
124+
static boolean fillMemberCategoryOffsets(String orderString, int[] offsets) {
125+
StringTokenizer tokenizer = new StringTokenizer(orderString, ",");
125126
int i = 0;
126127
offsets[8] = i++;
127128
while (tokenizer.hasMoreTokens()) {
128129
String token = tokenizer.nextToken();
129-
if (token != null) {
130-
switch (token) {
131-
case "C":
132-
offsets[CONSTRUCTORS_INDEX] = i++;
133-
break;
134-
case "F":
135-
offsets[FIELDS_INDEX] = i++;
136-
break;
137-
case "I":
138-
offsets[INIT_INDEX] = i++;
139-
break;
140-
case "M":
141-
offsets[METHOD_INDEX] = i++;
142-
break;
143-
case "T":
144-
offsets[TYPE_INDEX] = i++;
145-
break;
146-
case "SF":
147-
offsets[STATIC_FIELDS_INDEX] = i++;
148-
break;
149-
case "SI":
150-
offsets[STATIC_INIT_INDEX] = i++;
151-
break;
152-
case "SM":
153-
offsets[STATIC_METHODS_INDEX] = i++;
154-
}
130+
switch (token) {
131+
case "C":
132+
offsets[CONSTRUCTORS_INDEX] = i++;
133+
break;
134+
case "F":
135+
offsets[FIELDS_INDEX] = i++;
136+
break;
137+
case "I":
138+
offsets[INIT_INDEX] = i++;
139+
break;
140+
case "M":
141+
offsets[METHOD_INDEX] = i++;
142+
break;
143+
case "T":
144+
offsets[TYPE_INDEX] = i++;
145+
break;
146+
case "SF":
147+
offsets[STATIC_FIELDS_INDEX] = i++;
148+
break;
149+
case "SI":
150+
offsets[STATIC_INIT_INDEX] = i++;
151+
break;
152+
case "SM":
153+
offsets[STATIC_METHODS_INDEX] = i++;
155154
}
156155
}
157156
return i == N_CATEGORIES;
@@ -219,6 +218,7 @@ private int getVisibilityIndex(int modifierFlags) {
219218
* @see CompilationUnitSorter#sort(int, org.eclipse.jdt.core.ICompilationUnit, int[], java.util.Comparator, int, org.eclipse.core.runtime.IProgressMonitor)
220219
*/
221220
@Override
221+
@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "when switching to a more recent Java version, we can avoid those unconfirmed casts")
222222
public int compare(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2) {
223223
boolean preserved1 = doNotSortFields && isSortPreserved(bodyDeclaration1);
224224
boolean preserved2 = doNotSortFields && isSortPreserved(bodyDeclaration2);

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

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,17 @@
2828
import org.eclipse.jdt.core.IOpenable;
2929
import org.eclipse.jdt.core.JavaModelException;
3030
import org.eclipse.jdt.core.dom.AST;
31-
import org.eclipse.jdt.internal.core.CompilationUnit;
32-
import org.eclipse.jdt.internal.core.JavaProject;
3331
import org.eclipse.jdt.internal.core.SortElementsOperation;
3432

3533
public class EclipseJdtSortMembers {
3634

37-
private static CompilationUnit compilationUnit(String code) {
38-
return new CompilationUnit(null, null, null) {
39-
private final Buffer buffer = new Buffer(code);
40-
41-
@Override
42-
public IBuffer getBuffer() {
43-
return buffer;
44-
}
45-
46-
@Override
47-
public JavaProject getJavaProject() {
48-
return new JavaProject(null, null) {
49-
@Override
50-
public Map<String, String> getOptions(boolean inheritJavaCoreOptions) {
51-
return Map.of();
52-
}
53-
};
54-
}
55-
56-
@Override
57-
public Map<String, String> getOptions(boolean inheritJavaCoreOptions) {
58-
return Map.of();
59-
}
60-
61-
@Override
62-
public ICompilationUnit getPrimary() {
63-
return this;
64-
}
65-
};
66-
}
67-
6835
static String sortMember(String code, SortProperties properties) {
6936
if (!properties.enabled) {
7037
return code;
7138
}
7239

7340
try {
74-
CompilationUnit compilationUnit = compilationUnit(code);
41+
CompilationUnit compilationUnit = new CompilationUnit(code);
7542
DefaultJavaElementComparator comparator = DefaultJavaElementComparator.of(
7643
properties.doNotSortFields,
7744
properties.membersOrder,
@@ -159,6 +126,45 @@ public void setContents(String contents) {
159126
}
160127
}
161128

129+
@SuppressFBWarnings(value = "EQ_DOESNT_OVERRIDE_EQUALS", justification = "the equals method shouldn't be called in the sort members use case")
130+
private static class CompilationUnit extends org.eclipse.jdt.internal.core.CompilationUnit {
131+
private final Buffer buffer;
132+
133+
CompilationUnit(String code) {
134+
super(null, null, null);
135+
buffer = new Buffer(code);
136+
}
137+
138+
@Override
139+
public IBuffer getBuffer() {
140+
return buffer;
141+
}
142+
143+
@Override
144+
public JavaProject getJavaProject() {
145+
return JavaProject.INSTANCE;
146+
}
147+
148+
@Override
149+
public Map<String, String> getOptions(boolean inheritJavaCoreOptions) {
150+
return Map.of();
151+
}
152+
153+
@Override
154+
public ICompilationUnit getPrimary() {
155+
return this;
156+
}
157+
}
158+
159+
private static class JavaProject extends org.eclipse.jdt.internal.core.JavaProject {
160+
static final JavaProject INSTANCE = new JavaProject();
161+
162+
@Override
163+
public Map<String, String> getOptions(boolean inheritJavaCoreOptions) {
164+
return Map.of();
165+
}
166+
}
167+
162168
private static class Sorter extends SortElementsOperation {
163169

164170
Sorter(int level, CompilationUnit compilationUnit, int[] positions, Comparator comparator) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra.glue.jdt;
17+
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.RetentionPolicy;
20+
21+
@Retention(RetentionPolicy.CLASS)
22+
@interface SuppressFBWarnings {
23+
String[] value() default {};
24+
25+
String justification() default "";
26+
}

0 commit comments

Comments
 (0)