Search in sources :

Example 86 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree 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 87 with IdentifierTree

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

the class DataStoredInSessionCheck method checkArgument.

private void checkArgument(ExpressionTree argument, ExpressionTree startPoint, MethodInvocationTree reportTree) {
    ExpressionTree expressionToEvaluate = argument;
    if (expressionToEvaluate.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) expressionToEvaluate;
        identifiersUsedToSetAttribute.add(identifier);
        Symbol variable = identifier.symbol();
        ExpressionTree lastAssignmentOrDeclaration = ReassignmentFinder.getClosestReassignmentOrDeclarationExpression(startPoint, variable);
        if (lastAssignmentOrDeclaration != null && !usedBetween(variable, lastAssignmentOrDeclaration, startPoint)) {
            expressionToEvaluate = lastAssignmentOrDeclaration;
        }
    }
    if (isRequestOrCookieDataRetrieval(expressionToEvaluate)) {
        reportIssue(reportTree.methodSelect(), "Make sure the user is authenticated before this data is stored in the session.");
    } else if (expressionToEvaluate.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expressionToEvaluate;
        if (NO_EFFECT_OPERATION.anyMatch(mit)) {
            checkArgument(mit.arguments().get(0), mit, reportTree);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 88 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree 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 89 with IdentifierTree

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

the class CollectionMethodsWithLinearComplexityCheck method usageInAssignment.

private static Stream<AssignmentExpressionTree> usageInAssignment(IdentifierTree usage) {
    Tree prevParent = usage;
    Tree parent = usage.parent();
    while (parent != null && !parent.is(Tree.Kind.ASSIGNMENT) && parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.IDENTIFIER)) {
        prevParent = parent;
        parent = parent.parent();
    }
    if (parent != null && parent.is(Tree.Kind.ASSIGNMENT)) {
        AssignmentExpressionTree assignment = (AssignmentExpressionTree) parent;
        if (assignment.variable().equals(prevParent)) {
            return Stream.of(assignment);
        }
    }
    return Stream.empty();
}
Also used : Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 90 with IdentifierTree

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

the class CloneOverrideCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    IdentifierTree identifierTree = methodTree.simpleName();
    if (methodTree.parameters().isEmpty() && "clone".equals(identifierTree.name()) && !isUnsupportedCloneOverride(methodTree)) {
        reportIssue(identifierTree, "Remove this \"clone\" implementation; use a copy constructor or copy factory instead.");
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7