org.sonar.java.checks.InstanceofUsedOnExceptionCheck Maven / Gradle / Ivy
/*
* 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 Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* 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 Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.CatchTree;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.IfStatementTree;
import org.sonar.plugins.java.api.tree.InstanceOfTree;
import org.sonar.plugins.java.api.tree.StatementTree;
import org.sonar.plugins.java.api.tree.Tree;
@Rule(key = "S1193")
public class InstanceofUsedOnExceptionCheck extends IssuableSubscriptionVisitor {
@Override
public List nodesToVisit() {
return Collections.singletonList(Tree.Kind.CATCH);
}
@Override
public void visitNode(Tree tree) {
CatchTree catchTree = ((CatchTree) tree);
String caughtVariable = catchTree.parameter().simpleName().name();
List body = catchTree.block().body();
if (body.stream().allMatch(statement -> statement.is(Tree.Kind.RETURN_STATEMENT, Tree.Kind.THROW_STATEMENT, Tree.Kind.IF_STATEMENT))) {
reportSimpleInstanceOf(body, caughtVariable);
}
}
private void reportSimpleInstanceOf(List body, String caughtVariable) {
List conditions = body.stream()
.filter(statement -> statement.is(Tree.Kind.IF_STATEMENT))
.map(IfStatementTree.class::cast)
.flatMap(InstanceofUsedOnExceptionCheck::getFollowingElseIf)
.map(IfStatementTree::condition)
.toList();
if (conditions.stream().allMatch(cond -> cond.is(Tree.Kind.INSTANCE_OF) && isLeftOperandAndException((InstanceOfTree) cond, caughtVariable))) {
conditions.stream()
.map(InstanceOfTree.class::cast)
.forEach(instanceOfTree ->
reportIssue(instanceOfTree.instanceofKeyword(), "Replace the usage of the \"instanceof\" operator by a catch block."));
}
}
private static boolean isLeftOperandAndException(InstanceOfTree instanceOfTree, String caughtVariable) {
ExpressionTree expression = instanceOfTree.expression();
if (expression.is(Tree.Kind.IDENTIFIER)) {
return caughtVariable.equals(((IdentifierTree) expression).name())
&& instanceOfTree.type().symbolType().isSubtypeOf("java.lang.Throwable");
}
return false;
}
private static Stream getFollowingElseIf(IfStatementTree ifStatementTree) {
List ifStatements = new ArrayList<>();
ifStatements.add(ifStatementTree);
StatementTree elseStatement = ifStatementTree.elseStatement();
while (elseStatement != null) {
if (elseStatement.is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree elseIfStatement = (IfStatementTree) elseStatement;
ifStatements.add(elseIfStatement);
elseStatement = elseIfStatement.elseStatement();
} else {
elseStatement = null;
}
}
return ifStatements.stream();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy