Search in sources :

Example 51 with IdentifierTree

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

the class MutableMembersUsageCheck method assignementsOfMutableType.

private static boolean assignementsOfMutableType(List<IdentifierTree> usages) {
    for (IdentifierTree usage : usages) {
        Tree current = usage;
        Tree parent = usage.parent();
        do {
            if (parent.is(Tree.Kind.ASSIGNMENT)) {
                break;
            }
            current = parent;
            parent = current.parent();
        } while (parent != null);
        if (parent != null) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) parent;
            if (assignment.variable().equals(current) && isMutableType(assignment.expression())) {
                return true;
            }
        }
    }
    return false;
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Example 52 with IdentifierTree

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

the class LeftCurlyBraceBaseTreeVisitor method getLastTokenFromSignature.

@CheckForNull
private static SyntaxToken getLastTokenFromSignature(ClassTree classTree) {
    List<TypeTree> superInterfaces = classTree.superInterfaces();
    if (!superInterfaces.isEmpty()) {
        return getIdentifierToken(Iterables.getLast(superInterfaces));
    }
    TypeTree superClass = classTree.superClass();
    if (superClass != null) {
        return getIdentifierToken(superClass);
    }
    TypeParameters typeParameters = classTree.typeParameters();
    if (!typeParameters.isEmpty()) {
        return typeParameters.closeBracketToken();
    }
    IdentifierTree simpleName = classTree.simpleName();
    if (simpleName != null) {
        return simpleName.identifierToken();
    }
    // enum constants and new class trees are handled separately
    return null;
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull)

Example 53 with IdentifierTree

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

the class MethodParametersOrderCheck method matchingTypesWrongOrder.

private static boolean matchingTypesWrongOrder(ParametersList formalParameterList, List<IdentifierTree> argumentList) {
    Iterator<IdentifierTree> argumentsIterator = argumentList.stream().filter(Objects::nonNull).iterator();
    int countArgumentsNotOrdered = 0;
    while (argumentsIterator.hasNext()) {
        IdentifierTree argument = argumentsIterator.next();
        int index = formalParameterList.indexOf(argument.name().toLowerCase(Locale.ENGLISH));
        Type formalType = formalParameterList.typeOfIndex(index);
        Type argType = argument.symbolType();
        if (!formalType.is(argType.fullyQualifiedName()) || formalType.isUnknown() || argType.isUnknown()) {
            return false;
        }
        if (argumentList.indexOf(argument) != index) {
            countArgumentsNotOrdered++;
        }
    }
    return countArgumentsNotOrdered >= 2;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 54 with IdentifierTree

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

the class OutputStreamOverrideWriteCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    Type superType = classTree.symbol().superClass();
    IdentifierTree className = classTree.simpleName();
    if (className == null || classTree.symbol().isAbstract() || superType == null || !(superType.is("java.io.OutputStream") || superType.is("java.io.FilterOutputStream"))) {
        return;
    }
    Optional<MethodTree> writeByteIntInt = findMethod(classTree, WRITE_BYTES_INT_INT);
    Optional<MethodTree> writeInt = findMethod(classTree, WRITE_INT);
    if (!writeByteIntInt.isPresent()) {
        String message = "Provide an override of \"write(byte[],int,int)\" for this class.";
        if (writeInt.isPresent()) {
            MethodTree writeIntTree = writeInt.get();
            if (writeIntTree.block().body().isEmpty()) {
                message = "Provide an empty override of \"write(byte[],int,int)\" for this class as well.";
            }
        }
        reportIssue(className, message);
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 55 with IdentifierTree

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

the class ParameterReassignedToCheck method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    ExpressionTree variable = tree.variable();
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) variable;
        Symbol reference = identifier.symbol();
        if (reference.isVariableSymbol() && variables.contains(reference)) {
            context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\".");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7