Search in sources :

Example 1 with ModifierKeywordTree

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

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

the class JavaTreeModelTest method class_declaration.

/*
   * 8. Classes
   */
@Test
public void class_declaration() {
    ClassTree tree = firstType("public class T<U> extends C implements I1, I2 { }");
    assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
    List<ModifierKeywordTree> modifiers = tree.modifiers().modifiers();
    assertThat(modifiers).hasSize(1);
    assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
    assertThat(tree.simpleName().name()).isEqualTo("T");
    TypeParameters typeParameters = tree.typeParameters();
    assertThat(typeParameters).isNotEmpty();
    assertThat(typeParameters.separators()).isEmpty();
    assertThatChildrenIteratorHasSize(typeParameters, 3);
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.superClass()).isNotNull();
    assertThat(tree.superInterfaces()).hasSize(2);
    assertThat(tree.superInterfaces().separators()).hasSize(1);
    assertThat(tree.superInterfaces().separators().get(0).text()).isEqualTo(",");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("public class T { }");
    modifiers = tree.modifiers().modifiers();
    assertThat(modifiers).hasSize(1);
    assertThat(modifiers.get(0).modifier()).isEqualTo(Modifier.PUBLIC);
    assertThat(modifiers.get(0).keyword().text()).isEqualTo("public");
    assertThat(tree.simpleName().name()).isEqualTo("T");
    assertThat(tree.typeParameters()).isEmpty();
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("class T<U,V> { }");
    assertThat(tree.modifiers()).isEmpty();
    assertThat(tree.simpleName().name()).isEqualTo("T");
    typeParameters = tree.typeParameters();
    assertThat(typeParameters).hasSize(2);
    assertThat(typeParameters.separators()).hasSize(1);
    assertThatChildrenIteratorHasSize(typeParameters, 5);
    assertThat(tree.superClass()).isNull();
    assertThat(tree.superInterfaces()).isEmpty();
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
    tree = firstType("@Deprecated class T { }");
    assertThat(tree.is(Tree.Kind.CLASS)).isTrue();
    assertThat(tree.modifiers().annotations()).hasSize(1);
    assertThat(tree.declarationKeyword().text()).isEqualTo("class");
}
Also used : ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 3 with ModifierKeywordTree

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

the class ModifiersOrderCheck method getFirstBadlyOrdered.

private static ModifierTree getFirstBadlyOrdered(ModifiersTree modifiersTree) {
    ListIterator<ModifierTree> modifiersIterator = modifiersTree.listIterator();
    skipAnnotations(modifiersIterator);
    Modifier[] modifiers = Modifier.values();
    int modifierIndex = 0;
    while (modifiersIterator.hasNext()) {
        ModifierTree modifier = modifiersIterator.next();
        if (modifier.is(Kind.ANNOTATION)) {
            break;
        }
        ModifierKeywordTree mkt = (ModifierKeywordTree) modifier;
        for (; modifierIndex < modifiers.length && !mkt.modifier().equals(modifiers[modifierIndex]); modifierIndex++) {
        // We're just interested in the final value of modifierIndex
        }
        if (modifierIndex == modifiers.length) {
            return modifier;
        }
    }
    return testOnlyAnnotationsAreLeft(modifiersIterator);
}
Also used : ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) Modifier(org.sonar.plugins.java.api.tree.Modifier)

Example 4 with ModifierKeywordTree

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

the class NestedEnumStaticCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    ModifierKeywordTree staticKeyword = ModifiersUtils.getModifier(classTree.modifiers(), Modifier.STATIC);
    if (staticKeyword != null) {
        List<JavaFileScannerContext.Location> secondary = Collections.singletonList(new JavaFileScannerContext.Location("", classTree.declarationKeyword()));
        reportIssue(staticKeyword, "Remove this redundant \"static\" qualifier.", secondary, null);
    }
}
Also used : ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 5 with ModifierKeywordTree

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

the class PublicStaticFieldShouldBeFinalCheck method isPublicStaticNotFinal.

private static boolean isPublicStaticNotFinal(VariableTree tree) {
    boolean isPublic = false;
    boolean isStatic = false;
    boolean isFinal = false;
    for (ModifierKeywordTree modifierKeywordTree : tree.modifiers().modifiers()) {
        Modifier modifier = modifierKeywordTree.modifier();
        if (modifier == Modifier.PUBLIC) {
            isPublic = true;
        } else if (modifier == Modifier.STATIC) {
            isStatic = true;
        } else if (modifier == Modifier.FINAL) {
            isFinal = true;
        }
    }
    return isPublic && isStatic && !isFinal;
}
Also used : ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) Modifier(org.sonar.plugins.java.api.tree.Modifier)

Aggregations

ModifierKeywordTree (org.sonar.plugins.java.api.tree.ModifierKeywordTree)6 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)3 Modifier (org.sonar.plugins.java.api.tree.Modifier)3 Test (org.junit.Test)1 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)1 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 ModifierTree (org.sonar.plugins.java.api.tree.ModifierTree)1 ModifiersTree (org.sonar.plugins.java.api.tree.ModifiersTree)1 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)1 PrimitiveTypeTree (org.sonar.plugins.java.api.tree.PrimitiveTypeTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 TypeParameters (org.sonar.plugins.java.api.tree.TypeParameters)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1