SpelEvaluationException.java 2.7 KB
Newer Older
A
Andy Clement 已提交
1
/*
2
 * Copyright 2004-2009 the original author or authors.
A
Andy Clement 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.expression.spel;

import org.springframework.expression.EvaluationException;

/**
 * Root exception for Spring EL related exceptions. Rather than holding a hard coded string indicating the problem, it
22
 * records a message key and the inserts for the message. See {@link SpelMessage} for the list of all possible messages
A
Andy Clement 已提交
23 24 25
 * that can occur.
 * 
 * @author Andy Clement
26
 * @since 3.0
A
Andy Clement 已提交
27
 */
28
public class SpelEvaluationException extends EvaluationException {
A
Andy Clement 已提交
29

30
	private SpelMessage message;
A
Andy Clement 已提交
31 32
	private Object[] inserts;

33
	public SpelEvaluationException(SpelMessage message, Object... inserts) {
34
		super(message.formatMessage(0, inserts)); // TODO poor position information, can the callers not really supply something?
A
Andy Clement 已提交
35 36 37 38
		this.message = message;
		this.inserts = inserts;
	}

39
	public SpelEvaluationException(int position, SpelMessage message, Object... inserts) {
40
		super(position, message.formatMessage(position, inserts)); 
A
Andy Clement 已提交
41 42 43 44
		this.message = message;
		this.inserts = inserts;
	}

45
	public SpelEvaluationException(int position, Throwable cause,
46
			SpelMessage message, Object... inserts) {
47
		super(position,message.formatMessage(position,inserts),cause);
A
Andy Clement 已提交
48 49 50 51
		this.message = message;
		this.inserts = inserts;
	}

52
	public SpelEvaluationException(Throwable cause, SpelMessage message, Object... inserts) {
53
		super(message.formatMessage(0,inserts),cause);
A
Andy Clement 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		this.message = message;
		this.inserts = inserts;
	}

	/**
	 * @return a formatted message with inserts applied
	 */
	@Override
	public String getMessage() {
		if (message != null)
			return message.formatMessage(position, inserts);
		else
			return super.getMessage();
	}

	/**
70
	 * @return the message code
A
Andy Clement 已提交
71
	 */
72
	public SpelMessage getMessageCode() {
A
Andy Clement 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		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
	 */
	public Object[] getInserts() {
		return inserts;
	}

}