Search in sources :

Example 36 with MemberSelectExpressionTree

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

the class CatchUsesExceptionWithContextCheck method visitMemberSelectExpression.

@Override
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
    IdentifierTree identifier = null;
    ExpressionTree expression = tree.expression();
    if (expression.is(Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) expression;
    } else if (expression.is(Kind.PARENTHESIZED_EXPRESSION) && ((ParenthesizedTree) expression).expression().is(Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) ((ParenthesizedTree) expression).expression();
    }
    if (!validUsagesStack.isEmpty() && identifier != null) {
        Iterator<Collection<IdentifierTree>> iterator = validUsagesStack.iterator();
        while (iterator.hasNext()) {
            iterator.next().remove(identifier);
        }
    }
    super.visitMemberSelectExpression(tree);
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Collection(java.util.Collection)

Example 37 with MemberSelectExpressionTree

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

the class ClassComparedByNameCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    List<ExpressionTree> expressionsToCheck = new ArrayList<>(2);
    if (mit.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
        expressionsToCheck.add(((MemberSelectExpressionTree) mit.methodSelect()).expression());
    }
    expressionsToCheck.add(mit.arguments().get(0));
    boolean useAssignableMessage = expressionsToCheck.size() > 1;
    boolean useClassGetName = false;
    boolean useStackTraceElementGetClassName = false;
    for (ExpressionTree expression : expressionsToCheck) {
        if (expression.is(Tree.Kind.IDENTIFIER) && isParam(((IdentifierTree) expression).symbol())) {
            // exclude comparison to method parameters
            return;
        }
        ClassGetNameDetector visitor = new ClassGetNameDetector();
        expression.accept(visitor);
        useAssignableMessage &= visitor.useClassGetName;
        useClassGetName |= visitor.useClassGetName;
        useStackTraceElementGetClassName |= visitor.useStackTraceElementGetClassName;
    }
    if (useClassGetName && !useStackTraceElementGetClassName) {
        String message = "Use an \"instanceof\" comparison instead.";
        if (useAssignableMessage) {
            message = "Use \"isAssignableFrom\" instead.";
        }
        reportIssue(mit, message);
    }
}
Also used : ArrayList(java.util.ArrayList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 38 with MemberSelectExpressionTree

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

the class CollectionCallingItselfCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
        Symbol symbolReference = null;
        Symbol method = null;
        String reportedName = "";
        if (methodInvocationTree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) methodInvocationTree.methodSelect();
            IdentifierTree identifier = mse.identifier();
            reportedName = identifier.name();
            method = identifier.symbol();
            if (mse.expression().is(Tree.Kind.IDENTIFIER)) {
                symbolReference = ((IdentifierTree) mse.expression()).symbol();
            }
        }
        if (symbolReference != null && isMethodFromCollection(method)) {
            reportIssueForParameters(methodInvocationTree, symbolReference, reportedName);
        }
    }
}
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) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 39 with MemberSelectExpressionTree

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

the class CollectionInappropriateCallsCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree tree) {
    ExpressionTree firstArgument = tree.arguments().get(0);
    Type argumentType = firstArgument.symbolType();
    if (argumentType.isUnknown()) {
        // could happen with type inference.
        return;
    }
    Type collectionType = getMethodOwner(tree);
    // can be null when using raw types
    Type collectionParameterType = getTypeParameter(collectionType);
    boolean isCallToParametrizedOrUnknownMethod = isCallToParametrizedOrUnknownMethod(firstArgument);
    if (!isCallToParametrizedOrUnknownMethod && tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
        isCallToParametrizedOrUnknownMethod = isCallToParametrizedOrUnknownMethod(((MemberSelectExpressionTree) tree.methodSelect()).expression());
    }
    if (collectionParameterType != null && !collectionParameterType.isUnknown() && !isCallToParametrizedOrUnknownMethod && !isArgumentCompatible(argumentType, collectionParameterType)) {
        reportIssue(ExpressionUtils.methodName(tree), MessageFormat.format("A \"{0}<{1}>\" cannot contain a \"{2}\"", collectionType, collectionParameterType, argumentType));
    }
}
Also used : TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) 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 40 with MemberSelectExpressionTree

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

the class FlowComputation method getArgumentIdentifier.

@CheckForNull
public static IdentifierTree getArgumentIdentifier(MethodInvocationTree mit, int index) {
    Arguments arguments = mit.arguments();
    if (index < 0 || index > arguments.size()) {
        throw new IllegalArgumentException("index must be within arguments range.");
    }
    ExpressionTree expr = ExpressionUtils.skipParentheses(arguments.get(index));
    switch(expr.kind()) {
        case MEMBER_SELECT:
            return ((MemberSelectExpressionTree) expr).identifier();
        case IDENTIFIER:
            return ((IdentifierTree) expr);
        default:
            return null;
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) 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