Search in sources :

Example 6 with TypeCastTree

use of org.sonar.plugins.java.api.tree.TypeCastTree 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);
}
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) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) 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)

Aggregations

TypeCastTree (org.sonar.plugins.java.api.tree.TypeCastTree)6 JavaType (org.sonar.java.resolve.JavaType)3 MethodJavaType (org.sonar.java.resolve.MethodJavaType)3 TypeVariableJavaType (org.sonar.java.resolve.TypeVariableJavaType)3 Type (org.sonar.plugins.java.api.semantic.Type)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 Test (org.junit.Test)2 JavaSymbol (org.sonar.java.resolve.JavaSymbol)2 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)2 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 CheckForNull (javax.annotation.CheckForNull)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1