use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ReplaceLambdaByMethodRefCheck method hasNonFinalFieldInMethodSelect.
private static boolean hasNonFinalFieldInMethodSelect(MethodInvocationTree mit) {
MemberSelectExpressionTree mse = getMemberSelect(mit);
if (mse == null) {
return false;
}
ExpressionTree expression = ExpressionUtils.skipParentheses(mse.expression());
Symbol symbol = null;
if (expression.is(Tree.Kind.IDENTIFIER)) {
symbol = ((IdentifierTree) expression).symbol();
} else if (expression.is(Tree.Kind.MEMBER_SELECT)) {
symbol = ((MemberSelectExpressionTree) expression).identifier().symbol();
}
return symbol != null && symbol.owner().isTypeSymbol() && !isThisOrSuper(symbol.name()) && !symbol.isFinal();
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ReplaceLambdaByMethodRefCheck method getNullCheck.
private static Optional<String> getNullCheck(@Nullable Tree statement, LambdaExpressionTree tree) {
if (statement == null) {
return Optional.empty();
}
Tree expr = statement;
if (expr.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) {
expr = ExpressionUtils.skipParentheses((ParenthesizedTree) statement);
}
if (expr.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
BinaryExpressionTree bet = (BinaryExpressionTree) expr;
ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
if (nullAgainstParam(leftOperand, rightOperand, tree) || nullAgainstParam(rightOperand, leftOperand, tree)) {
return Optional.of(expr.is(Tree.Kind.EQUAL_TO) ? "isNull" : "nonNull");
}
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class RedundantStreamCollectCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree collectMIT) {
ExpressionTree argument = collectMIT.arguments().get(0);
if (argument.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) argument;
REPLACEMENTS.entrySet().stream().filter(e -> e.getKey().matches(methodInvocation)).findFirst().ifPresent(e -> context.reportIssue(this, ExpressionUtils.methodName(methodInvocation), "Use \"" + e.getValue() + "\" instead."));
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method requiredForMemberAccess.
private static boolean requiredForMemberAccess(TypeCastTree typeCastTree) {
ExpressionTree expression = typeCastTree.expression();
if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
Tree parent = typeCastTree.parent();
return expression.is(Tree.Kind.METHOD_REFERENCE) && parent != null && skipParentheses(parent).is(Tree.Kind.MEMBER_SELECT);
}
Symbol symbol = ((MethodInvocationTree) expression).symbol();
if (!symbol.isMethodSymbol()) {
return false;
}
Type returnType = ((Symbol.MethodSymbol) symbol).returnType().type();
if (!(returnType instanceof TypeVariableJavaType) || ((TypeVariableJavaType) returnType).bounds().get(0).is("java.lang.Object")) {
return false;
}
// as the member accessed could have also been part of initial type
return skipParentheses(typeCastTree.parent()).is(Tree.Kind.MEMBER_SELECT);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class StringLiteralDuplicatedCheck method visitVariable.
@Override
public void visitVariable(VariableTree tree) {
ExpressionTree initializer = tree.initializer();
if (initializer != null && initializer.is(Tree.Kind.STRING_LITERAL) && ModifiersUtils.hasModifier(tree.modifiers(), Modifier.STATIC) && ModifiersUtils.hasModifier(tree.modifiers(), Modifier.FINAL)) {
constants.putIfAbsent(((LiteralTree) initializer).value(), tree);
return;
}
super.visitVariable(tree);
}
Aggregations