Search in sources :

Example 1 with InstanceOfTree

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

the class InstanceOfAlwaysTrueCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    InstanceOfTree instanceOfTree = (InstanceOfTree) tree;
    Type expressionType = instanceOfTree.expression().symbolType();
    Type instanceOf = instanceOfTree.type().symbolType();
    if (expressionType.isSubtypeOf(instanceOf) && !instanceOfTree.expression().is(Tree.Kind.NULL_LITERAL)) {
        reportIssue(instanceOfTree.instanceofKeyword(), "Remove this useless \"instanceof\" operator; it will always return \"true\". ");
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree)

Example 2 with InstanceOfTree

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

the class JavaTreeModelTest method instanceof_expression.

/**
 * 15.20.2. Type Comparison Operator instanceof
 */
@Test
public void instanceof_expression() {
    InstanceOfTree tree = (InstanceOfTree) expressionOfReturnStatement("class T { boolean m() { return null instanceof Object; } }");
    assertThat(tree.is(Tree.Kind.INSTANCE_OF)).isTrue();
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.instanceofKeyword().text()).isEqualTo("instanceof");
    assertThat(tree.type()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) Test(org.junit.Test)

Example 3 with InstanceOfTree

use of org.sonar.plugins.java.api.tree.InstanceOfTree 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)

Aggregations

InstanceOfTree (org.sonar.plugins.java.api.tree.InstanceOfTree)3 Test (org.junit.Test)1 Type (org.sonar.plugins.java.api.semantic.Type)1 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)1