View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.expression.spel;
17  
18  import org.springframework.expression.EvaluationException;
19  
20  /**
21   * Root exception for Spring EL related exceptions. Rather than holding a hard coded
22   * string indicating the problem, it records a message key and the inserts for the
23   * message. See {@link SpelMessage} for the list of all possible messages that can occur.
24   *
25   * @author Andy Clement
26   * @since 3.0
27   */
28  @SuppressWarnings("serial")
29  public class SpelEvaluationException extends EvaluationException {
30  
31  	private final SpelMessage message;
32  
33  	private final Object[] inserts;
34  
35  
36  	public SpelEvaluationException(SpelMessage message, Object... inserts) {
37  		super(message.formatMessage(0, inserts)); // TODO poor position information, can the callers not really supply something?
38  		this.message = message;
39  		this.inserts = inserts;
40  	}
41  
42  	public SpelEvaluationException(int position, SpelMessage message, Object... inserts) {
43  		super(position, message.formatMessage(position, inserts));
44  		this.message = message;
45  		this.inserts = inserts;
46  	}
47  
48  	public SpelEvaluationException(int position, Throwable cause,
49  			SpelMessage message, Object... inserts) {
50  		super(position,message.formatMessage(position,inserts),cause);
51  		this.message = message;
52  		this.inserts = inserts;
53  	}
54  
55  	public SpelEvaluationException(Throwable cause, SpelMessage message, Object... inserts) {
56  		super(message.formatMessage(0,inserts),cause);
57  		this.message = message;
58  		this.inserts = inserts;
59  	}
60  
61  
62  	/**
63  	 * @return a formatted message with inserts applied
64  	 */
65  	@Override
66  	public String getMessage() {
67  		if (this.message != null) {
68  			return this.message.formatMessage(this.position, this.inserts);
69  		}
70  		else {
71  			return super.getMessage();
72  		}
73  	}
74  
75  	/**
76  	 * @return the message code
77  	 */
78  	public SpelMessage getMessageCode() {
79  		return this.message;
80  	}
81  
82  	/**
83  	 * Set the position in the related expression which gave rise to this exception.
84  	 *
85  	 * @param position the position in the expression that gave rise to the exception
86  	 */
87  	public void setPosition(int position) {
88  		this.position = position;
89  	}
90  
91  	/**
92  	 * @return the message inserts
93  	 */
94  	public Object[] getInserts() {
95  		return this.inserts;
96  	}
97  
98  }