-
Notifications
You must be signed in to change notification settings - Fork 720
Expand file tree
/
Copy pathRedundantRecordMethodsCheckSample.java
More file actions
231 lines (194 loc) · 6.42 KB
/
RedundantRecordMethodsCheckSample.java
File metadata and controls
231 lines (194 loc) · 6.42 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
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() {
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;
}
}
}
}