/
CombineCatchCheck.java
147 lines (132 loc) · 5.68 KB
/
CombineCatchCheck.java
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
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.checks;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
import org.sonar.java.checks.helpers.QuickFixHelper;
import org.sonar.java.model.JavaTree.UnionTypeTreeImpl;
import org.sonar.java.model.LineUtils;
import org.sonar.java.model.SyntacticEquivalence;
import org.sonar.java.model.expression.MemberSelectExpressionTreeImpl;
import org.sonar.java.reporting.JavaQuickFix;
import org.sonar.java.reporting.JavaTextEdit;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.JavaVersion;
import org.sonar.plugins.java.api.tree.CatchTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TryStatementTree;
import org.sonar.plugins.java.api.tree.TypeTree;
@Rule(key = "S2147")
public class CombineCatchCheck extends IssuableSubscriptionVisitor implements JavaVersionAwareVisitor {
@Override
public List<Tree.Kind> nodesToVisit() {
return Collections.singletonList(Tree.Kind.TRY_STATEMENT);
}
@Override
public void visitNode(Tree tree) {
ArrayDeque<CatchTree> catches = new ArrayDeque<>();
for (CatchTree catchTree : ((TryStatementTree) tree).catches()) {
for (CatchTree catchTreeToBeCompared : catches) {
if (SyntacticEquivalence.areSemanticallyEquivalent(catchTree.block().body(), catchTreeToBeCompared.block().body())) {
reportIssueWithQuickFix(catchTree, catchTreeToBeCompared);
break;
}
}
catches.push(catchTree);
}
}
private void reportIssueWithQuickFix(CatchTree catchTree, CatchTree catchTreeToBeCompared) {
String quickFixMessage = "Combine this catch with the one at line " + LineUtils.startLine(catchTreeToBeCompared.catchKeyword());
String issueMessage = quickFixMessage + ", which has the same body." + context.getJavaVersion().java7CompatibilityMessage();
List<JavaFileScannerContext.Location> flow = Collections.singletonList(new JavaFileScannerContext.Location("Combine with this catch", catchTreeToBeCompared));
QuickFixHelper.newIssue(context)
.forRule(this)
.onTree(catchTree.parameter())
.withMessage(issueMessage)
.withSecondaries(flow)
.withQuickFix( () -> computeQuickFix(catchTree, catchTreeToBeCompared, quickFixMessage) )
.report();
}
@Override
public boolean isCompatibleWithJavaVersion(JavaVersion version) {
return version.isJava7Compatible();
}
private JavaQuickFix computeQuickFix(CatchTree catchTree, CatchTree catchTreeToBeCompared, String qfMessage) {
List<TypeTree> upperCatchTypes = getExceptionTypesCaught(catchTreeToBeCompared);
List<TypeTree> lowerCatchTypes = getExceptionTypesCaught(catchTree);
List<TypeTree> mergedTypes = mergeCatchTypes(upperCatchTypes, lowerCatchTypes);
var builder = JavaQuickFix.newQuickFix(qfMessage);
builder.addTextEdit(JavaTextEdit.removeTree(catchTree));
String replacement = computeReplacementString(mergedTypes, catchTreeToBeCompared);
builder.addTextEdit(
JavaTextEdit.replaceBetweenTree(
catchTreeToBeCompared.openParenToken(), false,
catchTreeToBeCompared.closeParenToken(), false,
replacement)
);
return builder.build();
}
private String computeReplacementString(Collection<TypeTree> types, CatchTree toReplaceCatch) {
StringBuilder sb = new StringBuilder();
types.forEach(type -> sb.append(formatType(type) + " | "));
sb.delete(sb.lastIndexOf("| "), sb.length());
sb.append(toReplaceCatch.parameter().simpleName().name());
return sb.toString();
}
private static List<TypeTree> mergeCatchTypes(List<TypeTree> upperCatchTypes, List<TypeTree> lowerCatchTypes) {
List<TypeTree> result = new ArrayList<>();
for (TypeTree upperType : upperCatchTypes) {
if (isNotMaskedBySuperType(upperType, lowerCatchTypes)) {
result.add(upperType);
}
}
result.addAll(lowerCatchTypes);
return result;
}
private static boolean isNotMaskedBySuperType(TypeTree type, List<TypeTree> types) {
for (TypeTree other : types) {
if (type.symbolType().isSubtypeOf(other.symbolType())) {
return false;
}
}
return true;
}
private String formatType(TypeTree type) {
if(type instanceof MemberSelectExpressionTreeImpl mtype) {
return QuickFixHelper.contentForTree(mtype, context);
}
return type.toString();
}
private static List<TypeTree> getExceptionTypesCaught(CatchTree catchTree){
TypeTree catchType = catchTree.parameter().type();
if(catchType instanceof UnionTypeTreeImpl unionTypes) {
unionTypes.symbolType();
return unionTypes.typeAlternatives();
}else {
return List.of(catchType);
}
}
}