SPR-6941
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3314 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
dedf6489ac
commit
4e32e896ab
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2010 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.
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This exception wraps (as cause) a checked exception thrown by some method that SpEL invokes.
|
||||||
|
* It differs from a SpelEvaluationException because this indicates the occurrence of a checked exception
|
||||||
|
* that the invoked method was defined to throw. SpelEvaluationExceptions are for handling (and wrapping)
|
||||||
|
* unexpected exceptions.
|
||||||
|
*
|
||||||
|
* @author Andy Clement
|
||||||
|
* @since 3.0.3
|
||||||
|
*/
|
||||||
|
public class ExpressionInvocationTargetException extends EvaluationException {
|
||||||
|
|
||||||
|
public ExpressionInvocationTargetException(int position, String message, Throwable cause) {
|
||||||
|
super(position, message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionInvocationTargetException(int position, String message) {
|
||||||
|
super(position, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionInvocationTargetException(String expressionString, String message) {
|
||||||
|
super(expressionString, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionInvocationTargetException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionInvocationTargetException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -21,6 +21,7 @@ import java.util.List;
|
||||||
import org.springframework.expression.AccessException;
|
import org.springframework.expression.AccessException;
|
||||||
import org.springframework.expression.EvaluationContext;
|
import org.springframework.expression.EvaluationContext;
|
||||||
import org.springframework.expression.EvaluationException;
|
import org.springframework.expression.EvaluationException;
|
||||||
|
import org.springframework.expression.ExpressionInvocationTargetException;
|
||||||
import org.springframework.expression.MethodExecutor;
|
import org.springframework.expression.MethodExecutor;
|
||||||
import org.springframework.expression.MethodResolver;
|
import org.springframework.expression.MethodResolver;
|
||||||
import org.springframework.expression.TypedValue;
|
import org.springframework.expression.TypedValue;
|
||||||
|
|
@ -123,8 +124,9 @@ public class MethodReference extends SpelNodeImpl {
|
||||||
if (rootCause instanceof RuntimeException) {
|
if (rootCause instanceof RuntimeException) {
|
||||||
throw (RuntimeException)rootCause;
|
throw (RuntimeException)rootCause;
|
||||||
} else {
|
} else {
|
||||||
throw new SpelEvaluationException( getStartPosition(), rootCause, SpelMessage.EXCEPTION_DURING_METHOD_INVOCATION,
|
throw new ExpressionInvocationTargetException( getStartPosition(),
|
||||||
this.name, state.getActiveContextObject().getValue().getClass().getName(), rootCause.getMessage());
|
"A problem occurred when trying to execute method '"+this.name+"' on object of type '"+state.getActiveContextObject().getValue().getClass().getName()+"'",
|
||||||
|
rootCause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import org.junit.Test;
|
||||||
import org.springframework.expression.AccessException;
|
import org.springframework.expression.AccessException;
|
||||||
import org.springframework.expression.EvaluationContext;
|
import org.springframework.expression.EvaluationContext;
|
||||||
import org.springframework.expression.Expression;
|
import org.springframework.expression.Expression;
|
||||||
|
import org.springframework.expression.ExpressionInvocationTargetException;
|
||||||
import org.springframework.expression.MethodExecutor;
|
import org.springframework.expression.MethodExecutor;
|
||||||
import org.springframework.expression.MethodFilter;
|
import org.springframework.expression.MethodFilter;
|
||||||
import org.springframework.expression.MethodResolver;
|
import org.springframework.expression.MethodResolver;
|
||||||
|
|
@ -146,7 +147,7 @@ public class MethodInvocationTests extends ExpressionTestCase {
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 4 means it will throw a checked exception - this will be wrapped
|
// 4 means it will throw a checked exception - this will be wrapped
|
||||||
if (!(e instanceof SpelEvaluationException)) {
|
if (!(e instanceof ExpressionInvocationTargetException)) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Assert.fail("Should have been wrapped");
|
Assert.fail("Should have been wrapped");
|
||||||
}
|
}
|
||||||
|
|
@ -183,6 +184,29 @@ public class MethodInvocationTests extends ExpressionTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMethodThrowingException_SPR6941_2() {
|
||||||
|
// Test method on inventor: throwException()
|
||||||
|
// On 1 it will throw an IllegalArgumentException
|
||||||
|
// On 2 it will throw a RuntimeException
|
||||||
|
// On 3 it will exit normally
|
||||||
|
// In each case it increments the Inventor field 'counter' when invoked
|
||||||
|
|
||||||
|
SpelExpressionParser parser = new SpelExpressionParser();
|
||||||
|
Expression expr = parser.parseExpression("throwException(#bar)");
|
||||||
|
|
||||||
|
eContext.setVariable("bar",4);
|
||||||
|
try {
|
||||||
|
expr.getValue(eContext);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (ExpressionInvocationTargetException e) {
|
||||||
|
Throwable t = e.getCause();
|
||||||
|
Assert.assertEquals("org.springframework.expression.spel.testresources.Inventor$TestException", t.getClass().getName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Assert.fail("Should not be a SpelEvaluationException");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMethodFiltering_SPR6764() {
|
public void testMethodFiltering_SPR6764() {
|
||||||
SpelExpressionParser parser = new SpelExpressionParser();
|
SpelExpressionParser parser = new SpelExpressionParser();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue