Search in sources :

Example 16 with MemberSelectExpressionTree

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

the class PublicStaticMutableMembersCheck method checkAssignment.

private void checkAssignment(AssignmentExpressionTree node) {
    ExpressionTree variable = ExpressionUtils.skipParentheses(node.variable());
    if (variable.is(Tree.Kind.MEMBER_SELECT)) {
        variable = ((MemberSelectExpressionTree) variable).identifier();
    }
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifierTree = (IdentifierTree) variable;
        Symbol symbol = identifierTree.symbol();
        if (IMMUTABLE_CANDIDATES.contains(symbol) && isMutable(node.expression(), symbol)) {
            reportIssue(identifierTree, "Make member \"" + symbol.name() + "\" \"protected\".");
            IMMUTABLE_CANDIDATES.remove(symbol);
        }
    }
}
Also used : 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) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 17 with MemberSelectExpressionTree

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

the class PrimitiveTypeBoxingWithToStringCheck method visitMethodInvocation.

@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    if (TO_STRING_MATCHERS.anyMatch(tree)) {
        ExpressionTree abstractTypedTree = ((MemberSelectExpressionTree) tree.methodSelect()).expression();
        if (abstractTypedTree.is(Kind.NEW_CLASS) || isValueOfInvocation(abstractTypedTree)) {
            String typeName = abstractTypedTree.symbolType().toString();
            createIssue(tree, typeName);
        }
    }
    super.visitMethodInvocation(tree);
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 18 with MemberSelectExpressionTree

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

the class PrintStackTraceCalledWithoutArgumentCheck method visitMethodInvocation.

@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    super.visitMethodInvocation(tree);
    if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) tree.methodSelect();
        IdentifierTree identifierTree = memberSelectExpressionTree.identifier();
        if (!enclosingClassExtendsThrowable() && "printStackTrace".equals(identifierTree.name()) && calledOnTypeInheritedFromThrowable(memberSelectExpressionTree.expression()) && tree.arguments().isEmpty()) {
            context.reportIssue(this, identifierTree, "Use a logger to log this exception.");
        }
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 19 with MemberSelectExpressionTree

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

the class MutableMembersUsageCheck method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    super.visitAssignmentExpression(tree);
    if (!isMutableType(tree.expression())) {
        return;
    }
    ExpressionTree variable = tree.variable();
    Symbol leftSymbol = null;
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifierTree = (IdentifierTree) variable;
        leftSymbol = identifierTree.symbol();
    } else if (variable.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mit = (MemberSelectExpressionTree) variable;
        leftSymbol = mit.identifier().symbol();
    }
    if (leftSymbol != null && leftSymbol.isPrivate()) {
        checkStore(tree.expression());
    }
}
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) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 20 with MemberSelectExpressionTree

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

the class OverwrittenKeyCheck method isMapPut.

@CheckForNull
private static CollectionAndKey isMapPut(StatementTree statementTree) {
    if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
        if (expression.is(Tree.Kind.METHOD_INVOCATION) && MAP_PUT.matches((MethodInvocationTree) expression)) {
            MethodInvocationTree mapPut = (MethodInvocationTree) expression;
            Symbol collection = mapPut.methodSelect().is(Tree.Kind.MEMBER_SELECT) ? symbolFromIdentifier(((MemberSelectExpressionTree) mapPut.methodSelect()).expression()) : null;
            ExpressionTree keyTree = mapPut.arguments().get(0);
            Object key = extractKey(keyTree);
            if (collection != null && key != null) {
                return new CollectionAndKey(collection, keyTree, key, false, null);
            }
        }
    }
    return null;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) CheckForNull(javax.annotation.CheckForNull)

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