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);
}
}
}
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);
}
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);
}
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);
}
}
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()));
}
Aggregations