Search in sources :

Example 51 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class PrintfMisuseCheck method checkFormatting.

private void checkFormatting(MethodInvocationTree mit, boolean isMessageFormat) {
    if (mit.arguments().stream().map(ExpressionTree::symbolType).anyMatch(Type::isUnknown)) {
        // method resolved but not all the parameters are
        return;
    }
    ExpressionTree formatStringTree;
    List<ExpressionTree> args;
    // Check type of first argument:
    if (mit.arguments().get(0).symbolType().is("java.lang.String")) {
        formatStringTree = mit.arguments().get(0);
        args = mit.arguments().subList(1, mit.arguments().size());
    } else {
        // format method with "Locale" first argument, skip that one.
        formatStringTree = mit.arguments().get(1);
        args = mit.arguments().subList(2, mit.arguments().size());
    }
    if (formatStringTree.is(Tree.Kind.STRING_LITERAL)) {
        String formatString = LiteralUtils.trimQuotes(((LiteralTree) formatStringTree).value());
        if (isMessageFormat) {
            handleMessageFormat(mit, formatString, args);
        } else {
            handlePrintfFormat(mit, formatString, args);
        }
    } else if (isConcatenationOnSameLine(formatStringTree)) {
        reportIssue(mit, "Format specifiers should be used instead of string concatenation.");
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 52 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class MethodParametersOrderCheck method matchingTypesWrongOrder.

private static boolean matchingTypesWrongOrder(ParametersList formalParameterList, List<IdentifierTree> argumentList) {
    Iterator<IdentifierTree> argumentsIterator = argumentList.stream().filter(Objects::nonNull).iterator();
    int countArgumentsNotOrdered = 0;
    while (argumentsIterator.hasNext()) {
        IdentifierTree argument = argumentsIterator.next();
        int index = formalParameterList.indexOf(argument.name().toLowerCase(Locale.ENGLISH));
        Type formalType = formalParameterList.typeOfIndex(index);
        Type argType = argument.symbolType();
        if (!formalType.is(argType.fullyQualifiedName()) || formalType.isUnknown() || argType.isUnknown()) {
            return false;
        }
        if (argumentList.indexOf(argument) != index) {
            countArgumentsNotOrdered++;
        }
    }
    return countArgumentsNotOrdered >= 2;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 53 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class RedundantTypeCastCheck method targetType.

@CheckForNull
private static Type targetType(TypeCastTree tree) {
    Tree parent = skipParentheses(tree.parent());
    Type target = null;
    if (parent.is(Tree.Kind.RETURN_STATEMENT)) {
        Tree method = parent;
        while (!method.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
            method = method.parent();
        }
        target = method.is(Tree.Kind.LAMBDA_EXPRESSION) ? null : ((MethodJavaType) ((MethodTree) method).symbol().type()).resultType();
    } else if (parent.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) parent;
        target = variableTree.symbol().type();
    } else if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) parent;
        if (mit.symbol().isMethodSymbol()) {
            JavaSymbol.MethodJavaSymbol sym = (JavaSymbol.MethodJavaSymbol) mit.symbol();
            int castArgIndex = mit.arguments().indexOf(tree);
            target = sym.parameterTypes().get(castArgIndex);
        }
    } else if (parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.CONDITIONAL_EXPRESSION)) {
        target = tree.type().symbolType();
    } else if (parent.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        target = ((ArrayAccessExpressionTree) parent).expression().symbolType();
    } else if (parent instanceof ExpressionTree) {
        target = ((ExpressionTree) parent).symbolType();
    }
    return target;
}
Also used : JavaType(org.sonar.java.resolve.JavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) CheckForNull(javax.annotation.CheckForNull)

Example 54 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class RedundantTypeCastCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    TypeCastTree typeCastTree = (TypeCastTree) tree;
    Type cast = typeCastTree.type().symbolType();
    Type target = targetType(typeCastTree);
    Type expressionType = typeCastTree.expression().symbolType();
    if (isPrimitiveWrapperInConditional(expressionType, typeCastTree) || requiredForMemberAccess(typeCastTree)) {
        // Primitive wrappers excluded because covered by S2154
        return;
    }
    if (target != null && (isRedundantNumericalCast(cast, expressionType) || isUnnecessarySubtypeCast(expressionType, typeCastTree, target))) {
        reportIssue(typeCastTree.type(), "Remove this unnecessary cast to \"" + cast + "\".");
    }
}
Also used : TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) JavaType(org.sonar.java.resolve.JavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 55 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class RawExceptionCheck method visitMethod.

@Override
public void visitMethod(MethodTree tree) {
    super.visitMethod(tree);
    if ((tree.is(Tree.Kind.CONSTRUCTOR) || isNotOverridden(tree)) && isNotMainMethod(tree)) {
        for (TypeTree throwClause : tree.throwsClauses()) {
            Type exceptionType = throwClause.symbolType();
            if (isRawException(exceptionType) && !exceptionsThrownByMethodInvocations.contains(exceptionType)) {
                reportIssue(throwClause);
            }
        }
    }
    exceptionsThrownByMethodInvocations.clear();
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Aggregations

Type (org.sonar.plugins.java.api.semantic.Type)164 Test (org.junit.Test)67 Symbol (org.sonar.plugins.java.api.semantic.Symbol)23 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)23 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)18 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)18 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)17 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 JavaType (org.sonar.java.resolve.JavaType)16 Tree (org.sonar.plugins.java.api.tree.Tree)15 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)12 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)11 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)10 ArrayList (java.util.ArrayList)9 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)8 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8