Search in sources :

Example 1 with ModifiersTree

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

Example 2 with ModifiersTree

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

the class ClassVariableVisibilityCheck method visitVariable.

@Override
public void visitVariable(VariableTree tree) {
    ModifiersTree modifiers = tree.modifiers();
    List<AnnotationTree> annotations = modifiers.annotations();
    if (isClass() && isPublic(modifiers) && !(isFinal(modifiers) || !annotations.isEmpty())) {
        context.reportIssue(this, tree.simpleName(), "Make " + tree.simpleName() + " a static final constant or non-public and provide accessors if needed.");
    }
    super.visitVariable(tree);
}
Also used : ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree)

Example 3 with ModifiersTree

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

Aggregations

ModifiersTree (org.sonar.plugins.java.api.tree.ModifiersTree)3 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 ModifierKeywordTree (org.sonar.plugins.java.api.tree.ModifierKeywordTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 PrimitiveTypeTree (org.sonar.plugins.java.api.tree.PrimitiveTypeTree)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1