use of org.sonar.plugins.java.api.tree.UnaryExpressionTree 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;
}
use of org.sonar.plugins.java.api.tree.UnaryExpressionTree in project sonar-java by SonarSource.
the class AbsOnNegativeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodTree = (MethodInvocationTree) tree;
if (MATH_ABS_METHODS.anyMatch(methodTree)) {
ExpressionTree firstArgument = methodTree.arguments().get(0);
checkForIssue(firstArgument);
}
} else {
ExpressionTree operand = ((UnaryExpressionTree) tree).expression();
checkForIssue(operand);
}
}
use of org.sonar.plugins.java.api.tree.UnaryExpressionTree in project sonar-java by SonarSource.
the class UselessIncrementCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
if (tree.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnExpression = ((ReturnStatementTree) tree).expression();
if (returnExpression != null && isPostfix(returnExpression)) {
UnaryExpressionTree unaryExpression = (UnaryExpressionTree) returnExpression;
ExpressionTree expression = ExpressionUtils.skipParentheses(unaryExpression.expression());
if (expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).symbol().owner().isMethodSymbol()) {
reportIssue(unaryExpression);
}
}
} else {
AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
if (isPostfix(aet.expression())) {
UnaryExpressionTree postfix = (UnaryExpressionTree) aet.expression();
if (SyntacticEquivalence.areEquivalent(aet.variable(), postfix.expression())) {
reportIssue(postfix);
}
}
}
}
use of org.sonar.plugins.java.api.tree.UnaryExpressionTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method unary_operators.
/**
* 15.15. Unary Operators
*/
@Test
public void unary_operators() {
UnaryExpressionTree tree;
tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { ++i; } }"))).expression();
assertThat(tree.is(Tree.Kind.PREFIX_INCREMENT)).isTrue();
assertThat(tree.operatorToken().text()).isEqualTo("++");
assertThat(tree.expression()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 2);
tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { --i; } }"))).expression();
assertThat(tree.is(Tree.Kind.PREFIX_DECREMENT)).isTrue();
assertThat(tree.operatorToken().text()).isEqualTo("--");
assertThat(tree.expression()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 2);
}
use of org.sonar.plugins.java.api.tree.UnaryExpressionTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method postfix_expression.
/**
* 15.14. Postfix Expressions
*/
@Test
public void postfix_expression() {
UnaryExpressionTree tree;
tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i++; } }"))).expression();
assertThat(tree.is(Tree.Kind.POSTFIX_INCREMENT)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.operatorToken().text()).isEqualTo("++");
assertThatChildrenIteratorHasSize(tree, 2);
tree = (UnaryExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(("class T { void m() { i--; } }"))).expression();
assertThat(tree.is(Tree.Kind.POSTFIX_DECREMENT)).isTrue();
assertThat(tree.expression()).isNotNull();
assertThat(tree.operatorToken().text()).isEqualTo("--");
assertThatChildrenIteratorHasSize(tree, 2);
}
Aggregations