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