Search in sources :

Example 11 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class FileCreateTempFileCheck method checkAndAdvanceState.

@Nullable
private State checkAndAdvanceState(MethodInvocationTree mit, State requiredState, State nextState) {
    ExpressionTree methodSelect = mit.methodSelect();
    if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        ExpressionTree expressionTree = ((MemberSelectExpressionTree) methodSelect).expression();
        if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
            Symbol symbol = ((IdentifierTree) expressionTree).symbol();
            Map<Symbol, State> symbolStateMap = symbolStack.peek();
            if (symbolStateMap != null && symbolStateMap.containsKey(symbol) && requiredState.equals(symbolStateMap.get(symbol))) {
                symbolStateMap.put(symbol, nextState);
                return nextState;
            }
        }
    }
    return null;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Nullable(javax.annotation.Nullable)

Example 12 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class DefaultEncodingUsageCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    if (COMMONS_IO_CHARSET_MATCHERS.anyMatch(mit)) {
        Arguments arguments = mit.arguments();
        ExpressionTree lastArgument = arguments.get(arguments.size() - 1);
        testNullLiteralPassedForEncoding(lastArgument);
    } else if (FILEUTILS_WRITE_WITH_CHARSET_MATCHERS.anyMatch(mit)) {
        testNullLiteralPassedForEncoding(mit.arguments().get(2));
    } else {
        reportIssue(ExpressionUtils.methodName(mit), "Remove this use of \"" + mit.symbol().name() + "\"");
    }
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 13 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class IndexOfStartPositionCheck method checkIndexOfInvocation.

private void checkIndexOfInvocation(MethodInvocationTree mit, ExpressionTree other) {
    if (INDEX_OF_METHOD.matches(mit)) {
        String replaceMessage;
        ExpressionTree firstPar = mit.arguments().get(0);
        if (firstPar.is(Tree.Kind.STRING_LITERAL)) {
            replaceMessage = ((LiteralTree) firstPar).value();
        } else if (firstPar.is(Tree.Kind.IDENTIFIER)) {
            replaceMessage = ((IdentifierTree) firstPar).name();
        } else {
            replaceMessage = "xxx";
        }
        Long otherValue = LiteralUtils.longLiteralValue(other);
        if (otherValue != null && otherValue != -1 && otherValue != 0) {
            reportIssue(ExpressionUtils.methodName(mit), "Use \".indexOf(" + replaceMessage + ",n) > -1\" instead.");
        }
    }
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 14 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class LiteralUtils method longLiteralValue.

@CheckForNull
public static Long longLiteralValue(ExpressionTree tree) {
    ExpressionTree expression = tree;
    int sign = tree.is(Kind.UNARY_MINUS) ? -1 : 1;
    if (tree.is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) {
        expression = ((UnaryExpressionTree) tree).expression();
    }
    if (expression.is(Kind.INT_LITERAL, Kind.LONG_LITERAL)) {
        String value = trimLongSuffix(((LiteralTree) expression).value());
        // long as hexadecimal can be written using underscore to separate groups
        value = value.replaceAll("\\_", "");
        try {
            return sign * Long.decode(value);
        } catch (NumberFormatException e) {
        // Long.decode() may fail in case of very large long number written in hexadecimal. In such situation, we ignore the number.
        // Note that Long.MAX_VALUE = "0x7FFF_FFFF_FFFF_FFFFL", but it is possible to write larger numbers in hexadecimal
        // to be used as mask in bitwise operation. For instance:
        // 0x8000_0000_0000_0000L (MAX_VALUE + 1),
        // 0xFFFF_FFFF_FFFF_FFFFL (only ones),
        // 0xFFFF_FFFF_FFFF_FFFEL (only ones except least significant bit), ...
        }
    }
    return null;
}
Also used : UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) CheckForNull(javax.annotation.CheckForNull)

Example 15 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class PackageUtils method packageName.

public static String packageName(@Nullable PackageDeclarationTree packageDeclarationTree, String separator) {
    if (packageDeclarationTree == null) {
        return "";
    }
    Deque<String> pieces = new LinkedList<>();
    ExpressionTree expr = packageDeclarationTree.packageName();
    while (expr.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mse = (MemberSelectExpressionTree) expr;
        pieces.push(mse.identifier().name());
        pieces.push(separator);
        expr = mse.expression();
    }
    if (expr.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree idt = (IdentifierTree) expr;
        pieces.push(idt.name());
    }
    StringBuilder sb = new StringBuilder();
    for (String piece : pieces) {
        sb.append(piece);
    }
    return sb.toString();
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LinkedList(java.util.LinkedList)

Aggregations

ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)189 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)88 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)80 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)66 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)50 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)44 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)35 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)30 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)30 Test (org.junit.Test)25 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)24 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)23 Type (org.sonar.plugins.java.api.semantic.Type)21 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)20 Tree (org.sonar.plugins.java.api.tree.Tree)20 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)14 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)13