Search in sources :

Example 1 with ExpressionTree

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

the class AnnotationArgumentOrderCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
    if (annotationSymbol.isUnknown()) {
        return;
    }
    List<String> declarationNames = new ArrayList<>();
    for (Symbol symbol : annotationSymbol.memberSymbols()) {
        declarationNames.add(symbol.name());
    }
    List<String> annotationArguments = new ArrayList<>();
    for (ExpressionTree argument : annotationTree.arguments()) {
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
            IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
            annotationArguments.add(nameTree.name());
        }
    }
    declarationNames.retainAll(annotationArguments);
    if (!declarationNames.equals(annotationArguments)) {
        reportIssue(annotationTree.annotationType(), "Reorder annotation arguments to match the order of declaration.");
    }
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 2 with ExpressionTree

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

the class KeySetInsteadOfEntrySetCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        ForEachStatement forEachTree = (ForEachStatement) tree;
        ExpressionTree expressionTree = forEachTree.expression();
        if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree methodTree = (MethodInvocationTree) expressionTree;
            Symbol ownerSymbol = getOwnerSymbol(methodTree);
            if (ownerSymbol != null && MAP_KEYSET_METHOD.matches(methodTree)) {
                new GetUsageVisitor().isCallingGetWithSymbol(forEachTree, forEachTree.variable().symbol(), ownerSymbol);
            }
        }
    }
}
Also used : 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) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 3 with ExpressionTree

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

the class ImmediateReverseBoxingCheck method checkForUnboxing.

private void checkForUnboxing(ExpressionTree expressionTree) {
    if (!expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        return;
    }
    MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expressionTree;
    if (isUnboxingMethodInvocation(methodInvocationTree)) {
        ExpressionTree methodSelect = methodInvocationTree.methodSelect();
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) methodSelect;
            ExpressionTree unboxedExpression = memberSelectExpressionTree.expression();
            String unboxingResultTypeName = methodInvocationTree.symbolType().fullyQualifiedName();
            if (unboxingResultTypeName.equals(PRIMITIVE_TYPES_BY_WRAPPER.get(unboxedExpression.symbolType().fullyQualifiedName()))) {
                addUnboxingIssue(expressionTree, unboxedExpression);
            }
        }
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) 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 4 with ExpressionTree

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

the class InvalidDateValuesCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    String name = getMethodName(mit);
    Arguments arguments = mit.arguments();
    if ("set".equals(name)) {
        // Calendar method
        ExpressionTree arg0 = arguments.get(0);
        ExpressionTree arg1 = arguments.get(1);
        String referenceName = getReferencedCalendarName(arg0);
        if (referenceName != null) {
            checkArgument(arg1, referenceName, "\"{0}\" is not a valid value for setting \"{1}\".");
        }
    } else if ("<init>".equals(mit.symbol().name())) {
        // call to this() or super()
        checkConstructorArguments(mit.arguments());
    } else {
        checkArgument(arguments.get(0), name, "\"{0}\" is not a valid value for \"{1}\" method.");
    }
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 5 with ExpressionTree

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

the class LDAPInjectionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodInvocationTree mit = (MethodInvocationTree) tree;
    if (isDirContextSearchCall(mit)) {
        // Check the first two arguments of search method
        checkDirContextArg(mit.arguments().get(0), mit);
        checkDirContextArg(mit.arguments().get(1), mit);
    } else if (isSearchControlCall(mit)) {
        ExpressionTree arg = mit.arguments().get(0);
        if (isDynamicArray(arg, mit)) {
            reportOnArgument(arg);
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Aggregations

ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)189 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)88 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)80 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)66 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)50 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)44 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)35 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)30 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)30 Test (org.junit.Test)25 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)24 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)23 Type (org.sonar.plugins.java.api.semantic.Type)21 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)20 Tree (org.sonar.plugins.java.api.tree.Tree)20 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)14 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)13