use of org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol in project sonar-java by SonarSource.
the class AnnotationArgumentOrderCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
if (annotationSymbol.isUnknown()) {
return;
}
List<String> declarationNames = new ArrayList<>();
for (Symbol symbol : annotationSymbol.memberSymbols()) {
declarationNames.add(symbol.name());
}
List<String> annotationArguments = new ArrayList<>();
for (ExpressionTree argument : annotationTree.arguments()) {
if (argument.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
annotationArguments.add(nameTree.name());
}
}
declarationNames.retainAll(annotationArguments);
if (!declarationNames.equals(annotationArguments)) {
reportIssue(annotationTree.annotationType(), "Reorder annotation arguments to match the order of declaration.");
}
}
use of org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol 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.semantic.Symbol.TypeSymbol in project sonar-java by SonarSource.
the class PrivateFieldUsedLocallyCheck method visitNode.
@Override
public void visitNode(Tree tree) {
TypeSymbol classSymbol = ((ClassTree) tree).symbol();
Set<Symbol> fieldsReadOnAnotherInstance = FieldsReadOnAnotherInstanceVisitor.getFrom(tree);
classSymbol.memberSymbols().stream().filter(PrivateFieldUsedLocallyCheck::isPrivateField).filter(s -> !(s.isFinal() && s.isStatic())).filter(s -> !hasAnnotation(s)).filter(s -> !s.usages().isEmpty()).filter(s -> !fieldsReadOnAnotherInstance.contains(s)).forEach(s -> checkPrivateField(s, classSymbol));
}
use of org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol in project sonar-java by SonarSource.
the class UselessExtendsCheck method checkRedundancy.
private void checkRedundancy(TypeTree currentInterface, List<Type> superInterfacesTypes, Set<ClassJavaType> superTypes) {
Type interfaceType = currentInterface.symbolType();
for (ClassJavaType superType : superTypes) {
TypeSymbol superTypeSymbol = superType.symbol();
if (superTypeSymbol.interfaces().contains(interfaceType)) {
String typeOfParentMsg = "implemented by a super class";
if (superTypeSymbol.isInterface() && superInterfacesTypes.contains(superType)) {
typeOfParentMsg = "already extended by \"" + superTypeSymbol.name() + "\"";
}
reportIssue(currentInterface, "\"" + interfaceType.name() + "\" is " + typeOfParentMsg + "; there is no need to implement it here.");
break;
}
}
}
Aggregations