/** * Process the string form of a number, using the specified base if supplied * and return an appropriate literal to hold it. Any suffix to indicate a * long will be taken into account (either 'l' or 'L' is supported). * @param numberToken the token holding the number as its payload (eg. 1234 or 0xCAFE) * @param radix the base of number * @return a subtype of Literal that can represent it */ public static Literal getIntLiteral(String numberToken, int startPos, int endPos, int radix) { try { int value = Integer.parseInt(numberToken, radix); return new IntLiteral(numberToken, startPos, endPos, value); } catch (NumberFormatException ex) { throw new InternalParseException(new SpelParseException(startPos, ex, SpelMessage.NOT_AN_INTEGER, numberToken)); } }
@Test public void test_unaryPlusWithNumberOperand() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); { RealLiteral realLiteral = new RealLiteral("123.00", -1, -1, 123.0); OpPlus o = new OpPlus(-1, -1, realLiteral); TypedValue value = o.getValueInternal(expressionState); assertEquals(Double.class, value.getTypeDescriptor().getObjectType()); assertEquals(Double.class, value.getTypeDescriptor().getType()); assertEquals(realLiteral.getLiteralValue().getValue(), value.getValue()); } { IntLiteral intLiteral = new IntLiteral("123", -1, -1, 123); OpPlus o = new OpPlus(-1, -1, intLiteral); TypedValue value = o.getValueInternal(expressionState); assertEquals(Integer.class, value.getTypeDescriptor().getObjectType()); assertEquals(Integer.class, value.getTypeDescriptor().getType()); assertEquals(intLiteral.getLiteralValue().getValue(), value.getValue()); } { LongLiteral longLiteral = new LongLiteral("123", -1, -1, 123L); OpPlus o = new OpPlus(-1, -1, longLiteral); TypedValue value = o.getValueInternal(expressionState); assertEquals(Long.class, value.getTypeDescriptor().getObjectType()); assertEquals(Long.class, value.getTypeDescriptor().getType()); assertEquals(longLiteral.getLiteralValue().getValue(), value.getValue()); } }
/** * Process the string form of a number, using the specified base if supplied * and return an appropriate literal to hold it. Any suffix to indicate a * long will be taken into account (either 'l' or 'L' is supported). * @param numberToken the token holding the number as its payload (eg. 1234 or 0xCAFE) * @param radix the base of number * @return a subtype of Literal that can represent it */ public static Literal getIntLiteral(String numberToken, int pos, int radix) { try { int value = Integer.parseInt(numberToken, radix); return new IntLiteral(numberToken, pos, value); } catch (NumberFormatException ex) { throw new InternalParseException(new SpelParseException(pos>>16, ex, SpelMessage.NOT_AN_INTEGER, numberToken)); } }
IntLiteral n1 = new IntLiteral("123", -1, -1, 123); IntLiteral n2 = new IntLiteral("456", -1, -1, 456); OpPlus o = new OpPlus(-1, -1, n1, n2); TypedValue value = o.getValueInternal(expressionState);
/** * Process the string form of a number, using the specified base if supplied * and return an appropriate literal to hold it. Any suffix to indicate a * long will be taken into account (either 'l' or 'L' is supported). * @param numberToken the token holding the number as its payload (eg. 1234 or 0xCAFE) * @param radix the base of number * @return a subtype of Literal that can represent it */ public static Literal getIntLiteral(String numberToken, int pos, int radix) { try { int value = Integer.parseInt(numberToken, radix); return new IntLiteral(numberToken, pos, value); } catch (NumberFormatException ex) { throw new InternalParseException(new SpelParseException(pos>>16, ex, SpelMessage.NOT_AN_INTEGER, numberToken)); } }