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.");
}
}
}
}
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");
}
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);
}
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);
}
}
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;
}
Aggregations