Search in sources :

Example 1 with TypeVariableJavaType

use of org.sonar.java.resolve.TypeVariableJavaType in project sonar-java by SonarSource.

the class EnumMapCheck method hasEnumKey.

private static boolean hasEnumKey(Type symbolType) {
    Type type = symbolType;
    if (type instanceof MethodJavaType) {
        type = ((MethodJavaType) type).resultType();
    }
    if (type instanceof ParametrizedTypeJavaType) {
        ParametrizedTypeJavaType parametrizedTypeJavaType = (ParametrizedTypeJavaType) type;
        List<TypeVariableJavaType> typeParameters = parametrizedTypeJavaType.typeParameters();
        if (!typeParameters.isEmpty()) {
            return parametrizedTypeJavaType.substitution(typeParameters.get(0)).symbol().isEnum();
        }
    }
    return false;
}
Also used : TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) MethodJavaType(org.sonar.java.resolve.MethodJavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 2 with TypeVariableJavaType

use of org.sonar.java.resolve.TypeVariableJavaType in project sonar-java by SonarSource.

the class EnumSetCheck method checkIssue.

private void checkIssue(Type type, Tree reportTree, TypeTree typeTree) {
    if (type.isSubtypeOf("java.util.Set") && !type.isSubtypeOf("java.util.EnumSet") && type instanceof ParametrizedTypeJavaType) {
        ParametrizedTypeJavaType parametrizedType = (ParametrizedTypeJavaType) type;
        List<TypeVariableJavaType> typeParameters = parametrizedType.typeParameters();
        Type variableType = typeTree.symbolType();
        if (typeParameters.isEmpty() && variableType instanceof ParametrizedTypeJavaType) {
            // for java 7 diamond operator lookup declaration.
            parametrizedType = (ParametrizedTypeJavaType) variableType;
            typeParameters = parametrizedType.typeParameters();
        }
        if (!typeParameters.isEmpty()) {
            Type typeParameter = parametrizedType.substitution(typeParameters.get(0));
            if (typeParameter != null && typeParameter.symbol().isEnum()) {
                reportIssue(reportTree, "Convert this Set to an EnumSet.");
            }
        }
    }
}
Also used : TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType)

Example 3 with TypeVariableJavaType

use of org.sonar.java.resolve.TypeVariableJavaType 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 4 with TypeVariableJavaType

use of org.sonar.java.resolve.TypeVariableJavaType in project sonar-java by SonarSource.

the class CollectionInappropriateCallsCheck method getTypeParameter.

@Nullable
private static Type getTypeParameter(Type collectionType) {
    if (collectionType instanceof ParametrizedTypeJavaType) {
        ParametrizedTypeJavaType parametrizedType = (ParametrizedTypeJavaType) collectionType;
        TypeVariableJavaType first = Iterables.getFirst(parametrizedType.typeParameters(), null);
        if (first != null) {
            return parametrizedType.substitution(first);
        }
    }
    return null;
}
Also used : ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Nullable(javax.annotation.Nullable)

Aggregations

TypeVariableJavaType (org.sonar.java.resolve.TypeVariableJavaType)4 ParametrizedTypeJavaType (org.sonar.java.resolve.ParametrizedTypeJavaType)3 Type (org.sonar.plugins.java.api.semantic.Type)3 MethodJavaType (org.sonar.java.resolve.MethodJavaType)2 Nullable (javax.annotation.Nullable)1 JavaSymbol (org.sonar.java.resolve.JavaSymbol)1 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)1 JavaType (org.sonar.java.resolve.JavaType)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 TypeCastTree (org.sonar.plugins.java.api.tree.TypeCastTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1