use of org.sonar.plugins.java.api.tree.AnnotationTree 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);
}
use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.
the class TreeFactory method newAnnotatedParameterizedIdentifier.
public ExpressionTree newAnnotatedParameterizedIdentifier(Optional<List<AnnotationTreeImpl>> annotations, InternalSyntaxToken identifierToken, Optional<TypeArgumentListTreeImpl> typeArguments) {
List<AnnotationTree> annotationList = ImmutableList.copyOf(annotations.or(ImmutableList.of()));
ExpressionTree result = new IdentifierTreeImpl(identifierToken);
if (typeArguments.isPresent()) {
result = new ParameterizedTypeTreeImpl((TypeTree) result, typeArguments.get()).complete(annotationList);
} else {
result = ((IdentifierTreeImpl) result).complete(annotationList);
}
return result;
}
use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.
the class AnnotationDefaultArgumentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
if (annotationSymbol.isUnknown()) {
return;
}
Map<String, Object> defaultValueByName = annotationSymbol.memberSymbols().stream().filter(Symbol::isMethodSymbol).map(s -> (JavaSymbol.MethodJavaSymbol) s).filter(s -> s.defaultValue() != null).collect(Collectors.toMap(Symbol::name, JavaSymbol.MethodJavaSymbol::defaultValue));
for (ExpressionTree argument : annotationTree.arguments()) {
Tree valueSet = argument;
// Single element annotation : JLS8 9.7.3 : one param must be named value.
String paramName = "value";
if (argument.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
paramName = nameTree.name();
valueSet = assignmentTree.expression();
}
if (setValueIsSameAsDefaultValue(defaultValueByName.get(paramName), valueSet)) {
reportIssue(argument, String.format("Remove this default value assigned to parameter \"%s\".", paramName));
}
}
}
use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.
the class ChangeMethodContractCheck method checkContractChange.
private void checkContractChange(MethodTreeImpl methodTree, Symbol.MethodSymbol overridee) {
if (methodTree.isEqualsMethod() && methodTree.parameters().get(0).symbol().metadata().isAnnotatedWith(JAVAX_ANNOTATION_NONNULL)) {
reportIssue(methodTree.parameters().get(0), "Equals method should accept null parameters and return false.");
return;
}
for (int i = 0; i < methodTree.parameters().size(); i++) {
VariableTree parameter = methodTree.parameters().get(i);
Symbol overrideeParamSymbol = ((JavaSymbol.MethodJavaSymbol) overridee).getParameters().scopeSymbols().get(i);
checkParameter(parameter, overrideeParamSymbol);
}
if (nonNullVsNull(overridee, methodTree.symbol())) {
for (AnnotationTree annotationTree : methodTree.modifiers().annotations()) {
if (annotationTree.symbolType().is(JAVAX_ANNOTATION_NULLABLE) || annotationTree.symbolType().is(JAVAX_ANNOTATION_CHECK_FOR_NULL)) {
reportIssue(annotationTree, "Remove this \"" + annotationTree.symbolType().name() + "\" annotation to honor the overridden method's contract.");
}
}
}
}
use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.
the class SuppressWarningsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
List<String> ruleWarnings = getAllowedWarnings();
if (isJavaLangSuppressWarnings(annotationTree)) {
if (ruleWarnings.isEmpty()) {
reportIssue(annotationTree.annotationType(), "Suppressing warnings is not allowed");
} else {
List<String> suppressedWarnings = getSuppressedWarnings(annotationTree.arguments().get(0));
List<String> issues = suppressedWarnings.stream().filter(currentWarning -> !ruleWarnings.contains(currentWarning)).collect(Collectors.toList());
if (!issues.isEmpty()) {
StringBuilder sb = new StringBuilder("Suppressing the '").append(Joiner.on(", ").join(issues)).append("' warning").append(issues.size() > 1 ? "s" : "").append(" is not allowed");
reportIssue(annotationTree.annotationType(), sb.toString());
}
}
}
}
Aggregations