use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class PrintfMisuseCheck method checkFormatting.
private void checkFormatting(MethodInvocationTree mit, boolean isMessageFormat) {
if (mit.arguments().stream().map(ExpressionTree::symbolType).anyMatch(Type::isUnknown)) {
// method resolved but not all the parameters are
return;
}
ExpressionTree formatStringTree;
List<ExpressionTree> args;
// Check type of first argument:
if (mit.arguments().get(0).symbolType().is("java.lang.String")) {
formatStringTree = mit.arguments().get(0);
args = mit.arguments().subList(1, mit.arguments().size());
} else {
// format method with "Locale" first argument, skip that one.
formatStringTree = mit.arguments().get(1);
args = mit.arguments().subList(2, mit.arguments().size());
}
if (formatStringTree.is(Tree.Kind.STRING_LITERAL)) {
String formatString = LiteralUtils.trimQuotes(((LiteralTree) formatStringTree).value());
if (isMessageFormat) {
handleMessageFormat(mit, formatString, args);
} else {
handlePrintfFormat(mit, formatString, args);
}
} else if (isConcatenationOnSameLine(formatStringTree)) {
reportIssue(mit, "Format specifiers should be used instead of string concatenation.");
}
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class MethodParametersOrderCheck method matchingTypesWrongOrder.
private static boolean matchingTypesWrongOrder(ParametersList formalParameterList, List<IdentifierTree> argumentList) {
Iterator<IdentifierTree> argumentsIterator = argumentList.stream().filter(Objects::nonNull).iterator();
int countArgumentsNotOrdered = 0;
while (argumentsIterator.hasNext()) {
IdentifierTree argument = argumentsIterator.next();
int index = formalParameterList.indexOf(argument.name().toLowerCase(Locale.ENGLISH));
Type formalType = formalParameterList.typeOfIndex(index);
Type argType = argument.symbolType();
if (!formalType.is(argType.fullyQualifiedName()) || formalType.isUnknown() || argType.isUnknown()) {
return false;
}
if (argumentList.indexOf(argument) != index) {
countArgumentsNotOrdered++;
}
}
return countArgumentsNotOrdered >= 2;
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method targetType.
@CheckForNull
private static Type targetType(TypeCastTree tree) {
Tree parent = skipParentheses(tree.parent());
Type target = null;
if (parent.is(Tree.Kind.RETURN_STATEMENT)) {
Tree method = parent;
while (!method.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
method = method.parent();
}
target = method.is(Tree.Kind.LAMBDA_EXPRESSION) ? null : ((MethodJavaType) ((MethodTree) method).symbol().type()).resultType();
} else if (parent.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) parent;
target = variableTree.symbol().type();
} else if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) parent;
if (mit.symbol().isMethodSymbol()) {
JavaSymbol.MethodJavaSymbol sym = (JavaSymbol.MethodJavaSymbol) mit.symbol();
int castArgIndex = mit.arguments().indexOf(tree);
target = sym.parameterTypes().get(castArgIndex);
}
} else if (parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.CONDITIONAL_EXPRESSION)) {
target = tree.type().symbolType();
} else if (parent.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
target = ((ArrayAccessExpressionTree) parent).expression().symbolType();
} else if (parent instanceof ExpressionTree) {
target = ((ExpressionTree) parent).symbolType();
}
return target;
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
TypeCastTree typeCastTree = (TypeCastTree) tree;
Type cast = typeCastTree.type().symbolType();
Type target = targetType(typeCastTree);
Type expressionType = typeCastTree.expression().symbolType();
if (isPrimitiveWrapperInConditional(expressionType, typeCastTree) || requiredForMemberAccess(typeCastTree)) {
// Primitive wrappers excluded because covered by S2154
return;
}
if (target != null && (isRedundantNumericalCast(cast, expressionType) || isUnnecessarySubtypeCast(expressionType, typeCastTree, target))) {
reportIssue(typeCastTree.type(), "Remove this unnecessary cast to \"" + cast + "\".");
}
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class RawExceptionCheck method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
super.visitMethod(tree);
if ((tree.is(Tree.Kind.CONSTRUCTOR) || isNotOverridden(tree)) && isNotMainMethod(tree)) {
for (TypeTree throwClause : tree.throwsClauses()) {
Type exceptionType = throwClause.symbolType();
if (isRawException(exceptionType) && !exceptionsThrownByMethodInvocations.contains(exceptionType)) {
reportIssue(throwClause);
}
}
}
exceptionsThrownByMethodInvocations.clear();
}
Aggregations