Search in sources :

Example 56 with MemberSelectExpressionTree

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

the class ImmediateReverseBoxingCheck method visitMethodInvocationTree.

private void visitMethodInvocationTree(MethodInvocationTree mit) {
    ExpressionTree methodSelect = mit.methodSelect();
    if (isValueOfInvocation(mit)) {
        ExpressionTree arg0 = mit.arguments().get(0);
        checkForUnboxing(arg0);
        checkForUselessUnboxing(mit.symbolType(), methodSelect, arg0);
    } else if (isUnboxingMethodInvocation(mit)) {
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            checkForBoxing(((MemberSelectExpressionTree) methodSelect).expression());
        }
    } else {
        Symbol symbol = mit.symbol();
        if (symbol.isMethodSymbol()) {
            checkMethodInvocationArguments(mit, ((Symbol.MethodSymbol) symbol).parameterTypes());
        }
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 57 with MemberSelectExpressionTree

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

the class ErrorClassExtendedCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    TypeTree superClass = tree.superClass();
    if (tree.is(Tree.Kind.CLASS) && superClass != null) {
        if (superClass.is(Tree.Kind.IDENTIFIER)) {
            IdentifierTree idt = (IdentifierTree) superClass;
            if ("Error".equals(idt.name())) {
                context.reportIssue(this, superClass, "Extend \"java.lang.Exception\" or one of its subclasses.");
            }
        } else if (superClass.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) superClass;
            if ("Error".equals(mse.identifier().name()) && isJavaLang(mse.expression())) {
                context.reportIssue(this, superClass, "Extend \"java.lang.Exception\" or one of its subclasses.");
            }
        }
    }
    super.visitClass(tree);
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 58 with MemberSelectExpressionTree

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

the class WaitInSynchronizeCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    if (!isInSyncBlock()) {
        IdentifierTree methodName = ExpressionUtils.methodName(mit);
        ExpressionTree methodSelect = mit.methodSelect();
        String lockName;
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            lockName = ((MemberSelectExpressionTree) methodSelect).expression().symbolType().name();
        } else {
            lockName = "this";
        }
        reportIssue(methodName, "Move this call to \"" + methodName + "()\" into a synchronized block to be sure the monitor on \"" + lockName + "\" is held.");
    }
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 59 with MemberSelectExpressionTree

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

the class ExpressionsHelper method concatenate.

public static String concatenate(@Nullable ExpressionTree tree) {
    if (tree == null) {
        return "";
    }
    Deque<String> pieces = new LinkedList<>();
    ExpressionTree expr = tree;
    while (expr.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
        pieces.push(mse.identifier().name());
        pieces.push(".");
        expr = mse.expression();
    }
    if (expr.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree idt = (IdentifierTree) expr;
        pieces.push(idt.name());
    }
    StringBuilder sb = new StringBuilder();
    for (String piece : pieces) {
        sb.append(piece);
    }
    return sb.toString();
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LinkedList(java.util.LinkedList)

Example 60 with MemberSelectExpressionTree

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

the class SyntaxTreeNameFinderTest method testClassCast.

@Test
public void testClassCast() {
    MethodTree tree = buildSyntaxTree("public boolean equals(Object obj) {((String) obj).length;}");
    BlockTree block = tree.block();
    StatementTree statementTree = block.body().get(0);
    MemberSelectExpressionTree mse = (MemberSelectExpressionTree) ((ExpressionStatementTree) statementTree).expression();
    assertThat(SyntaxTreeNameFinder.getName(mse)).isEqualTo("obj");
}
Also used : ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Aggregations

MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)60 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)36 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)36 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 Symbol (org.sonar.plugins.java.api.semantic.Symbol)13 Test (org.junit.Test)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)8 Tree (org.sonar.plugins.java.api.tree.Tree)7 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)5 CheckForNull (javax.annotation.CheckForNull)4 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)4 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)4 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)4 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)4 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 Resolution (org.sonar.java.resolve.Resolve.Resolution)3