use of org.sonar.plugins.java.api.tree.MethodTree 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());
}
}
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class GettersSettersOnRightFieldCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
isGetterLike(methodTree.symbol()).ifPresent(fieldName -> checkGetter(fieldName, methodTree));
isSetterLike(methodTree.symbol()).ifPresent(fieldName -> checkSetter(fieldName, methodTree));
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class HasNextCallingNextCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
if (!hasSemantic()) {
return;
}
if (methodTree.block() != null && isHasNextMethod(methodTree)) {
hasNextBodyVisitor.setHasNextOwner(methodTree.symbol().owner());
methodTree.block().accept(hasNextBodyVisitor);
}
}
use of org.sonar.plugins.java.api.tree.MethodTree 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.MethodTree in project sonar-java by SonarSource.
the class EqualsArgumentTypeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
if (methodTree.block() != null && "equals".equals(methodTree.symbol().name()) && methodTree.parameters().size() == 1) {
Symbol parameterSymbol = methodTree.parameters().get(0).symbol();
if (parameterSymbol.type().is("java.lang.Object")) {
CastVisitor castVisitor = new CastVisitor(parameterSymbol);
methodTree.accept(castVisitor);
if (castVisitor.hasUncheckedCast) {
reportIssue(methodTree.simpleName(), "Add a type test to this method.");
}
}
}
}
Aggregations