use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class PublicStaticMutableMembersCheck method checkAssignment.
private void checkAssignment(AssignmentExpressionTree node) {
ExpressionTree variable = ExpressionUtils.skipParentheses(node.variable());
if (variable.is(Tree.Kind.MEMBER_SELECT)) {
variable = ((MemberSelectExpressionTree) variable).identifier();
}
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifierTree = (IdentifierTree) variable;
Symbol symbol = identifierTree.symbol();
if (IMMUTABLE_CANDIDATES.contains(symbol) && isMutable(node.expression(), symbol)) {
reportIssue(identifierTree, "Make member \"" + symbol.name() + "\" \"protected\".");
IMMUTABLE_CANDIDATES.remove(symbol);
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class PreferStreamAnyMatchCheck method handleAnyMatch.
private void handleAnyMatch(MethodInvocationTree anyMatchMIT) {
ExpressionTree predicate = anyMatchMIT.arguments().get(0);
IdentifierTree reportTree = ExpressionUtils.methodName(anyMatchMIT);
if (anyMatchMIT.parent().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
if (predicate.is(Tree.Kind.LAMBDA_EXPRESSION) && ((LambdaExpressionTree) predicate).body().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
// !stream.anyMatch(x -> !(...))
context.reportIssue(this, reportTree, "Replace this double negation with \"allMatch()\" and positive predicate.");
} else {
context.reportIssue(this, reportTree, "Replace this negation and \"anyMatch()\" with \"noneMatch()\".");
}
}
if (predicate.is(Tree.Kind.METHOD_REFERENCE) && isBooleanValueReference((MethodReferenceTree) predicate)) {
previousMITInChain(anyMatchMIT).filter(MAP_METHODS::anyMatch).ifPresent(mapMIT -> context.reportIssue(this, reportTree, "Use mapper from \"map()\" directly as predicate in \"anyMatch()\"."));
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class PrintStackTraceCalledWithoutArgumentCheck method visitMethodInvocation.
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
super.visitMethodInvocation(tree);
if (tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) tree.methodSelect();
IdentifierTree identifierTree = memberSelectExpressionTree.identifier();
if (!enclosingClassExtendsThrowable() && "printStackTrace".equals(identifierTree.name()) && calledOnTypeInheritedFromThrowable(memberSelectExpressionTree.expression()) && tree.arguments().isEmpty()) {
context.reportIssue(this, identifierTree, "Use a logger to log this exception.");
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class NullDereferenceInConditionalCheck method visitBinaryExpression.
@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
if (isAndWithNullComparison(tree) || isOrWithNullExclusion(tree)) {
ExpressionTree nonNullOperand = getNonNullOperand(tree.leftOperand());
IdentifierTree identifierTree = getIdentifier(nonNullOperand);
if (identifierTree != null) {
IdentifierVisitor visitor = new IdentifierVisitor(identifierTree);
tree.rightOperand().accept(visitor);
if (visitor.raiseIssue) {
context.reportIssue(this, tree, "Either reverse the equality operator in the \"" + identifierTree.name() + "\" null test, or reverse the logical operator that follows it.");
}
}
}
super.visitBinaryExpression(tree);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class MutableMembersUsageCheck method visitAssignmentExpression.
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
super.visitAssignmentExpression(tree);
if (!isMutableType(tree.expression())) {
return;
}
ExpressionTree variable = tree.variable();
Symbol leftSymbol = null;
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifierTree = (IdentifierTree) variable;
leftSymbol = identifierTree.symbol();
} else if (variable.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mit = (MemberSelectExpressionTree) variable;
leftSymbol = mit.identifier().symbol();
}
if (leftSymbol != null && leftSymbol.isPrivate()) {
checkStore(tree.expression());
}
}
Aggregations