Search in sources :

Example 1 with MethodJavaType

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;
}
Also used : TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) MethodJavaType(org.sonar.java.resolve.MethodJavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 2 with MethodJavaType

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 + "[]\".");
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 3 with MethodJavaType

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);
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 4 with MethodJavaType

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;
}
Also used : JavaType(org.sonar.java.resolve.JavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) CheckForNull(javax.annotation.CheckForNull)

Example 5 with MethodJavaType

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);
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Aggregations

MethodJavaType (org.sonar.java.resolve.MethodJavaType)5 Type (org.sonar.plugins.java.api.semantic.Type)4 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 JavaSymbol (org.sonar.java.resolve.JavaSymbol)2 TypeVariableJavaType (org.sonar.java.resolve.TypeVariableJavaType)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 NewArrayTree (org.sonar.plugins.java.api.tree.NewArrayTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 CheckForNull (javax.annotation.CheckForNull)1 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)1 JavaType (org.sonar.java.resolve.JavaType)1 ParametrizedTypeJavaType (org.sonar.java.resolve.ParametrizedTypeJavaType)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Arguments (org.sonar.plugins.java.api.tree.Arguments)1 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)1