-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathReplaceUnusedExceptionParameterWithUnnamedPatternCheckSample.java
More file actions
146 lines (118 loc) · 3.43 KB
/
Copy pathReplaceUnusedExceptionParameterWithUnnamedPatternCheckSample.java
File metadata and controls
146 lines (118 loc) · 3.43 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
package checks;
import io.restassured.exception.PathException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Supplier;
public class ReplaceUnusedExceptionParameterWithUnnamedPatternCheckSample {
public void simpleNonCompliant() {
List<String> elements = List.of();
int value = 0;
try {
var elem = elements.get(10);
value = Integer.parseInt(elem);
} catch (NumberFormatException nfe) { // Noncompliant {{Replace "nfe" with an unnamed pattern.}}
// ^^^
// fix@qf1 {{Replace "nfe" with "_"}}
// edit@qf1 [[sc=36;ec=39]] {{_}}
System.err.println("Wrong number format");
} catch (IndexOutOfBoundsException ioob) { // Noncompliant {{Replace "ioob" with an unnamed pattern.}}
// ^^^^
// fix@qf2 {{Replace "ioob" with "_"}}
// edit@qf2 [[sc=40;ec=44]] {{_}}
System.err.println("No such element");
}
}
public void simpleCompliant() {
List<String> elements = List.of();
int value = 0;
try {
var elem = elements.get(10);
value = Integer.parseInt(elem);
} catch (NumberFormatException _) { // compliant
System.err.println("Wrong number format");
} catch (IndexOutOfBoundsException _) { // compliant
System.err.println("No such element");
}
}
public void severalExceptionNonCompliant() {
try {
} catch (NumberFormatException | IndexOutOfBoundsException e) { // Noncompliant
} catch (PathException | ClassCastException e) { // Noncompliant
}
}
public void severalExceptionCompliant() {
try {
} catch (NumberFormatException | IndexOutOfBoundsException _) { // compliant
} catch ( PathException | ClassCastException _) { // compliant
}
}
public void nestedExceptionNonCompliant() {
try {
} catch (IndexOutOfBoundsException e) { // Noncompliant
try {
} catch (NumberFormatException k) { // Noncompliant
try {
} catch (PathException _) {
}
}
}
}
public void codeInsideCatchBlockNonCompliant() {
try {
} catch (Exception e) { // Noncompliant
int x = 0;
foo(new RuntimeException());
e();
}
}
public void useExceptionCompliant() {
try {
} catch (Exception e) {
var x = e;
}
try {
} catch (Exception e) {
foo(e);
}
try {
} catch (Exception e) {
throw e;
}
try {
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
} catch (Exception e) {
var s = e.getStackTrace();
}
try {
} catch (Exception e) {
Supplier<Integer> s =() -> {
var x = e;
return 0;
};
}
}
public void foo(Exception e) {
}
public void e(){}
private final Logger log = LogManager.getLogger();
private void executor_run_compliant() {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(
() -> {
try {
Integer.parseInt("1.2");
} catch (Exception e) {
log.error("SonarQube Cloud reports 'e' should be replaced by _ while it's used", e);
}
},
0,
100_000,
TimeUnit.MILLISECONDS
);
}
}