Search in sources :

Example 26 with TypeTree

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

the class EnumMutableFieldCheck method isSetter.

private static boolean isSetter(MethodTree methodTree) {
    TypeTree returnType = methodTree.returnType();
    BlockTree block = methodTree.block();
    boolean returnsVoid = returnType.is(Tree.Kind.PRIMITIVE_TYPE) && "void".equals(((PrimitiveTypeTree) returnType).keyword().text());
    boolean hasAtLeastOneStatement = block == null || !block.body().isEmpty();
    return methodTree.simpleName().name().startsWith("set") && methodTree.parameters().size() == 1 && returnsVoid && hasAtLeastOneStatement;
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 27 with TypeTree

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

the class ErrorClassExtendedCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    TypeTree superClass = tree.superClass();
    if (tree.is(Tree.Kind.CLASS) && superClass != null) {
        if (superClass.is(Tree.Kind.IDENTIFIER)) {
            IdentifierTree idt = (IdentifierTree) superClass;
            if ("Error".equals(idt.name())) {
                context.reportIssue(this, superClass, "Extend \"java.lang.Exception\" or one of its subclasses.");
            }
        } else if (superClass.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) superClass;
            if ("Error".equals(mse.identifier().name()) && isJavaLang(mse.expression())) {
                context.reportIssue(this, superClass, "Extend \"java.lang.Exception\" or one of its subclasses.");
            }
        }
    }
    super.visitClass(tree);
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 28 with TypeTree

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

the class UselessExtendsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    checkExtendsObject(classTree);
    ListTree<TypeTree> superInterfaces = classTree.superInterfaces();
    if (superInterfaces.isEmpty()) {
        return;
    }
    Set<ClassJavaType> superTypes = ((JavaSymbol.TypeJavaSymbol) classTree.symbol()).superTypes();
    List<Type> superInterfacesTypes = getTypes(superInterfaces);
    Set<String> reportedNames = new HashSet<>();
    for (TypeTree superInterface : superInterfaces) {
        String superInterfaceName = extractInterfaceName(superInterface);
        if (isDuplicate(superInterfaces, superInterface) && !reportedNames.add(superInterfaceName)) {
            // add an issue on a duplicated interface the second time it is encountered
            reportIssue(superInterface, "\"" + superInterfaceName + "\" is listed multiple times.");
        }
        if (!superInterface.symbolType().isUnknown()) {
            checkRedundancy(superInterface, superInterfacesTypes, superTypes);
        }
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Type(org.sonar.plugins.java.api.semantic.Type) ClassJavaType(org.sonar.java.resolve.ClassJavaType) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ClassJavaType(org.sonar.java.resolve.ClassJavaType) HashSet(java.util.HashSet)

Example 29 with TypeTree

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

the class TreeFactory method basicClassExpression.

public ExpressionTree basicClassExpression(PrimitiveTypeTreeImpl basicType, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> dimensions, InternalSyntaxToken dotToken, InternalSyntaxToken classToken) {
    // 15.8.2. Class Literals
    // int.class
    // int[].class
    IdentifierTreeImpl classIdentifier = new IdentifierTreeImpl(classToken);
    ArrayTypeTreeImpl nestedDimensions = newArrayTypeTree(dimensions);
    TypeTree typeTree = applyDim(basicType, nestedDimensions);
    return new MemberSelectExpressionTreeImpl((ExpressionTree) typeTree, dotToken, classIdentifier);
}
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) ArrayTypeTreeImpl(org.sonar.java.model.JavaTree.ArrayTypeTreeImpl) MemberSelectExpressionTreeImpl(org.sonar.java.model.expression.MemberSelectExpressionTreeImpl)

Example 30 with TypeTree

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

the class TreeFactory method newMethodOrConstructor.

private static MethodTreeImpl newMethodOrConstructor(Optional<TypeTree> type, InternalSyntaxToken identifierToken, FormalParametersListTreeImpl parameters, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> annotatedDimensions, Optional<Tuple<InternalSyntaxToken, QualifiedIdentifierListTreeImpl>> throwsClause, JavaTree blockOrSemicolon) {
    IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
    ArrayTypeTreeImpl nestedDimensions = newArrayTypeTreeWithAnnotations(annotatedDimensions);
    TypeTree actualType;
    if (type.isPresent()) {
        actualType = applyDim(type.get(), nestedDimensions);
    } else {
        actualType = null;
    }
    BlockTreeImpl block = null;
    InternalSyntaxToken semicolonToken = null;
    if (blockOrSemicolon.is(Tree.Kind.BLOCK)) {
        block = (BlockTreeImpl) blockOrSemicolon;
    } else {
        semicolonToken = (InternalSyntaxToken) blockOrSemicolon;
    }
    InternalSyntaxToken throwsToken = null;
    ListTree<TypeTree> throwsClauses = QualifiedIdentifierListTreeImpl.emptyList();
    if (throwsClause.isPresent()) {
        throwsToken = throwsClause.get().first();
        throwsClauses = throwsClause.get().second();
    }
    return new MethodTreeImpl(actualType, identifier, parameters, throwsToken, throwsClauses, block, semicolonToken);
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) BlockTreeImpl(org.sonar.java.model.statement.BlockTreeImpl) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) ArrayTypeTreeImpl(org.sonar.java.model.JavaTree.ArrayTypeTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

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