Skip to content

Commit

Permalink
Use ASTNodes.getIntegerLiteral()
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Tiercelin committed Apr 28, 2020
1 parent f6a080c commit caa9410
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NormalAnnotation;
import org.eclipse.jdt.core.dom.NullLiteral;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.PostfixExpression;
Expand Down Expand Up @@ -1687,10 +1686,22 @@ public static boolean isHardCoded(final Expression expression) {
* @return Integer literal.
*/
public static Long getIntegerLiteral(final Expression input) {
NumberLiteral contant= as(input, NumberLiteral.class);
if (input == null) {
return null;
}

Object number= input.resolveConstantExpressionValue();

if (number instanceof Short) {
return (long) ((Short) number).intValue();
}

if (number instanceof Integer) {
return (long) ((Integer) number).intValue();
}

if (contant != null) {
return positiveLiteral(contant);
if (number instanceof Long) {
return (Long) number;
}

InfixExpression operation= as(input, InfixExpression.class);
Expand Down Expand Up @@ -1785,30 +1796,6 @@ && hasOperator(operation,
return null;
}

/**
* Positive literal.
*
* @param input The input
* @return Positive literal.
*/
public static Long positiveLiteral(final NumberLiteral input) {
Object number= input.resolveConstantExpressionValue();

if (number instanceof Short) {
return (long) ((Short) number).intValue();
}

if (number instanceof Integer) {
return (long) ((Integer) number).intValue();
}

if (number instanceof Long) {
return (Long) number;
}

return null;
}

/**
* Returns whether the provided binding represents a local variable.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@ private boolean isFloatingPointType(final Expression expression) {

/** See JLS, section 5.2 Assignment Conversion */
private boolean isConstantExpressionAssignmentConversion(final CastExpression node) {
Object value= node.getExpression().resolveConstantExpressionValue();
if (value instanceof Integer) {
int val= (Integer) value;
Long value= ASTNodes.getIntegerLiteral(node.getExpression());

if (value != null) {
long val= value;
return ASTNodes.hasType(node, byte.class.getSimpleName()) && Byte.MIN_VALUE <= val && val <= Byte.MAX_VALUE
|| ASTNodes.hasType(node, short.class.getSimpleName()) && Short.MIN_VALUE <= val && val <= Short.MAX_VALUE
|| ASTNodes.hasType(node, char.class.getSimpleName()) && 0 <= val && val <= 65535;
Expand Down

0 comments on commit caa9410

Please sign in to comment.