Search in sources :

Example 6 with LiteralTree

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

the class UnderscoreMisplacedOnNumberCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LiteralTree literalTree = (LiteralTree) tree;
    String value = literalTree.value();
    if (hasIrregularPattern(value)) {
        reportIssue(literalTree, "Review this number; its irregular pattern indicates an error.");
    }
}
Also used : LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 7 with LiteralTree

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

the class StringLiteralDuplicatedCheck method scanFile.

@Override
public void scanFile(JavaFileScannerContext context) {
    occurrences.clear();
    constants.clear();
    scan(context.getTree());
    for (String entry : occurrences.keySet()) {
        Collection<LiteralTree> literalTrees = occurrences.get(entry);
        int literalOccurrence = literalTrees.size();
        if (constants.containsKey(entry)) {
            VariableTree constant = constants.get(entry);
            List<LiteralTree> duplications = literalTrees.stream().filter(literal -> literal.parent() != constant).collect(Collectors.toList());
            context.reportIssue(this, duplications.iterator().next(), "Use already-defined constant '" + constant.simpleName() + "' instead of duplicating its value here.", secondaryLocations(duplications.subList(1, duplications.size())), literalOccurrence);
        } else if (literalOccurrence >= threshold) {
            context.reportIssue(this, literalTrees.iterator().next(), "Define a constant instead of duplicating this literal " + entry + " " + literalOccurrence + " times.", secondaryLocations(literalTrees), literalOccurrence);
        }
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Modifier(org.sonar.plugins.java.api.tree.Modifier) RuleProperty(org.sonar.check.RuleProperty) Collection(java.util.Collection) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) HashMap(java.util.HashMap) Tree(org.sonar.plugins.java.api.tree.Tree) Multimap(com.google.common.collect.Multimap) ModifiersUtils(org.sonar.java.model.ModifiersUtils) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Map(java.util.Map) Rule(org.sonar.check.Rule) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 8 with LiteralTree

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

the class JavaTreeModelTest method literal.

@Test
public void literal() {
    LiteralTree tree = (LiteralTree) expressionOfReturnStatement("class T { int m() { return 1; } }");
    assertThat(tree.is(Tree.Kind.INT_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1");
    assertThatChildrenIteratorHasSize(tree, 1);
    SyntaxToken token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(27);
    tree = (LiteralTree) expressionOfReturnStatement("class T { long m() { return 1L; } }");
    assertThat(tree.is(Tree.Kind.LONG_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1L");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(28);
    tree = (LiteralTree) expressionOfReturnStatement("class T { float m() { return 1F; } }");
    assertThat(tree.is(Tree.Kind.FLOAT_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1F");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(29);
    tree = (LiteralTree) expressionOfReturnStatement("class T { double m() { return 1d; } }");
    assertThat(tree.is(Tree.Kind.DOUBLE_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("1d");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(30);
    tree = (LiteralTree) expressionOfReturnStatement("class T { boolean m() { return true; } }");
    assertThat(tree.is(Tree.Kind.BOOLEAN_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("true");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(31);
    tree = (LiteralTree) expressionOfReturnStatement("class T { boolean m() { return false; } }");
    assertThat(tree.is(Tree.Kind.BOOLEAN_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("false");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(31);
    tree = (LiteralTree) expressionOfReturnStatement("class T { char m() { return 'c'; } }");
    assertThat(tree.is(Tree.Kind.CHAR_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("'c'");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(28);
    tree = (LiteralTree) expressionOfReturnStatement("class T { String m() { return \"s\"; } }");
    assertThat(tree.is(Tree.Kind.STRING_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("\"s\"");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(30);
    tree = (LiteralTree) expressionOfReturnStatement("class T { Object m() { return null; } }");
    assertThat(tree.is(Tree.Kind.NULL_LITERAL)).isTrue();
    assertThat(tree.value()).isEqualTo("null");
    assertThatChildrenIteratorHasSize(tree, 1);
    token = tree.token();
    assertThat(token).isNotNull();
    assertThat(token.line()).isEqualTo(1);
    assertThat(token.column()).isEqualTo(30);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Test(org.junit.Test)

Example 9 with LiteralTree

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

the class InvalidDateValuesCheck method checkArgument.

private void checkArgument(ExpressionTree arg, String name, String message) {
    LiteralTree literal = null;
    int sign = 1;
    if (arg.is(Tree.Kind.INT_LITERAL)) {
        literal = (LiteralTree) arg;
    } else if (arg.is(Tree.Kind.UNARY_MINUS, Tree.Kind.UNARY_PLUS) && ((UnaryExpressionTree) arg).expression().is(Tree.Kind.INT_LITERAL)) {
        if (arg.is(Tree.Kind.UNARY_MINUS)) {
            sign = -1;
        }
        literal = (LiteralTree) ((UnaryExpressionTree) arg).expression();
    }
    if (literal != null) {
        int argValue = Integer.parseInt(literal.value()) * sign;
        if (argValue > Threshold.getThreshold(name) || argValue < 0) {
            reportIssue(arg, MessageFormat.format(message, argValue, name));
        }
    }
}
Also used : LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 10 with LiteralTree

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

the class SyntaxTreeDebug method memberSelectString.

private static String memberSelectString(MemberSelectExpressionTree expression) {
    StringBuilder buffer = new StringBuilder();
    ExpressionTree target = expression.expression();
    switch(target.kind()) {
        case IDENTIFIER:
            buffer.append(identifierString((IdentifierTree) target));
            break;
        case METHOD_INVOCATION:
            buffer.append(methodInvocationString((MethodInvocationTree) target));
            break;
        case VARIABLE:
            buffer.append(variableString((VariableTree) target));
            break;
        case INT_LITERAL:
            buffer.append(literalString((LiteralTree) target));
            break;
        default:
            break;
    }
    buffer.append('.');
    buffer.append(identifierString(expression.identifier()));
    return buffer.toString();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) 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