-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathRedundantRecordMethodsCheckSample.java
More file actions
319 lines (267 loc) · 8.56 KB
/
RedundantRecordMethodsCheckSample.java
File metadata and controls
319 lines (267 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package checks;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Locale;
import java.util.Random;
public class RedundantRecordMethodsCheckSample {
record RedundantConstructorAndGetters(String name, int age) {
static Object variable = null;
static Object someOtherVariable = null;
RedundantConstructorAndGetters(String name, int age) { // Noncompliant {{Remove this redundant constructor which is the same as a default one.}}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this.name = name;
this.age = age;
}
public String name() { // Noncompliant {{Remove this redundant method which is the same as a default one.}}
// ^^^^
return this.name;
}
public int age() { // Noncompliant {{Remove this redundant method which is the same as a default one.}}
// ^^^
return age;
}
}
record ConstructorAssignsWithRedundantCast(String name, int age) {
ConstructorAssignsWithRedundantCast(String name, int age) { // Compliant FN as the unnecessary cast should be flagged by other rules
this.name = (String) name;
this.age = age;
}
public String name() { // Noncompliant {{Remove this redundant method which is the same as a default one.}}
// ^^^^
return this.name;
}
public int age() { // Noncompliant {{Remove this redundant method which is the same as a default one.}}
// ^^^
return age;
}
}
record ConstructorWithSideEffects(String name, int age) {
ConstructorWithSideEffects(String name, int age) { // Noncompliant {{Consider using a compact constructor here.}}
sideEffect();
this.name = name;
this.age = age;
}
public void sideEffect() {
System.out.println("Here's a side effect!");
}
}
record ThrowingConstructor(String name, int age) {
ThrowingConstructor(String name, int age) { // Noncompliant {{Consider using a compact constructor here.}}
if (age < 0) {
throw new IllegalArgumentException("Negative age");
}
this.name = name;
this.age = age;
}
}
record ParameterMismatch(String name, String address) {
ParameterMismatch(String name, String address) { // Compliant
this.name = address;
this.address = name;
}
}
record CompliantConstructorIgnoringParameter(String name, int age) {
CompliantConstructorIgnoringParameter(String name, int age) { // Compliant
this.name = name;
this.age = 42;
}
}
record CompliantConstructorNotAssigningToComponent(String name, int age) {
CompliantConstructorNotAssigningToComponent(String name, int age) { // Compliant
this.name = "A";
this.age = age;
}
CompliantConstructorNotAssigningToComponent(int age) {
this("ignored", 42);
}
}
record ConstructorAssignsStaticValue(String name, int age) {
static final String THE_ONLY_ACCEPTABLE_NAME = "A";
ConstructorAssignsStaticValue(String name, int age) { // Compliant
this.name = THE_ONLY_ACCEPTABLE_NAME;
this.age = 42;
}
}
record EmptyConstructorAndRedundantGetter(String name, int age) {
EmptyConstructorAndRedundantGetter { // Noncompliant {{Remove this useless empty compact constructor.}}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
public String name() { // Noncompliant {{Remove this redundant method which is the same as a default one.}}
// ^^^^
return name;
}
}
record Compliant(String name, int age) { // Compliant
}
record CompliantConstructorWithAddedValue(String name, int age) {
CompliantConstructorWithAddedValue(String name, int age) { // Compliant
this.name = name.toLowerCase(Locale.ROOT);
this.age = age;
}
}
record CompliantConstructorWithSideEffect(String name, int age) {
CompliantConstructorWithSideEffect(String name, int age) { // Compliant: the constructor includes side effects after assignments
if (age < 0) {
throw new IllegalArgumentException("Negative age");
}
this.name = name.toLowerCase(Locale.ROOT);
this.age = age;
System.out.println("Hello");
}
}
record CompliantConstructorComplementAndTransformativeGetter(String name, int age) {
CompliantConstructorComplementAndTransformativeGetter { // Compliant
if (age < 0) {
throw new IllegalArgumentException("Negative age");
}
}
public String name() { // Compliant
return name.toUpperCase(Locale.ROOT);
}
}
record MisleadingGetters(String name, int age) {
static final String MESSAGE = "Hello";
public String name() {// Compliant
return MESSAGE;
}
public int age() {// Compliant
return 42;
}
}
record PoorlyNamedGetter(String name, int age) {
public String something() { // Compliant
return name;
}
}
record GetterWithBranches(String name, int age) {
public String name() { // Noncompliant {{Remove this redundant method which is the same as a default one.}}
// ^^^^
if ((new Random()).nextBoolean()) {
return this.name;
} else {
return this.name;
}
}
}
record RecordWithConstructorAnnotation(String value) {
@JsonCreator
RecordWithConstructorAnnotation(@JsonProperty("myName") String value) { // Compliant: annotated
this.value = value;
}
}
record RecordWithParamAnnotation(String value) {
// The annotation can be applied to the component and the constructor can be removed.
RecordWithParamAnnotation(@JsonProperty("myName") String value) { // Noncompliant
this.value = value;
}
}
@interface MyAnnotation {}
record RecordWithCustomAnnotation(String value) {
@MyAnnotation
RecordWithCustomAnnotation(String value) { // Compliant: annotated
this.value = value;
}
}
record AssignmentInConstructor(int arg1, int arg2) {
public AssignmentInConstructor(int arg1, int arg2) { // Compliant
this.arg1 = arg1;
if (arg2 == 5) {
arg2 = 4;
}
this.arg2 = arg2;
}
}
record BranchInConstructor(int arg1, int arg2) {
public BranchInConstructor(int arg1, int arg2) { // Noncompliant
this.arg1 = arg1;
if (arg2 == 5) {
this.arg2 = arg2;
} else {
arg1 = 0;
this.arg2 = arg2;
}
}
}
record BranchInConstructor2(int arg1, int arg2) {
public BranchInConstructor2(int arg1, int arg2) { // Compliant
this.arg1 = arg1;
if (arg2 == 5) {
this.arg2 = arg2;
} else {
this.arg2 = 0;
}
}
}
record AccessorWithLoop(String name) {
public String name() { // Noncompliant {{Remove this redundant method which is the same as a default one.}}
// ^^^^
while (true) {
return name;
}
}
}
interface NamedThing {
String name();
}
record AnnotatedAccessor(String name) implements NamedThing {
@Override
public String name() { // Compliant, no issues are raised for annotated accessors
return name;
}
}
record DifferentlyNamedAccessor(String name) {
public String getName() { // Compliant, no issues are raised for accessors with different names
return name;
}
}
record AccessorSettingField(String name) {
private static int AGE;
public String name() {
AGE = 0;
return name;
}
}
record AccessorWithParameter(String name) {
public String name(int x) { // Compliant, no issues are raised for accessors with parameters
return name;
}
}
record AccessorWithLogic(String name) {
public String name() { // Compliant, no issues are raised for accessors with logic
System.out.println("Side effects !");
return name;
}
}
record AccessorWithLogic2(String name) {
public String name() {
int x = 42;
System.out.println(x);
return name;
}
}
record AccessorWithAssertion(String name) {
public String name() { // Compliant, no issues are raised for accessors with assertions
assert name != null;
return name;
}
}
record AccessorWithThrow(int age) {
public int age() { // Compliant, no issues are raised for accessors that throw exceptions
if (age < 0) {
throw new IllegalStateException("Negative age");
}
return age;
}
}
record AccessorWithValidation(int age) {
public int age() { // Compliant, no issues are raised for accessors that validate input
validate(age);
return age;
}
void validate(int age) {
if (this.age < 0) {
throw new IllegalStateException("Negative age");
}
}
}
}