use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class ParameterReassignedToCheck method visitAssignmentExpression.
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
ExpressionTree variable = tree.variable();
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifier = (IdentifierTree) variable;
Symbol reference = identifier.symbol();
if (reference.isVariableSymbol() && variables.contains(reference)) {
context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\".");
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class LeastSpecificTypeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
if (!methodSymbol.isPublic() || !Boolean.FALSE.equals(methodTree.isOverriding()) || isOverloaded(methodSymbol)) {
return;
}
boolean springInjectionAnnotated = isSpringInjectionAnnotated(methodSymbol.metadata());
methodTree.parameters().stream().map(VariableTree::symbol).filter(p -> p.type().isClass() && !p.type().symbol().isEnum() && !p.type().is("java.lang.String")).filter(p -> !(springInjectionAnnotated && p.type().is("java.util.Collection"))).forEach(p -> handleParameter(p, springInjectionAnnotated));
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class MainInServletCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree node = (ClassTree) tree;
Symbol.TypeSymbol symbol = node.symbol();
if (isServletOrEjb(symbol)) {
for (Tree member : node.members()) {
if (member.is(Tree.Kind.METHOD) && ((MethodTreeImpl) member).isMainMethod()) {
reportIssue(((MethodTree) member).simpleName(), "Remove this unwanted \"main\" method.");
}
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class VariableReadExtractor method visitIdentifier.
@Override
public void visitIdentifier(IdentifierTree tree) {
Symbol owner = tree.symbol().owner();
if (methodSymbol.equals(owner) || (includeFields && isField(tree.symbol(), methodSymbol.owner()))) {
used.add(tree.symbol());
}
super.visitIdentifier(tree);
}
use of org.sonar.plugins.java.api.semantic.Symbol 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));
}
}
}
Aggregations