Search in sources :

Example 6 with ParametrizedTypeJavaType

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

the class OptionalAsParameterCheck method expectedTypeInsteadOfOptional.

private static Optional<String> expectedTypeInsteadOfOptional(Type type) {
    if (type.is(JAVA_UTIL_OPTIONAL)) {
        String msg;
        if (((JavaType) type).isParameterized()) {
            ParametrizedTypeJavaType ptjt = (ParametrizedTypeJavaType) type;
            String parameterTypeName = ptjt.substitution(ptjt.typeParameters().get(0)).erasure().name();
            msg = formatMsg(parameterTypeName);
        } else {
            msg = "Specify a type instead.";
        }
        return Optional.of(msg);
    }
    return PRIMITIVE_OPTIONALS.stream().filter(type::is).findFirst().map(optional -> formatMsg(optional.substring(JAVA_UTIL_OPTIONAL.length()).toLowerCase()));
}
Also used : JavaType(org.sonar.java.resolve.JavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType)

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