Consistent ExpressionException-style quoting of expression string and position

Issue: SPR-14942
This commit is contained in:
Juergen Hoeller
2016-11-24 11:08:55 +01:00
parent b10045dc0e
commit 14eba5034d
8 changed files with 155 additions and 147 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@@ -26,7 +26,24 @@ package org.springframework.expression;
public class EvaluationException extends ExpressionException {
/**
* Creates a new expression evaluation exception.
* Create a new expression evaluation exception.
* @param message description of the problem that occurred
*/
public EvaluationException(String message) {
super(message);
}
/**
* Create a new expression evaluation exception.
* @param message description of the problem that occurred
* @param cause the underlying cause of this exception
*/
public EvaluationException(String message, Throwable cause) {
super(message,cause);
}
/**
* Create a new expression evaluation exception.
* @param position the position in the expression where the problem occurred
* @param message description of the problem that occurred
*/
@@ -35,7 +52,7 @@ public class EvaluationException extends ExpressionException {
}
/**
* Creates a new expression evaluation exception.
* Create a new expression evaluation exception.
* @param expressionString the expression that could not be evaluated
* @param message description of the problem that occurred
*/
@@ -44,7 +61,7 @@ public class EvaluationException extends ExpressionException {
}
/**
* Creates a new expression evaluation exception.
* Create a new expression evaluation exception.
* @param position the position in the expression where the problem occurred
* @param message description of the problem that occurred
* @param cause the underlying cause of this exception
@@ -53,16 +70,4 @@ public class EvaluationException extends ExpressionException {
super(position, message, cause);
}
/**
* Creates a new expression evaluation exception.
* @param message description of the problem that occurred
*/
public EvaluationException(String message) {
super(message);
}
public EvaluationException(String message, Throwable cause) {
super(message,cause);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -20,6 +20,7 @@ package org.springframework.expression;
* Super class for exceptions that can occur whilst processing expressions.
*
* @author Andy Clement
* @author Phil Webb
* @since 3.0
*/
@SuppressWarnings("serial")
@@ -27,9 +28,26 @@ public class ExpressionException extends RuntimeException {
protected String expressionString;
protected int position; // -1 if not known - but should be known in all reasonable cases
protected int position; // -1 if not known; should be known in all reasonable cases
/**
* Construct a new expression exception.
* @param message a descriptive message
*/
public ExpressionException(String message) {
super(message);
}
/**
* Construct a new expression exception.
* @param message a descriptive message
* @param cause the underlying cause of this exception
*/
public ExpressionException(String message, Throwable cause) {
super(message, cause);
}
/**
* Construct a new expression exception.
* @param expressionString the expression string
@@ -37,8 +55,8 @@ public class ExpressionException extends RuntimeException {
*/
public ExpressionException(String expressionString, String message) {
super(message);
this.position = -1;
this.expressionString = expressionString;
this.position = -1;
}
/**
@@ -49,8 +67,8 @@ public class ExpressionException extends RuntimeException {
*/
public ExpressionException(String expressionString, int position, String message) {
super(message);
this.position = position;
this.expressionString = expressionString;
this.position = position;
}
/**
@@ -74,23 +92,6 @@ public class ExpressionException extends RuntimeException {
this.position = position;
}
/**
* Construct a new expression exception.
* @param message a descriptive message
*/
public ExpressionException(String message) {
super(message);
}
/**
* Construct a new expression exception.
* @param message a descriptive message
* @param cause the underlying cause of this exception
*/
public ExpressionException(String message, Throwable cause) {
super(message,cause);
}
/**
* Return the expression string.
@@ -107,8 +108,9 @@ public class ExpressionException extends RuntimeException {
}
/**
* Return the exception message. Since Spring 4.0 this method returns the
* same result as {@link #toDetailedString()}.
* Return the exception message.
* As of Spring 4.0, this method returns the same result as {@link #toDetailedString()}.
* @see #getSimpleMessage()
* @see java.lang.Throwable#getMessage()
*/
@Override
@@ -123,11 +125,11 @@ public class ExpressionException extends RuntimeException {
public String toDetailedString() {
if (this.expressionString != null) {
StringBuilder output = new StringBuilder();
output.append("Expression '");
output.append("Expression [");
output.append(this.expressionString);
output.append("'");
if (this.position != -1) {
output.append(" @ ");
output.append("]");
if (this.position >= 0) {
output.append(" @");
output.append(this.position);
}
output.append(": ");
@@ -142,6 +144,7 @@ public class ExpressionException extends RuntimeException {
/**
* Return the exception simple message without including the expression
* that caused the failure.
* @since 4.0
*/
public String getSimpleMessage() {
return super.getMessage();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@@ -26,7 +26,7 @@ package org.springframework.expression;
public class ParseException extends ExpressionException {
/**
* Creates a new expression parsing exception.
* Create a new expression parsing exception.
* @param expressionString the expression string that could not be parsed
* @param position the position in the expression string where the problem occurred
* @param message description of the problem that occurred
@@ -36,7 +36,7 @@ public class ParseException extends ExpressionException {
}
/**
* Creates a new expression parsing exception.
* Create a new expression parsing exception.
* @param position the position in the expression string where the problem occurred
* @param message description of the problem that occurred
* @param cause the underlying cause of this exception
@@ -46,7 +46,7 @@ public class ParseException extends ExpressionException {
}
/**
* Creates a new expression parsing exception.
* Create a new expression parsing exception.
* @param position the position in the expression string where the problem occurred
* @param message description of the problem that occurred
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel;
import org.springframework.expression.EvaluationException;
@@ -23,6 +24,7 @@ import org.springframework.expression.EvaluationException;
* message. See {@link SpelMessage} for the list of all possible messages that can occur.
*
* @author Andy Clement
* @author Juergen Hoeller
* @since 3.0
*/
@SuppressWarnings("serial")
@@ -34,62 +36,46 @@ public class SpelEvaluationException extends EvaluationException {
public SpelEvaluationException(SpelMessage message, Object... inserts) {
super(message.formatMessage(0, inserts)); // TODO poor position information, can the callers not really supply something?
super(message.formatMessage(inserts));
this.message = message;
this.inserts = inserts;
}
public SpelEvaluationException(int position, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(position, inserts));
super(position, message.formatMessage(inserts));
this.message = message;
this.inserts = inserts;
}
public SpelEvaluationException(int position, Throwable cause,
SpelMessage message, Object... inserts) {
super(position,message.formatMessage(position,inserts),cause);
public SpelEvaluationException(int position, Throwable cause, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(inserts),cause);
this.message = message;
this.inserts = inserts;
}
public SpelEvaluationException(Throwable cause, SpelMessage message, Object... inserts) {
super(message.formatMessage(0,inserts),cause);
super(message.formatMessage(inserts), cause);
this.message = message;
this.inserts = inserts;
}
/**
* @return a formatted message with inserts applied
*/
@Override
public String getMessage() {
if (this.message != null) {
return this.message.formatMessage(this.position, this.inserts);
}
else {
return super.getMessage();
}
}
/**
* @return the message code
*/
public SpelMessage getMessageCode() {
return this.message;
}
/**
* Set the position in the related expression which gave rise to this exception.
*
* @param position the position in the expression that gave rise to the exception
*/
public void setPosition(int position) {
this.position = position;
}
/**
* @return the message inserts
* Return the message code.
*/
public SpelMessage getMessageCode() {
return this.message;
}
/**
* Return the message inserts.
*/
public Object[] getInserts() {
return this.inserts;

View File

@@ -24,16 +24,13 @@ import java.text.MessageFormat;
* expect particular code numbers rather than particular text, enabling the message text
* to more easily be modified and the tests to run successfully in different locales.
*
* <p>When a message is formatted, it will have this kind of form
* <p>When a message is formatted, it will have this kind of form, capturing the prefix
* and the error kind:
*
* <pre class="code">
* EL1004E: (pos 34): Type cannot be found 'String'
* </pre>
*
* The prefix captures the code and the error kind, whilst the position is included
* if it is known.
* <pre class="code">EL1004E: Type cannot be found 'String'</pre>
*
* @author Andy Clement
* @author Juergen Hoeller
* @since 3.0
*/
public enum SpelMessage {
@@ -175,7 +172,7 @@ public enum SpelMessage {
"Cannot find terminating \" for string"),
NON_TERMINATING_QUOTED_STRING(Kind.ERROR, 1046,
"Cannot find terminating ' for string"),
"Cannot find terminating '' for string"),
MISSING_LEADING_ZERO_FOR_NUMBER(Kind.ERROR, 1047,
"A real number must be prefixed by zero, it cannot start with just ''.''"),
@@ -190,7 +187,7 @@ public enum SpelMessage {
"The arguments '(...)' for the constructor call are missing"),
RUN_OUT_OF_ARGUMENTS(Kind.ERROR, 1051,
"Unexpected ran out of arguments"),
"Unexpectedly ran out of arguments"),
UNABLE_TO_GROW_COLLECTION(Kind.ERROR, 1052,
"Unable to grow collection"),
@@ -262,20 +259,42 @@ public enum SpelMessage {
private final String message;
private SpelMessage(Kind kind, int code, String message) {
SpelMessage(Kind kind, int code, String message) {
this.kind = kind;
this.code = code;
this.message = message;
}
/**
* Produce a complete message including the prefix and with the inserts
* applied to the message.
* @param inserts the inserts to put into the formatted message
* @return a formatted message
* @since 4.3.5
*/
public String formatMessage(Object... inserts) {
StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("EL").append(this.code);
switch (this.kind) {
case ERROR:
formattedMessage.append("E");
break;
}
formattedMessage.append(": ");
formattedMessage.append(MessageFormat.format(this.message, inserts));
return formattedMessage.toString();
}
/**
* Produce a complete message including the prefix, the position (if known)
* and with the inserts applied to the message.
* @param pos the position (ignored and not included in the message if less than 0)
* @param inserts the inserts to put into the formatted message
* @return a formatted message
* @deprecated as of Spring 4.3.5, in favor of {@link #formatMessage(Object...)}
*/
@Deprecated
public String formatMessage(int pos, Object... inserts) {
StringBuilder formattedMessage = new StringBuilder();
formattedMessage.append("EL").append(this.code);
@@ -285,7 +304,7 @@ public enum SpelMessage {
break;
}
formattedMessage.append(":");
if (pos != -1) {
if (pos >= 0) {
formattedMessage.append("(pos ").append(pos).append("): ");
}
formattedMessage.append(MessageFormat.format(this.message, inserts));
@@ -293,6 +312,6 @@ public enum SpelMessage {
}
public static enum Kind { INFO, WARNING, ERROR }
public enum Kind { INFO, WARNING, ERROR }
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel;
import org.springframework.expression.ParseException;
@@ -23,6 +24,7 @@ import org.springframework.expression.ParseException;
* message. See {@link SpelMessage} for the list of all possible messages that can occur.
*
* @author Andy Clement
* @author Juergen Hoeller
* @since 3.0
*/
@SuppressWarnings("serial")
@@ -34,45 +36,33 @@ public class SpelParseException extends ParseException {
public SpelParseException(String expressionString, int position, SpelMessage message, Object... inserts) {
super(expressionString, position, message.formatMessage(position,inserts));
this.position = position;
super(expressionString, position, message.formatMessage(inserts));
this.message = message;
this.inserts = inserts;
}
public SpelParseException(int position, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(position,inserts));
this.position = position;
super(position, message.formatMessage(inserts));
this.message = message;
this.inserts = inserts;
}
public SpelParseException(int position, Throwable cause, SpelMessage message, Object... inserts) {
super(position, message.formatMessage(position,inserts), cause);
this.position = position;
super(position, message.formatMessage(inserts), cause);
this.message = message;
this.inserts = inserts;
}
/**
* @return a formatted message with inserts applied
*/
@Override
public String getMessage() {
return (this.message != null ? this.message.formatMessage(this.position, this.inserts)
: super.getMessage());
}
/**
* @return the message code
* Return the message code.
*/
public SpelMessage getMessageCode() {
return this.message;
}
/**
* @return the message inserts
* Return the message inserts.
*/
public Object[] getInserts() {
return this.inserts;