Search in sources :

Example 1 with JavaType

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

the class ExplodedGraphWalker method executeTypeCast.

private void executeTypeCast(TypeCastTree typeCast) {
    Type type = typeCast.type().symbolType();
    if (type.isPrimitive()) {
        JavaType expType = (JavaType) typeCast.expression().symbolType();
        // create SV to consume factory if any
        SymbolicValue castSV = constraintManager.createSymbolicValue(typeCast);
        // if exp type is a primitive and subtype of cast type, we can reuse the same symbolic value
        if (!expType.isPrimitive() || !new Types().isSubtype(expType, (JavaType) type)) {
            ProgramState.Pop unstack = programState.unstackValue(1);
            programState = unstack.state;
            programState = programState.stackValue(castSV);
        }
    }
}
Also used : Types(org.sonar.java.resolve.Types) JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 2 with JavaType

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

the class PrimitiveTypeBoxingWithToStringCheck method isValueOfInvocation.

private static boolean isValueOfInvocation(ExpressionTree abstractTypedTree) {
    if (!abstractTypedTree.is(Kind.METHOD_INVOCATION)) {
        return false;
    }
    Type type = abstractTypedTree.symbolType();
    MethodMatcher valueOfMatcher = MethodMatcher.create().typeDefinition(type.fullyQualifiedName()).name("valueOf").addParameter(((JavaType) type).primitiveType().fullyQualifiedName());
    return valueOfMatcher.matches((MethodInvocationTree) abstractTypedTree);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) JavaType(org.sonar.java.resolve.JavaType) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 3 with JavaType

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

the class RedundantAbstractMethodCheck method differentReturnType.

private static boolean differentReturnType(JavaSymbol.MethodJavaSymbol method, JavaSymbol.MethodJavaSymbol overridee) {
    JavaType methodResultType = resultType(method);
    JavaType overrideeResultType = resultType(overridee);
    return specializationOfReturnType(methodResultType.erasure(), overrideeResultType.erasure()) || useRawTypeOfParametrizedType(methodResultType, overrideeResultType);
}
Also used : JavaType(org.sonar.java.resolve.JavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 4 with JavaType

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

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

JavaType (org.sonar.java.resolve.JavaType)5 Type (org.sonar.plugins.java.api.semantic.Type)3 ParametrizedTypeJavaType (org.sonar.java.resolve.ParametrizedTypeJavaType)2 MethodMatcher (org.sonar.java.matcher.MethodMatcher)1 MethodJavaType (org.sonar.java.resolve.MethodJavaType)1 Types (org.sonar.java.resolve.Types)1 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)1 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1