Search in sources :

Example 6 with CatchTree

use of org.sonar.plugins.java.api.tree.CatchTree in project sonar-java by SonarSource.

the class CatchIllegalMonitorStateExceptionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    CatchTree catchTree = (CatchTree) tree;
    TypeTree parameterTypeTree = catchTree.parameter().type();
    if (parameterTypeTree.is(Kind.UNION_TYPE)) {
        UnionTypeTree unionTypeTree = (UnionTypeTree) parameterTypeTree;
        for (TypeTree exceptionTypeTree : unionTypeTree.typeAlternatives()) {
            checkExceptionType(exceptionTypeTree);
        }
    } else {
        checkExceptionType(parameterTypeTree);
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree)

Example 7 with CatchTree

use of org.sonar.plugins.java.api.tree.CatchTree in project sonar-java by SonarSource.

the class CatchNPECheck method visitCatch.

@Override
public void visitCatch(CatchTree tree) {
    super.visitCatch(tree);
    Tree typeTree = tree.parameter().type();
    if (typeTree.is(Kind.UNION_TYPE)) {
        ((UnionTypeTree) typeTree).typeAlternatives().forEach(this::checkType);
    } else {
        checkType(typeTree);
    }
}
Also used : UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 8 with CatchTree

use of org.sonar.plugins.java.api.tree.CatchTree in project sonar-java by SonarSource.

the class InstanceofUsedOnExceptionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.CATCH)) {
        CatchTree catchTree = (CatchTree) tree;
        caughtVariables.add(catchTree.parameter().simpleName().name());
    } else if (isLeftOperandAnException((InstanceOfTree) tree)) {
        reportIssue(((InstanceOfTree) tree).instanceofKeyword(), "Replace the usage of the \"instanceof\" operator by a catch block.");
    }
}
Also used : CatchTree(org.sonar.plugins.java.api.tree.CatchTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree)

Example 9 with CatchTree

use of org.sonar.plugins.java.api.tree.CatchTree in project sonar-java by SonarSource.

the class InstanceofUsedOnExceptionCheck method leaveNode.

@Override
public void leaveNode(Tree tree) {
    if (tree.is(Tree.Kind.CATCH)) {
        CatchTree catchTree = (CatchTree) tree;
        caughtVariables.remove(catchTree.parameter().simpleName().name());
    }
}
Also used : CatchTree(org.sonar.plugins.java.api.tree.CatchTree)

Aggregations

CatchTree (org.sonar.plugins.java.api.tree.CatchTree)9 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 UnionTypeTree (org.sonar.plugins.java.api.tree.UnionTypeTree)3 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 Test (org.junit.Test)1 Type (org.sonar.plugins.java.api.semantic.Type)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)1 InstanceOfTree (org.sonar.plugins.java.api.tree.InstanceOfTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)1 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1