All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.java.checks.UnreachableCatchCheck 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.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.sonar.check.Rule;
import org.sonar.java.model.JProblem;
import org.sonar.java.model.JWarning;
import org.sonar.java.model.JavaTree.CompilationUnitTreeImpl;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.JavaFileScannerContext.Location;
import org.sonar.plugins.java.api.location.Position;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.CatchTree;
import org.sonar.plugins.java.api.tree.SyntaxToken;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TryStatementTree;
import org.sonar.plugins.java.api.tree.TypeTree;
import org.sonar.plugins.java.api.tree.UnionTypeTree;

@Rule(key = "S4970")
public class UnreachableCatchCheck extends IssuableSubscriptionVisitor {

  private final List warnings = new ArrayList<>();
  private static final Comparator LOCATION_COMPARATOR = Comparator.comparing(loc -> Position.startOf(loc.syntaxNode));

  @Override
  public List nodesToVisit() {
    return Arrays.asList(Tree.Kind.COMPILATION_UNIT, Tree.Kind.TRY_STATEMENT);
  }

  @Override
  public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.COMPILATION_UNIT)) {
      warnings.clear();
      warnings.addAll(((CompilationUnitTreeImpl) tree).warnings(JProblem.Type.MASKED_CATCH));
      return;
    }
    checkWarnings((TryStatementTree) tree);
  }

  private void checkWarnings(TryStatementTree tryStatementTree) {
    List typesWithWarnings = new ArrayList<>(warnings.size());
    List unionTypes = new ArrayList<>();
    Map typeByExceptions = new HashMap<>();
    Map reportTrees = new HashMap<>();

    for (CatchTree catchTree : tryStatementTree.catches()) {
      SyntaxToken catchKeyword = catchTree.catchKeyword();
      TypeTree caughtException = catchTree.parameter().type();

      boolean withinUnionType = caughtException.is(Tree.Kind.UNION_TYPE);
      if (withinUnionType) {
        // in case we need to report on the entire union type
        reportTrees.put(caughtException, catchKeyword);
        unionTypes.add((UnionTypeTree) caughtException);
      }

      for (TypeTree typeTree : caughtExceptionTypes(caughtException)) {
        typeByExceptions.put(typeTree, typeTree.symbolType());
        // types from union types should be reported individually
        reportTrees.put(typeTree, withinUnionType ? typeTree : catchKeyword);

        warnings.stream()
          .filter(warning -> typeTree.equals(warning.syntaxTree()))
          .forEach(warning -> typesWithWarnings.add(typeTree));
      }
    }
    reportUnreacheableCatch(typesWithWarnings, typeByExceptions, reportTrees, unionTypes);
  }

  private static List caughtExceptionTypes(TypeTree caughtException) {
    if (caughtException.is(Tree.Kind.UNION_TYPE)) {
      return ((UnionTypeTree) caughtException).typeAlternatives();
    }
    return Collections.singletonList(caughtException);
  }

  private void reportUnreacheableCatch(List typesWithWarnings, Map typeByExceptions, Map reportTrees, List unionTypes) {
    for (TypeTree type : typesToReport(typesWithWarnings, unionTypes)) {
      reportIssue(reportTrees.get(type), message(reportTrees.get(type)), secondaries(type, typeByExceptions), null);
    }
  }

  private static List typesToReport(List typesWithWarnings, List unionTypes) {
    List typesToReport = new ArrayList<>(typesWithWarnings);
    for (UnionTypeTree unionType : unionTypes) {
      List typeAlternatives = unionType.typeAlternatives();
      if (typesWithWarnings.containsAll(typeAlternatives)) {
        typesToReport.add(unionType);
        typesToReport.removeAll(typeAlternatives);
      }
    }
    return typesToReport;
  }

  private static String message(Tree reportTree) {
    if (reportTree.is(Tree.Kind.TOKEN)) {
      return "Remove or refactor this catch clause because it is unreachable, hidden by previous catch block(s).";
    }
    // reporting on the type alternative of an union type
    return "Remove this type because it is unreachable, hidden by previous catch block(s).";
  }

  private static List secondaries(TypeTree type, Map typeByExceptions) {
    List targets;
    if (type.is(Tree.Kind.UNION_TYPE)) {
      targets = ((UnionTypeTree) type).typeAlternatives()
        .stream()
        .map(typeByExceptions::get)
        .toList();
    } else {
      targets = Collections.singletonList(typeByExceptions.get(type));
    }
    return childrenExceptionTypes(targets, typeByExceptions);
  }

  private static List childrenExceptionTypes(List targets, Map exceptionTypes) {
    List secondaries = new ArrayList<>();
    targets.forEach(target -> {
      for (Map.Entry exceptionType : exceptionTypes.entrySet()) {
        TypeTree tree = exceptionType.getKey();
        Type type = exceptionType.getValue();
        if (!type.equals(target) && type.isSubtypeOf(target)) {
          secondaries.add(new Location("Already catch the exception", tree));
        }
      }
    });
    secondaries.sort(LOCATION_COMPARATOR);
    return secondaries;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy