use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class StaticFieldUpdateInConstructorCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree constructor = (MethodTree) tree;
Symbol.TypeSymbol owner = constructor.symbol().enclosingClass();
Set<Symbol> staticFields = owner.memberSymbols().stream().filter(Symbol::isVariableSymbol).filter(Symbol::isStatic).collect(Collectors.toSet());
StaticFieldUpdateVisitor visitor = new StaticFieldUpdateVisitor(staticFields);
constructor.block().accept(visitor);
visitor.assignedStaticFields().forEach(identifierTree -> {
Symbol staticField = identifierTree.symbol();
reportIssue(identifierTree, "Remove this assignment of \"" + staticField.name() + "\".", Collections.singletonList(new JavaFileScannerContext.Location("", staticField.declaration())), null);
});
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.
private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
ExpressionTree arg = null;
ExpressionTree expression = null;
if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
Symbol symbol = mit.symbol();
ExpressionTree methodSelect = mit.methodSelect();
if (mit.arguments().size() == 1) {
arg = mit.arguments().get(0);
if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
expression = ((MemberSelectExpressionTree) methodSelect).expression();
}
}
} else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
arg = equalTo.leftOperand();
expression = equalTo.rightOperand();
}
if (arg != null && expression != null) {
if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
} else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
}
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class SynchronizedFieldAssignmentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
SynchronizedStatementTree sst = (SynchronizedStatementTree) tree;
if (sst.expression().is(Kind.NEW_CLASS)) {
reportIssue(tree, "Synchronizing on a new instance is a no-op.");
return;
}
Symbol field = getField(sst.expression());
if (field != null) {
sst.block().accept(new AssignmentVisitor(field, sst.expression()));
} else {
Symbol parameter = getParam(sst.expression());
if (parameter != null) {
reportIssue(tree, String.format("\"%s\" is a method parameter, and should not be used for synchronization.", parameter.name()));
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class TooManyMethodsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
List<Tree> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && (countNonPublic || ((MethodTree) member).symbol().isPublic())).collect(Collectors.toList());
if (shouldNotReportIssue(classTree, methods)) {
return;
}
List<JavaFileScannerContext.Location> secondary = methods.stream().map(element -> new JavaFileScannerContext.Location("Method + 1", element)).collect(Collectors.toList());
String classDescription;
if (classTree.simpleName() == null) {
classDescription = "Anonymous class \"" + ((NewClassTree) classTree.parent()).identifier().symbolType().name() + "\"";
} else {
classDescription = classTree.declarationKeyword().text() + " \"" + classTree.simpleName() + "\"";
}
reportIssue(ExpressionsHelper.reportOnClassTree(classTree), String.format("%s has %d%s methods, which is greater than the %d authorized. Split it into smaller classes.", classDescription, methods.size(), countNonPublic ? "" : " public", maximumMethodThreshold), secondary, null);
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class ThreadAsRunnableArgumentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
List<ExpressionTree> arguments;
Symbol methodSymbol;
if (tree.is(Kind.NEW_CLASS)) {
NewClassTree nct = (NewClassTree) tree;
methodSymbol = nct.constructorSymbol();
arguments = nct.arguments();
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
methodSymbol = mit.symbol();
arguments = mit.arguments();
}
if (!arguments.isEmpty() && methodSymbol.isMethodSymbol()) {
checkArgumentsTypes(arguments, (MethodJavaSymbol) methodSymbol);
}
}
Aggregations