This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new d30d353e20 ExpressionFactory.coerceToType() should throw ELException
on error
d30d353e20 is described below
commit d30d353e20678a6ec853dc1e79ac448f4dd6c96f
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Jun 25 17:02:18 2024 +0100
ExpressionFactory.coerceToType() should throw ELException on error
---
java/org/apache/el/lang/ELSupport.java | 11 +++++++-
test/org/apache/el/TestExpressionFactory.java | 36 +++++++++++++++++++++++++++
webapps/docs/changelog.xml | 5 ++++
3 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/java/org/apache/el/lang/ELSupport.java
b/java/org/apache/el/lang/ELSupport.java
index 6165ccf9f5..0a97b3f9cd 100644
--- a/java/org/apache/el/lang/ELSupport.java
+++ b/java/org/apache/el/lang/ELSupport.java
@@ -30,6 +30,7 @@ import java.util.Set;
import javax.el.ELContext;
import javax.el.ELException;
+import org.apache.el.util.ExceptionUtils;
import org.apache.el.util.MessageFactory;
@@ -484,7 +485,15 @@ public class ELSupport {
} else if (obj instanceof Enum<?>) {
return ((Enum<?>) obj).name();
} else {
- return obj.toString();
+ try {
+ return obj.toString();
+ } catch (ELException e) {
+ // Unlikely but you never know
+ throw e;
+ } catch (Throwable t) {
+ ExceptionUtils.handleThrowable(t);
+ throw new ELException(t);
+ }
}
}
diff --git a/test/org/apache/el/TestExpressionFactory.java
b/test/org/apache/el/TestExpressionFactory.java
index 3396342593..bde88cb2e1 100644
--- a/test/org/apache/el/TestExpressionFactory.java
+++ b/test/org/apache/el/TestExpressionFactory.java
@@ -17,6 +17,7 @@
package org.apache.el;
import javax.el.ELContext;
+import javax.el.ELException;
import javax.el.ExpressionFactory;
import org.junit.Assert;
@@ -45,4 +46,39 @@ public class TestExpressionFactory {
factory.createValueExpression(context, "foo", null);
}
+
+
+ @Test
+ public void testCoerceToTypeString() {
+ ExpressionFactory factory = ExpressionFactory.newInstance();
+ TestObject testObjectA = new TestObject();
+ Object result = factory.coerceToType(testObjectA, String.class);
+ Assert.assertEquals(TestObject.OK, result);
+ }
+
+
+ @Test(expected = ELException.class)
+ public void testCoerceToTypeStringThrowsException() {
+ ExpressionFactory factory = ExpressionFactory.newInstance();
+ TestObjectException testObjectA = new TestObjectException();
+ factory.coerceToType(testObjectA, String.class);
+ }
+
+
+ private static class TestObject{
+
+ private static final String OK = "OK";
+ @Override
+ public String toString() {
+ return OK;
+ }
+ }
+
+ private static class TestObjectException{
+
+ @Override
+ public String toString() {
+ throw new RuntimeException();
+ }
+ }
}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 50101c2f04..8d1f2e267c 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -144,6 +144,11 @@
is aware of new classes added to the <code>java.lang</code> package in
Java 23. (markt)
</fix>
+ <fix>
+ Ensure that an exception in <code>toString()</code> still results in an
+ <code>ELException</code> when an object is coerced to a String using
+ <code>ExpressionFactory.coerceToType()</code>. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Other">
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]