Search in sources :

Example 1 with ParametrizedTypeJavaType

use of org.sonar.java.resolve.ParametrizedTypeJavaType 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 ParametrizedTypeJavaType

use of org.sonar.java.resolve.ParametrizedTypeJavaType 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 ParametrizedTypeJavaType

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

the class CompareToNotOverloadedCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    if (hasSemantic() && isCompareToMethod(methodTree) && Boolean.FALSE.equals(methodTree.isOverriding())) {
        ClassJavaType ownerType = (ClassJavaType) methodTree.symbol().owner().type();
        ownerType.superTypes().stream().filter(supertype -> supertype.is("java.lang.Comparable")).findFirst().ifPresent(comparableType -> {
            String name = "Object";
            if (comparableType.isParameterized()) {
                ParametrizedTypeJavaType ptjt = (ParametrizedTypeJavaType) comparableType;
                name = ptjt.substitution(ptjt.typeParameters().get(0)).symbol().name();
            }
            reportIssue(methodTree.parameters().get(0), "Refactor this method so that its argument is of type '" + name + "'.");
        });
    }
}
Also used : ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassJavaType(org.sonar.java.resolve.ClassJavaType)

Example 4 with ParametrizedTypeJavaType

use of org.sonar.java.resolve.ParametrizedTypeJavaType 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)

Example 5 with ParametrizedTypeJavaType

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

the class ClassWithoutHashCodeInHashStructureCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    Type type = ((NewClassTree) tree).symbolType();
    if (type instanceof ParametrizedTypeJavaType && useHashDataStructure(type)) {
        ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) type;
        Symbol.TypeSymbol symbol = ptt.substitution(ptt.typeParameters().get(0)).symbol();
        if (implementsEquals(symbol) && !implementsHashCode(symbol)) {
            reportIssue(tree, "Add a \"hashCode()\" method to \"" + symbol.name() + "\" or remove it from this hash.");
        }
    }
}
Also used : ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Aggregations

ParametrizedTypeJavaType (org.sonar.java.resolve.ParametrizedTypeJavaType)6 TypeVariableJavaType (org.sonar.java.resolve.TypeVariableJavaType)3 Type (org.sonar.plugins.java.api.semantic.Type)3 Nullable (javax.annotation.Nullable)1 ClassJavaType (org.sonar.java.resolve.ClassJavaType)1 JavaType (org.sonar.java.resolve.JavaType)1 MethodJavaType (org.sonar.java.resolve.MethodJavaType)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)1