use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class LiteralUtilsTest method test_int_and_long_value.
@Test
public void test_int_and_long_value() throws Exception {
Integer[] expectedIntegerValues = { 42, -7, 3, null, null, null, null, 5678 };
Long[] expectedLongValues = { 42L, 42L, -7L, -7L, +3L, +3L, null, null, 255L, null, null, null, Long.MAX_VALUE, Long.MAX_VALUE, null, 10010L };
int i = 0;
int j = 0;
for (VariableTree variableTree : variables) {
if (variableTree.simpleName().name().startsWith("x")) {
assertThat(LiteralUtils.intLiteralValue(variableTree.initializer())).isEqualTo(expectedIntegerValues[i++]);
} else if (variableTree.simpleName().name().startsWith("y")) {
assertThat(LiteralUtils.longLiteralValue(variableTree.initializer())).isEqualTo(expectedLongValues[j++]);
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class LiteralUtilsTest method testEmptyString.
@Test
public void testEmptyString() {
boolean[] expectedStringEmptyResult = { true, false, false, false };
int i = 0;
for (VariableTree variableTree : variables) {
if (variableTree.simpleName().name().startsWith("s")) {
assertThat(LiteralUtils.isEmptyString(variableTree.initializer())).isEqualTo(expectedStringEmptyResult[i++]);
}
}
Optional<VariableTree> nonStringVariable = variables.stream().filter(v -> v.simpleName().name().startsWith("x")).findFirst();
assertThat(nonStringVariable).isPresent();
assertThat(LiteralUtils.isEmptyString(nonStringVariable.get().initializer())).isFalse();
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class SyntaxTreeDebug method methodString.
private static String methodString(MethodTree syntaxNode) {
StringBuilder buffer = new StringBuilder();
for (ModifierTree modifier : syntaxNode.modifiers()) {
if (modifier.is(Tree.Kind.TOKEN)) {
buffer.append(((SyntaxToken) modifier).text());
buffer.append(' ');
}
}
buffer.append(syntaxNode.returnType().symbolType().toString());
buffer.append(' ');
buffer.append(syntaxNode.simpleName().name());
buffer.append('(');
boolean first = true;
for (VariableTree parameter : syntaxNode.parameters()) {
if (first) {
first = false;
} else {
buffer.append(", ");
}
buffer.append(parameter.type().symbolType().toString());
buffer.append(' ');
buffer.append(parameter.simpleName().toString());
}
buffer.append(')');
return buffer.toString();
}
use of org.sonar.plugins.java.api.tree.VariableTree 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();
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ImmediateReverseBoxingCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
visitMethodInvocationTree((MethodInvocationTree) tree);
} else if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
ExpressionTree initializer = variableTree.initializer();
if (initializer != null) {
checkExpression(initializer, variableTree.type().symbolType());
}
} else if (tree.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) tree;
checkExpression(assignmentTree.expression(), assignmentTree.symbolType());
} else {
NewClassTree newClassTree = (NewClassTree) tree;
Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
if (classSymbol != null) {
ExpressionTree arg0 = newClassTree.arguments().get(0);
checkForUnboxing(arg0);
checkForUselessUnboxing(newClassTree.symbolType(), newClassTree.identifier(), arg0);
}
}
}
Aggregations