use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class StandardFunctionalInterfaceCheck method lookupMatchingStandardInterface.
private static Optional<String> lookupMatchingStandardInterface(MethodSymbol functionalMethod) {
MethodTree declaration = functionalMethod.declaration();
if (!functionalMethod.thrownTypes().isEmpty() || (declaration != null && !declaration.typeParameters().isEmpty())) {
return Optional.empty();
}
Type returnType = declaration != null ? declaration.returnType().symbolType() : functionalMethod.returnType().type();
return STD_INTERFACE_BY_PARAMETER_COUNT.getOrDefault(functionalMethod.parameterTypes().size(), Collections.emptyList()).stream().map(standardInterface -> standardInterface.matchingSpecialization(functionalMethod, returnType)).filter(Objects::nonNull).findFirst();
}
use of org.sonar.plugins.java.api.semantic.Type 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.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class RedundantThrowsDeclarationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
ListTree<TypeTree> thrownList = methodTree.throwsClauses();
Set<Type> thrownExceptions = thrownExceptionsFromBody(methodTree);
boolean hasTryWithResourceInBody = hasTryWithResourceInBody(methodTree);
Set<String> reported = new HashSet<>();
for (TypeTree typeTree : thrownList) {
Type exceptionType = typeTree.symbolType();
if (hasTryWithResourceInBody && (exceptionType.is("java.io.IOException") || exceptionType.is("java.lang.Exception"))) {
// method 'close()' from 'java.io.Closeable' interface throws 'java.io.IOException"
continue;
}
String fullyQualifiedName = exceptionType.fullyQualifiedName();
if (!reported.contains(fullyQualifiedName)) {
String superTypeName = isSubclassOfAny(exceptionType, thrownList);
if (superTypeName != null) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a subclass of '%s'.", fullyQualifiedName, superTypeName));
} else if (exceptionType.isSubtypeOf("java.lang.RuntimeException")) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s' which is a runtime exception.", fullyQualifiedName));
} else if (declaredMoreThanOnce(fullyQualifiedName, thrownList)) {
reportIssue(typeTree, String.format("Remove the redundant '%s' thrown exception declaration(s).", fullyQualifiedName));
} else if (canNotBeThrown(methodTree, exceptionType, thrownExceptions)) {
reportIssue(typeTree, String.format("Remove the declaration of thrown exception '%s', as it cannot be thrown from %s's body.", fullyQualifiedName, methodTreeType(methodTree)));
}
reported.add(fullyQualifiedName);
}
}
}
use of org.sonar.plugins.java.api.semantic.Type 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);
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class SynchronizationOnStringOrBoxedCheck method visitNode.
@Override
public void visitNode(Tree tree) {
SynchronizedStatementTree syncStatement = (SynchronizedStatementTree) tree;
Type expressionType = syncStatement.expression().symbolType();
if (expressionType.isPrimitive() || isForbiddenType(expressionType)) {
reportIssue(syncStatement.expression(), "Synchronize on a new \"Object\" instead.");
}
}
Aggregations