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.");
}
}
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);
}
}
}
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);
}
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));
}
}
}
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();
}
Aggregations