Search in sources :

Example 66 with Type

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

the class StandardFunctionalInterfaceCheck method lookupMatchingStandardInterface.

private static Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) {
    MethodTree declaration = functionalMethod.declaration();
    if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) {
        return Optional.empty();
    }
    Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type();
    return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream().map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)).filter(Objects::nonNull).findFirst();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 67 with Type

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

the class SillyEqualsCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree tree) {
    ExpressionTree firstArgument = Iterables.getOnlyElement(tree.arguments());
    Type argumentType = firstArgument.symbolType().erasure();
    if (argumentType.isPrimitive()) {
        argumentType = ((JavaType) argumentType).primitiveWrapperType();
    }
    Type ownerType = getMethodOwnerType(tree).erasure();
    IdentifierTree methodInvocationName = ExpressionUtils.methodName(tree);
    if (isLiteralNull(firstArgument)) {
        reportIssue(methodInvocationName, "Remove this call to \"equals\"; comparisons against null always return false; consider using '== null' to check for nullity.");
    } else if (ownerType.isArray()) {
        checkWhenOwnerIsArray(methodInvocationName, (Type.ArrayType) ownerType, argumentType);
    } else {
        checkWhenOwnerIsNotArray(methodInvocationName, ownerType, argumentType);
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) 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 68 with Type

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

the class RedundantThrowsDeclarationCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    ListTree<TypeTree> thrownList = methodTree.throwsClauses();
    Set<Type> thrownExceptions = thrownExceptionsFromBody(methodTree);
    boolean hasTryWithResourceInBody = hasTryWithResourceInBody(methodTree);
    Set<String> reported = new HashSet<>();
    for (TypeTree typeTree : thrownList) {
        Type exceptionType = typeTree.symbolType();
        if (hasTryWithResourceInBody && (exceptionType.is("java.io.IOException") || exceptionType.is("java.lang.Exception"))) {
            // method 'close()' from 'java.io.Closeable' interface throws 'java.io.IOException"
            continue;
        }
        String fullyQualifiedName = exceptionType.fullyQualifiedName();
        if (!reported.contains(fullyQualifiedName)) {
            String superTypeName = isSubclassOfAny(exceptionType, thrownList);
            if (superTypeName != null) {
                reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a subclass of '%s'.", fullyQualifiedName, superTypeName));
            } else if (exceptionType.isSubtypeOf("java.lang.RuntimeException")) {
                reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a runtime exception.", fullyQualifiedName));
            } else if (declaredMoreThanOnce(fullyQualifiedName, thrownList)) {
                reportIssue(typeTree, String.format("Remove the redundant '%s' thrown exception declaration(s).", fullyQualifiedName));
            } else if (canNotBeThrown(methodTree, exceptionType, thrownExceptions)) {
                reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s', as it cannot be thrown from %s's body.", fullyQualifiedName, methodTreeType(methodTree)));
            }
            reported.add(fullyQualifiedName);
        }
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) HashSet(java.util.HashSet)

Example 69 with Type

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

the class RedundantTypeCastCheck method requiredForMemberAccess.

private static boolean requiredForMemberAccess(TypeCastTree typeCastTree) {
    ExpressionTree expression = typeCastTree.expression();
    if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
        Tree parent = typeCastTree.parent();
        return expression.is(Tree.Kind.METHOD_REFERENCE) && parent != null && skipParentheses(parent).is(Tree.Kind.MEMBER_SELECT);
    }
    Symbol symbol = ((MethodInvocationTree) expression).symbol();
    if (!symbol.isMethodSymbol()) {
        return false;
    }
    Type returnType = ((Symbol.MethodSymbol) symbol).returnType().type();
    if (!(returnType instanceof TypeVariableJavaType) || ((TypeVariableJavaType) returnType).bounds().get(0).is("java.lang.Object")) {
        return false;
    }
    // as the member accessed could have also been part of initial type
    return skipParentheses(typeCastTree.parent()).is(Tree.Kind.MEMBER_SELECT);
}
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) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) 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)

Example 70 with Type

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

the class SynchronizationOnStringOrBoxedCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    SynchronizedStatementTree syncStatement = (SynchronizedStatementTree) tree;
    Type expressionType = syncStatement.expression().symbolType();
    if (expressionType.isPrimitive() || isForbiddenType(expressionType)) {
        reportIssue(syncStatement.expression(), "Synchronize on a new \"Object\" instead.");
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree)

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