use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class CatchUsesExceptionWithContextCheck method visitMemberSelectExpression.
@Override
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
IdentifierTree identifier = null;
ExpressionTree expression = tree.expression();
if (expression.is(Kind.IDENTIFIER)) {
identifier = (IdentifierTree) expression;
} else if (expression.is(Kind.PARENTHESIZED_EXPRESSION) && ((ParenthesizedTree) expression).expression().is(Kind.IDENTIFIER)) {
identifier = (IdentifierTree) ((ParenthesizedTree) expression).expression();
}
if (!validUsagesStack.isEmpty() && identifier != null) {
Iterator<Collection<IdentifierTree>> iterator = validUsagesStack.iterator();
while (iterator.hasNext()) {
iterator.next().remove(identifier);
}
}
super.visitMemberSelectExpression(tree);
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree 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.MemberSelectExpressionTree 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.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class CollectionInappropriateCallsCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree tree) {
ExpressionTree firstArgument = tree.arguments().get(0);
Type argumentType = firstArgument.symbolType();
if (argumentType.isUnknown()) {
// could happen with type inference.
return;
}
Type collectionType = getMethodOwner(tree);
// can be null when using raw types
Type collectionParameterType = getTypeParameter(collectionType);
boolean isCallToParametrizedOrUnknownMethod = isCallToParametrizedOrUnknownMethod(firstArgument);
if (!isCallToParametrizedOrUnknownMethod && tree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
isCallToParametrizedOrUnknownMethod = isCallToParametrizedOrUnknownMethod(((MemberSelectExpressionTree) tree.methodSelect()).expression());
}
if (collectionParameterType != null && !collectionParameterType.isUnknown() && !isCallToParametrizedOrUnknownMethod && !isArgumentCompatible(argumentType, collectionParameterType)) {
reportIssue(ExpressionUtils.methodName(tree), MessageFormat.format("A \"{0}<{1}>\" cannot contain a \"{2}\"", collectionType, collectionParameterType, argumentType));
}
}
use of org.sonar.plugins.java.api.tree.MemberSelectExpressionTree in project sonar-java by SonarSource.
the class FlowComputation method getArgumentIdentifier.
@CheckForNull
public static IdentifierTree getArgumentIdentifier(MethodInvocationTree mit, int index) {
Arguments arguments = mit.arguments();
if (index < 0 || index > arguments.size()) {
throw new IllegalArgumentException("index must be within arguments range.");
}
ExpressionTree expr = ExpressionUtils.skipParentheses(arguments.get(index));
switch(expr.kind()) {
case MEMBER_SELECT:
return ((MemberSelectExpressionTree) expr).identifier();
case IDENTIFIER:
return ((IdentifierTree) expr);
default:
return null;
}
}
Aggregations