use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class ClassComparedByNameCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
List<ExpressionTree> expressionsToCheck = new ArrayList<>(2);
if (mit.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
expressionsToCheck.add(((MemberSelectExpressionTree) mit.methodSelect()).expression());
}
expressionsToCheck.add(mit.arguments().get(0));
boolean useAssignableMessage = expressionsToCheck.size() > 1;
boolean useClassGetName = false;
boolean useStackTraceElementGetClassName = false;
for (ExpressionTree expression : expressionsToCheck) {
if (expression.is(Tree.Kind.IDENTIFIER) && isParam(((IdentifierTree) expression).symbol())) {
// exclude comparison to method parameters
return;
}
ClassGetNameDetector visitor = new ClassGetNameDetector();
expression.accept(visitor);
useAssignableMessage &= visitor.useClassGetName;
useClassGetName |= visitor.useClassGetName;
useStackTraceElementGetClassName |= visitor.useStackTraceElementGetClassName;
}
if (useClassGetName && !useStackTraceElementGetClassName) {
String message = "Use an \"instanceof\" comparison instead.";
if (useAssignableMessage) {
message = "Use \"isAssignableFrom\" instead.";
}
reportIssue(mit, message);
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class DataStoredInSessionCheck method checkArgument.
private void checkArgument(ExpressionTree argument, ExpressionTree startPoint, MethodInvocationTree reportTree) {
ExpressionTree expressionToEvaluate = argument;
if (expressionToEvaluate.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifier = (IdentifierTree) expressionToEvaluate;
identifiersUsedToSetAttribute.add(identifier);
Symbol variable = identifier.symbol();
ExpressionTree lastAssignmentOrDeclaration = ReassignmentFinder.getClosestReassignmentOrDeclarationExpression(startPoint, variable);
if (lastAssignmentOrDeclaration != null && !usedBetween(variable, lastAssignmentOrDeclaration, startPoint)) {
expressionToEvaluate = lastAssignmentOrDeclaration;
}
}
if (isRequestOrCookieDataRetrieval(expressionToEvaluate)) {
reportIssue(reportTree.methodSelect(), "Make sure the user is authenticated before this data is stored in the session.");
} else if (expressionToEvaluate.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expressionToEvaluate;
if (NO_EFFECT_OPERATION.anyMatch(mit)) {
checkArgument(mit.arguments().get(0), mit, reportTree);
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class CollectionCallingItselfCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (hasSemantic()) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
Symbol symbolReference = null;
Symbol method = null;
String reportedName = "";
if (methodInvocationTree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mse = (MemberSelectExpressionTree) methodInvocationTree.methodSelect();
IdentifierTree identifier = mse.identifier();
reportedName = identifier.name();
method = identifier.symbol();
if (mse.expression().is(Tree.Kind.IDENTIFIER)) {
symbolReference = ((IdentifierTree) mse.expression()).symbol();
}
}
if (symbolReference != null && isMethodFromCollection(method)) {
reportIssueForParameters(methodInvocationTree, symbolReference, reportedName);
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class CollectionMethodsWithLinearComplexityCheck method usageInAssignment.
private static Stream<AssignmentExpressionTree> usageInAssignment(IdentifierTree usage) {
Tree prevParent = usage;
Tree parent = usage.parent();
while (parent != null && !parent.is(Tree.Kind.ASSIGNMENT) && parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.IDENTIFIER)) {
prevParent = parent;
parent = parent.parent();
}
if (parent != null && parent.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) parent;
if (assignment.variable().equals(prevParent)) {
return Stream.of(assignment);
}
}
return Stream.empty();
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class CloneOverrideCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
IdentifierTree identifierTree = methodTree.simpleName();
if (methodTree.parameters().isEmpty() && "clone".equals(identifierTree.name()) && !isUnsupportedCloneOverride(methodTree)) {
reportIssue(identifierTree, "Remove this \"clone\" implementation; use a copy constructor or copy factory instead.");
}
}
Aggregations