Search in sources :

Example 11 with TypeTree

use of org.sonar.plugins.java.api.tree.TypeTree 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)

Example 12 with TypeTree

use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.

the class TreeFactory method annotationIdentifier.

public TypeTree annotationIdentifier(InternalSyntaxToken firstIdentifier, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> rests) {
    List<InternalSyntaxToken> children = Lists.newArrayList();
    children.add(firstIdentifier);
    if (rests.isPresent()) {
        for (Tuple<InternalSyntaxToken, InternalSyntaxToken> rest : rests.get()) {
            children.add(rest.first());
            children.add(rest.second());
        }
    }
    JavaTree result = null;
    InternalSyntaxToken dotToken = null;
    for (InternalSyntaxToken child : children) {
        if (!child.getGrammarRuleKey().equals(JavaTokenType.IDENTIFIER)) {
            dotToken = child;
        } else {
            InternalSyntaxToken identifierToken = child;
            if (result == null) {
                result = new IdentifierTreeImpl(identifierToken);
            } else {
                IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
                result = new MemberSelectExpressionTreeImpl((ExpressionTree) result, dotToken, identifier);
            }
        }
    }
    return (TypeTree) result;
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) MemberSelectExpressionTreeImpl(org.sonar.java.model.expression.MemberSelectExpressionTreeImpl) JavaTree(org.sonar.java.model.JavaTree)

Example 13 with TypeTree

use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.

the class DiamondOperatorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    NewClassTree newClassTree = (NewClassTree) tree;
    TypeTree newTypeTree = newClassTree.identifier();
    if (newClassTree.classBody() == null && isParameterizedType(newTypeTree)) {
        TypeTree type = getTypeFromExpression(tree.parent(), expressionKindsToCheck);
        if (type != null && isParameterizedType(type)) {
            reportIssue(((ParameterizedTypeTree) newTypeTree).typeArguments(), "Replace the type specification in this constructor call with the diamond operator (\"<>\")." + context.getJavaVersion().java7CompatibilityMessage());
        }
    }
}
Also used : ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 14 with TypeTree

use of org.sonar.plugins.java.api.tree.TypeTree 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);
        }
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) HashSet(java.util.HashSet)

Example 15 with TypeTree

use of org.sonar.plugins.java.api.tree.TypeTree in project sonar-java by SonarSource.

the class CastArithmeticOperandCheck method checkMethodTree.

private void checkMethodTree(MethodTreeImpl methodTree) {
    TypeTree returnTypeTree = methodTree.returnType();
    Type returnType = returnTypeTree != null ? returnTypeTree.symbolType() : null;
    if (returnType != null && isVarTypeErrorProne(returnType)) {
        methodTree.accept(new ReturnStatementVisitor(returnType));
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Type(org.sonar.plugins.java.api.semantic.Type)

Aggregations

TypeTree (org.sonar.plugins.java.api.tree.TypeTree)31 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)11 Type (org.sonar.plugins.java.api.semantic.Type)8 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)6 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)6 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)6 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)6 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)5 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)5 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)4 PrimitiveTypeTree (org.sonar.plugins.java.api.tree.PrimitiveTypeTree)4 UnionTypeTree (org.sonar.plugins.java.api.tree.UnionTypeTree)4 Test (org.junit.Test)3 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)3 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 HashSet (java.util.HashSet)2 CheckForNull (javax.annotation.CheckForNull)2