Search in sources :

Example 1 with VariableTree

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

the class LambdaOptionalParenthesisCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LambdaExpressionTree let = (LambdaExpressionTree) tree;
    SyntaxToken openParenToken = let.openParenToken();
    if (openParenToken != null && let.parameters().size() == 1) {
        VariableTree param = let.parameters().get(0);
        String identifier = param.simpleName().name();
        if (param.type().is(Tree.Kind.INFERED_TYPE)) {
            reportIssue(openParenToken, "Remove the parentheses around the \"" + identifier + "\" parameter" + context.getJavaVersion().java8CompatibilityMessage());
        }
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 2 with VariableTree

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

the class HiddenFieldCheck method isVariableHidingField.

private void isVariableHidingField(VariableTree variableTree) {
    for (ImmutableMap<String, VariableTree> variables : fields) {
        if (variables.values().contains(variableTree)) {
            return;
        }
        String identifier = variableTree.simpleName().name();
        VariableTree hiddenVariable = variables.get(identifier);
        if (!flattenExcludedVariables.contains(variableTree) && hiddenVariable != null && !isInStaticInnerClass(hiddenVariable, variableTree)) {
            int line = hiddenVariable.firstToken().line();
            reportIssue(variableTree.simpleName(), "Rename \"" + identifier + "\" which hides the field declared at line " + line + ".");
            return;
        }
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 3 with VariableTree

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

the class HiddenFieldCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (isClassTree(tree)) {
        ClassTree classTree = (ClassTree) tree;
        ImmutableMap.Builder<String, VariableTree> builder = ImmutableMap.builder();
        for (Tree member : classTree.members()) {
            if (member.is(Tree.Kind.VARIABLE)) {
                VariableTree variableTree = (VariableTree) member;
                builder.put(variableTree.simpleName().name(), variableTree);
            }
        }
        fields.push(builder.build());
        excludedVariables.push(Lists.<VariableTree>newArrayList());
    } else if (tree.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) tree;
        isVariableHidingField(variableTree);
    } else if (tree.is(Tree.Kind.STATIC_INITIALIZER)) {
        excludeVariablesFromBlock((BlockTree) tree);
    } else {
        MethodTree methodTree = (MethodTree) tree;
        excludedVariables.peek().addAll(methodTree.parameters());
        flattenExcludedVariables.addAll(methodTree.parameters());
        if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.STATIC)) {
            excludeVariablesFromBlock(methodTree.block());
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with VariableTree

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

the class EnumSetCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    VariableTree variableTree = (VariableTree) tree;
    ExpressionTree initializer = variableTree.initializer();
    if (initializer == null) {
        return;
    }
    if (initializer.is(Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) initializer;
        if (COLLECTIONS_UNMODIFIABLE.matches(mit)) {
            // check the collection used as parameter
            initializer = mit.arguments().get(0);
        } else if (!SET_CREATION_METHODS.anyMatch(mit) || "immutableEnumSet".equals(mit.symbol().name())) {
            // but discard any other method invocations (killing the noise)
            return;
        }
    }
    checkIssue(initializer.symbolType(), initializer, variableTree.type());
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 5 with VariableTree

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

the class EnumMutableFieldCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree enumTree = (ClassTree) tree;
    for (Tree member : enumTree.members()) {
        if (member.is(Tree.Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) member;
            ModifiersTree modifiers = variableTree.modifiers();
            ModifierKeywordTree publicModifier = ModifiersUtils.getModifier(modifiers, Modifier.PUBLIC);
            if (publicModifier != null && (isNotStaticOrFinal(variableTree.modifiers()) || isMutableFinalMember(variableTree))) {
                reportIssue(publicModifier, "Lower the visibility of this field.");
            }
        } else if (member.is(Tree.Kind.METHOD)) {
            MethodTree methodTree = (MethodTree) member;
            ModifierKeywordTree publicModifier = ModifiersUtils.getModifier(methodTree.modifiers(), Modifier.PUBLIC);
            if (publicModifier != null && isSetter(methodTree)) {
                reportIssue(publicModifier, "Lower the visibility of this setter or remove it altogether.");
            }
        }
    }
}
Also used : ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) 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) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) 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)

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