Added getOriginalValue() accessor to (Real)Literal

Issue: SPR-10248
This commit is contained in:
Juergen Hoeller 2013-01-31 17:50:37 +01:00
parent bd72fcd469
commit 7d798acd35
1 changed files with 27 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,23 +17,32 @@
package org.springframework.expression.spel.ast;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.*;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.InternalParseException;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.expression.spel.SpelParseException;
/**
* Common superclass for nodes representing literals (boolean, string, number, etc).
*
* @author Andy Clement
* @author Juergen Hoeller
*/
public abstract class Literal extends SpelNodeImpl {
protected String literalValue;
private final String originalValue;
public Literal(String payload, int pos) {
public Literal(String originalValue, int pos) {
super(pos);
this.literalValue = payload;
this.originalValue = originalValue;
}
public abstract TypedValue getLiteralValue();
public final String getOriginalValue() {
return this.originalValue;
}
@Override
public final TypedValue getValueInternal(ExpressionState state) throws SpelEvaluationException {
@ -50,10 +59,13 @@ public abstract class Literal extends SpelNodeImpl {
return toString();
}
public abstract TypedValue getLiteralValue();
/**
* 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
@ -62,7 +74,8 @@ public abstract class Literal extends SpelNodeImpl {
try {
int value = Integer.parseInt(numberToken, radix);
return new IntLiteral(numberToken, pos, value);
} catch (NumberFormatException nfe) {
}
catch (NumberFormatException nfe) {
throw new InternalParseException(new SpelParseException(pos>>16, nfe, SpelMessage.NOT_AN_INTEGER, numberToken));
}
}
@ -71,25 +84,26 @@ public abstract class Literal extends SpelNodeImpl {
try {
long value = Long.parseLong(numberToken, radix);
return new LongLiteral(numberToken, pos, value);
} catch (NumberFormatException nfe) {
}
catch (NumberFormatException nfe) {
throw new InternalParseException(new SpelParseException(pos>>16, nfe, SpelMessage.NOT_A_LONG, numberToken));
}
}
public static Literal getRealLiteral(String numberToken, int pos, boolean isFloat) {
try {
if (isFloat) {
float value = Float.parseFloat(numberToken);
return new FloatLiteral(numberToken, pos, value);
} else {
}
else {
double value = Double.parseDouble(numberToken);
return new RealLiteral(numberToken, pos, value);
}
} catch (NumberFormatException nfe) {
}
catch (NumberFormatException nfe) {
throw new InternalParseException(new SpelParseException(pos>>16, nfe, SpelMessage.NOT_A_REAL, numberToken));
}
}
}