Search in sources :

Example 1 with Arguments

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

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

the class DefaultEncodingUsageCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    if (COMMONS_IO_CHARSET_MATCHERS.anyMatch(mit)) {
        Arguments arguments = mit.arguments();
        ExpressionTree lastArgument = arguments.get(arguments.size() - 1);
        testNullLiteralPassedForEncoding(lastArgument);
    } else if (FILEUTILS_WRITE_WITH_CHARSET_MATCHERS.anyMatch(mit)) {
        testNullLiteralPassedForEncoding(mit.arguments().get(2));
    } else {
        reportIssue(ExpressionUtils.methodName(mit), "Remove this use of \"" + mit.symbol().name() + "\"");
    }
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 3 with Arguments

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

the class TypeAndReferenceSolver method completeMetadata.

private static void completeMetadata(JavaSymbol symbol, List<AnnotationTree> annotations) {
    for (AnnotationTree tree : annotations) {
        AnnotationInstanceResolve annotationInstance = new AnnotationInstanceResolve((JavaSymbol.TypeJavaSymbol) tree.symbolType().symbol());
        symbol.metadata().addAnnotation(annotationInstance);
        Arguments arguments = tree.arguments();
        if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
            for (ExpressionTree expressionTree : arguments) {
                AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
                // TODO: Store more precise value than the expression (real value in case of literal, symbol for enums, array of values, solve constants?)
                annotationInstance.addValue(new AnnotationValueResolve(((IdentifierTree) aet.variable()).name(), aet.expression()));
            }
        } else {
            // Constant
            addConstantValue(tree, annotationInstance);
        }
    }
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree)

Example 4 with Arguments

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

the class TypeAndReferenceSolver method visitAnnotation.

@Override
public void visitAnnotation(AnnotationTree tree) {
    if (((AbstractTypedTree) tree.annotationType()).isTypeSet()) {
        // This handle the case where type and its annotation is shared between two variables.
        return;
    }
    resolveAs(tree.annotationType(), JavaSymbol.TYP);
    Arguments arguments = tree.arguments();
    if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
        // resolve by identifying correct identifier in assignment.
        for (ExpressionTree expressionTree : arguments) {
            AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
            IdentifierTree variable = (IdentifierTree) aet.variable();
            JavaSymbol identInType = resolve.findMethod(semanticModel.getEnv(tree), getType(tree.annotationType()), variable.name(), ImmutableList.<JavaType>of()).symbol();
            associateReference(variable, identInType);
            JavaType type = identInType.type;
            if (type == null) {
                type = Symbols.unknownType;
            }
            registerType(variable, type);
            resolveAs(aet.expression(), JavaSymbol.VAR);
        }
    } else {
        for (ExpressionTree expressionTree : arguments) {
            resolveAs(expressionTree, JavaSymbol.VAR);
        }
    }
    registerType(tree, getType(tree.annotationType()));
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree)

Example 5 with Arguments

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

the class ArrayForVarArgCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    Symbol sym;
    Arguments args;
    Tree methodName;
    if (tree.is(Tree.Kind.NEW_CLASS)) {
        NewClassTree nct = (NewClassTree) tree;
        sym = nct.constructorSymbol();
        args = nct.arguments();
        methodName = nct.identifier();
    } else {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        sym = mit.symbol();
        args = mit.arguments();
        methodName = mit.methodSelect();
    }
    if (sym.isMethodSymbol() && !args.isEmpty()) {
        ExpressionTree lastArg = args.get(args.size() - 1);
        JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) sym;
        MethodJavaType methodType = getMethodType(methodSymbol, methodName);
        checkInvokedMethod(methodSymbol, methodType, lastArg);
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Aggregations

Arguments (org.sonar.plugins.java.api.tree.Arguments)10 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)8 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)5 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)5 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)4 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)3 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)2 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 TypeArguments (org.sonar.plugins.java.api.tree.TypeArguments)2 HashSet (java.util.HashSet)1 CheckForNull (javax.annotation.CheckForNull)1 AbstractTypedTree (org.sonar.java.model.AbstractTypedTree)1 JavaSymbol (org.sonar.java.resolve.JavaSymbol)1 MethodJavaType (org.sonar.java.resolve.MethodJavaType)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1