Search in sources :

Example 36 with VariableTree

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

the class ReplaceLambdaByMethodRefCheck method isMethodInvocation.

private static boolean isMethodInvocation(@Nullable Tree tree, LambdaExpressionTree lambdaTree) {
    if (tree != null && tree.is(Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS)) {
        Arguments arguments;
        if (tree.is(Tree.Kind.NEW_CLASS)) {
            if (((NewClassTree) tree).classBody() != null) {
                return false;
            }
            arguments = ((NewClassTree) tree).arguments();
        } else {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            if (hasMethodInvocationInMethodSelect(mit) || hasNonFinalFieldInMethodSelect(mit)) {
                return false;
            }
            arguments = mit.arguments();
        }
        List<VariableTree> parameters = lambdaTree.parameters();
        return matchingParameters(parameters, arguments) || (arguments.isEmpty() && isNoArgMethodInvocationFromLambdaParam(tree, parameters));
    }
    return false;
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 37 with VariableTree

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

the class ServletInstanceFieldCheck method reportIssuesOnVariable.

private void reportIssuesOnVariable() {
    issuableVariables.removeAll(excludedVariables);
    for (VariableTree variable : issuableVariables) {
        reportIssue(variable.simpleName(), "Remove this misleading mutable servlet instance field or make it \"static\" and/or \"final\"");
    }
    issuableVariables.clear();
    excludedVariables.clear();
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 38 with VariableTree

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

the class SecureCookieCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) tree;
            addToUnsecuredCookies(variableTree);
        } else if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            checkSecureCall(mit);
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 39 with VariableTree

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

the class RedundantModifierCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    for (Tree member : classTree.members()) {
        if (member.is(Tree.Kind.METHOD)) {
            MethodTree methodTree = (MethodTree) member;
            ModifiersTree modifiers = methodTree.modifiers();
            if (isInterfaceOrAnnotation(tree)) {
                checkRedundantModifier(modifiers, Modifier.ABSTRACT);
                checkRedundantModifier(modifiers, Modifier.PUBLIC);
            } else if (ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.FINAL)) {
                checkRedundantModifier(modifiers, Modifier.FINAL);
            }
        } else if (member.is(Tree.Kind.VARIABLE) && isInterfaceOrAnnotation(tree)) {
            VariableTree variableTree = (VariableTree) member;
            ModifiersTree modifiers = variableTree.modifiers();
            checkRedundantModifier(modifiers, Modifier.PUBLIC);
            checkRedundantModifier(modifiers, Modifier.STATIC);
            checkRedundantModifier(modifiers, Modifier.FINAL);
        } else if (member.is(Kind.CONSTRUCTOR) && tree.is(Kind.ENUM)) {
            checkRedundantModifier(((MethodTree) member).modifiers(), Modifier.PRIVATE);
        }
    }
}
Also used : ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 40 with VariableTree

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

the class StaticMultithreadedUnsafeFieldsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    VariableTree variableTree = (VariableTree) tree;
    Type type = variableTree.type().symbolType();
    if (ModifiersUtils.hasModifier(variableTree.modifiers(), Modifier.STATIC) && isForbiddenType(variableTree)) {
        if (type.isSubtypeOf(JAVA_TEXT_SIMPLE_DATE_FORMAT) && onlySynchronizedUsages((Symbol.VariableSymbol) variableTree.symbol())) {
            return;
        }
        IdentifierTree identifierTree = variableTree.simpleName();
        reportIssue(identifierTree, String.format("Make \"%s\" an instance variable.", identifierTree.name()));
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

VariableTree (org.sonar.plugins.java.api.tree.VariableTree)86 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)32 Tree (org.sonar.plugins.java.api.tree.Tree)32 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)27 Test (org.junit.Test)25 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)22 Symbol (org.sonar.plugins.java.api.semantic.Symbol)18 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)16 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 List (java.util.List)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)12 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)11 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9