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