diff --git a/plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom/ASTNodes.java b/plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom/ASTNodes.java index e4ab66fc7..8086f4033 100644 --- a/plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom/ASTNodes.java +++ b/plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom/ASTNodes.java @@ -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; @@ -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); @@ -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. * diff --git a/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/RemoveUnnecessaryCastCleanUp.java b/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/RemoveUnnecessaryCastCleanUp.java index f9fe0a841..50410b5fe 100644 --- a/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/RemoveUnnecessaryCastCleanUp.java +++ b/plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/RemoveUnnecessaryCastCleanUp.java @@ -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;