use of org.sonar.java.resolve.MethodJavaType in project sonar-java by SonarSource.
the class EnumMapCheck method hasEnumKey.
private static boolean hasEnumKey(Type symbolType) {
Type type = symbolType;
if (type instanceof MethodJavaType) {
type = ((MethodJavaType) type).resultType();
}
if (type instanceof ParametrizedTypeJavaType) {
ParametrizedTypeJavaType parametrizedTypeJavaType = (ParametrizedTypeJavaType) type;
List<TypeVariableJavaType> typeParameters = parametrizedTypeJavaType.typeParameters();
if (!typeParameters.isEmpty()) {
return parametrizedTypeJavaType.substitution(typeParameters.get(0)).symbol().isEnum();
}
}
return false;
}
use of org.sonar.java.resolve.MethodJavaType in project sonar-java by SonarSource.
the class ArrayForVarArgCheck method checkInvokedMethod.
private void checkInvokedMethod(JavaSymbol.MethodJavaSymbol methodSymbol, @Nullable MethodJavaType methodType, ExpressionTree lastArg) {
if (methodSymbol.isVarArgs() && lastArg.is(Tree.Kind.NEW_ARRAY)) {
if (lastParamHasSameType(methodSymbol, methodType, lastArg.symbolType())) {
String message = "Remove this array creation";
NewArrayTree newArrayTree = (NewArrayTree) lastArg;
if (newArrayTree.openBraceToken() == null) {
ExpressionTree expression = newArrayTree.dimensions().get(0).expression();
Integer literalValue = LiteralUtils.intLiteralValue(expression);
if (literalValue == null || literalValue != 0 || isCallingOverload(methodSymbol, lastArg)) {
return;
}
} else if (!newArrayTree.initializers().isEmpty()) {
message += " and simply pass the elements";
}
reportIssue(lastArg, message + ".");
} else {
String type = ((Type.ArrayType) getLastParameterType(methodSymbol.parameterTypes())).elementType().name();
reportIssue(lastArg, "Disambiguate this call by either casting as \"" + type + "\" or \"" + type + "[]\".");
}
}
}
use of org.sonar.java.resolve.MethodJavaType in project sonar-java by SonarSource.
the class ArrayForVarArgCheck method visitNode.
@Override
public void visitNode(Tree tree) {
Symbol sym;
Arguments args;
Tree methodName;
if (tree.is(Tree.Kind.NEW_CLASS)) {
NewClassTree nct = (NewClassTree) tree;
sym = nct.constructorSymbol();
args = nct.arguments();
methodName = nct.identifier();
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
sym = mit.symbol();
args = mit.arguments();
methodName = mit.methodSelect();
}
if (sym.isMethodSymbol() && !args.isEmpty()) {
ExpressionTree lastArg = args.get(args.size() - 1);
JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) sym;
MethodJavaType methodType = getMethodType(methodSymbol, methodName);
checkInvokedMethod(methodSymbol, methodType, lastArg);
}
}
use of org.sonar.java.resolve.MethodJavaType 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.java.resolve.MethodJavaType in project sonar-java by SonarSource.
the class RawExceptionCheck method visitThrowStatement.
@Override
public void visitThrowStatement(ThrowStatementTree tree) {
if (tree.expression().is(Tree.Kind.NEW_CLASS)) {
TypeTree exception = ((NewClassTree) tree.expression()).identifier();
Type symbolType = exception.symbolType();
if (symbolType instanceof MethodJavaType) {
symbolType = ((MethodJavaType) exception.symbolType()).resultType();
}
if (isRawException(symbolType)) {
reportIssue(exception);
}
}
super.visitThrowStatement(tree);
}
Aggregations