Search in sources :

Example 1 with LiteralTree

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

the class DeprecatedHashAlgorithmCheck method algorithm.

private static Optional<InsecureAlgorithm> algorithm(ExpressionTree invocationArgument) {
    ExpressionTree expectedAlgorithm = invocationArgument;
    ExpressionTree defaultPropertyValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(invocationArgument);
    if (defaultPropertyValue != null) {
        expectedAlgorithm = defaultPropertyValue;
    }
    if (expectedAlgorithm.is(Tree.Kind.STRING_LITERAL)) {
        String algorithmName = LiteralUtils.trimQuotes(((LiteralTree) expectedAlgorithm).value());
        return Arrays.stream(InsecureAlgorithm.values()).filter(alg -> alg.match(algorithmName)).findFirst();
    }
    return Optional.empty();
}
Also used : LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Arrays(java.util.Arrays) TypeCriteria(org.sonar.java.matcher.TypeCriteria) ImmutableMap(com.google.common.collect.ImmutableMap) JavaPropertiesHelper(org.sonar.java.checks.helpers.JavaPropertiesHelper) ExpressionUtils(org.sonar.java.model.ExpressionUtils) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MD5(org.sonar.java.checks.DeprecatedHashAlgorithmCheck.InsecureAlgorithm.MD5) List(java.util.List) MD2(org.sonar.java.checks.DeprecatedHashAlgorithmCheck.InsecureAlgorithm.MD2) ImmutableList(com.google.common.collect.ImmutableList) AbstractMethodDetection(org.sonar.java.checks.methods.AbstractMethodDetection) Locale(java.util.Locale) Map(java.util.Map) LiteralUtils(org.sonar.java.model.LiteralUtils) MethodMatcher(org.sonar.java.matcher.MethodMatcher) SHA1(org.sonar.java.checks.DeprecatedHashAlgorithmCheck.InsecureAlgorithm.SHA1) Optional(java.util.Optional) Rule(org.sonar.check.Rule) Builder(com.google.common.collect.ImmutableList.Builder) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 2 with LiteralTree

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

the class StringBufferAndBuilderWithCharCheck method visitNewClass.

@Override
public void visitNewClass(NewClassTree tree) {
    if (TARGETED_CLASS.contains(getclassName(tree)) && tree.arguments().size() == 1) {
        ExpressionTree argument = tree.arguments().get(0);
        if (argument.is(Tree.Kind.CHAR_LITERAL)) {
            String character = ((LiteralTree) argument).value();
            context.reportIssue(this, argument, "Replace the constructor character parameter " + character + " with string parameter " + character.replace("'", "\"") + ".");
        }
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 3 with LiteralTree

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

the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.

private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
    ExpressionTree arg = null;
    ExpressionTree expression = null;
    if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
        Symbol symbol = mit.symbol();
        ExpressionTree methodSelect = mit.methodSelect();
        if (mit.arguments().size() == 1) {
            arg = mit.arguments().get(0);
            if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
                expression = ((MemberSelectExpressionTree) methodSelect).expression();
            }
        }
    } else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
        BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
        arg = equalTo.leftOperand();
        expression = equalTo.rightOperand();
    }
    if (arg != null && expression != null) {
        if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
            return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
        } else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
            return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
        }
    }
    return Optional.empty();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 4 with LiteralTree

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

the class SecureCookieCheck method isSetSecureCall.

private static boolean isSetSecureCall(MethodInvocationTree mit) {
    Symbol methodSymbol = mit.symbol();
    boolean hasArityOne = mit.arguments().size() == 1;
    if (hasArityOne && isCallSiteCookie(methodSymbol)) {
        ExpressionTree expressionTree = mit.arguments().get(0);
        if (expressionTree.is(Tree.Kind.BOOLEAN_LITERAL) && "false".equals(((LiteralTree) expressionTree).value())) {
            return false;
        }
        return "setSecure".equals(getIdentifier(mit).name());
    }
    return false;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 5 with LiteralTree

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

the class StringLiteralInsideEqualsCheck method check.

private void check(MethodInvocationTree tree) {
    if (isEquals(tree.methodSelect()) && tree.arguments().size() == 1 && tree.arguments().get(0).is(Kind.STRING_LITERAL)) {
        LiteralTree stringLiteral = (LiteralTree) tree.arguments().get(0);
        reportIssue(stringLiteral, "Move the " + stringLiteral.value() + " string literal on the left side of this string comparison.");
    }
}
Also used : LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Aggregations

LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)12 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)7 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)4 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)4 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 List (java.util.List)2 Map (java.util.Map)2 Rule (org.sonar.check.Rule)2 Symbol (org.sonar.plugins.java.api.semantic.Symbol)2 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 Builder (com.google.common.collect.ImmutableList.Builder)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Multimap (com.google.common.collect.Multimap)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1