Browse Source

SPR-6941

pull/1234/head
Andy Clement 15 years ago
parent
commit
2b0655b459
  1. 49
      org.springframework.expression/src/main/java/org/springframework/expression/ExpressionInvocationTargetException.java
  2. 6
      org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java
  3. 26
      org.springframework.expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java

49
org.springframework.expression/src/main/java/org/springframework/expression/ExpressionInvocationTargetException.java

@ -0,0 +1,49 @@ @@ -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);
}
}

6
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/MethodReference.java

@ -21,6 +21,7 @@ import java.util.List; @@ -21,6 +21,7 @@ import java.util.List;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.ExpressionInvocationTargetException;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.TypedValue;
@ -123,8 +124,9 @@ public class MethodReference extends SpelNodeImpl { @@ -123,8 +124,9 @@ public class MethodReference extends SpelNodeImpl {
if (rootCause instanceof RuntimeException) {
throw (RuntimeException)rootCause;
} else {
throw new SpelEvaluationException( getStartPosition(), rootCause, SpelMessage.EXCEPTION_DURING_METHOD_INVOCATION,
this.name, state.getActiveContextObject().getValue().getClass().getName(), rootCause.getMessage());
throw new ExpressionInvocationTargetException( getStartPosition(),
"A problem occurred when trying to execute method '"+this.name+"' on object of type '"+state.getActiveContextObject().getValue().getClass().getName()+"'",
rootCause);
}
}
}

26
org.springframework.expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java

@ -28,6 +28,7 @@ import org.junit.Test; @@ -28,6 +28,7 @@ import org.junit.Test;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionInvocationTargetException;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodFilter;
import org.springframework.expression.MethodResolver;
@ -146,7 +147,7 @@ public class MethodInvocationTests extends ExpressionTestCase { @@ -146,7 +147,7 @@ public class MethodInvocationTests extends ExpressionTestCase {
Assert.fail();
} catch (Exception e) {
// 4 means it will throw a checked exception - this will be wrapped
if (!(e instanceof SpelEvaluationException)) {
if (!(e instanceof ExpressionInvocationTargetException)) {
e.printStackTrace();
Assert.fail("Should have been wrapped");
}
@ -183,6 +184,29 @@ public class MethodInvocationTests extends ExpressionTestCase { @@ -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
public void testMethodFiltering_SPR6764() {
SpelExpressionParser parser = new SpelExpressionParser();

Loading…
Cancel
Save