This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push: new 63234fcff2 ExpressionFactory.coerceToType() should throw ELException on error 63234fcff2 is described below commit 63234fcff2ba3d1bf977f818e6e4e4c17056e04a Author: Mark Thomas <ma...@apache.org> 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 aafb057041..cd7a38130d 100644 --- a/java/org/apache/el/lang/ELSupport.java +++ b/java/org/apache/el/lang/ELSupport.java @@ -35,6 +35,7 @@ import jakarta.el.ELContext; import jakarta.el.ELException; import jakarta.el.LambdaExpression; +import org.apache.el.util.ExceptionUtils; import org.apache.el.util.MessageFactory; @@ -489,7 +490,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 3f7e2e7f4b..034cb2f579 100644 --- a/test/org/apache/el/TestExpressionFactory.java +++ b/test/org/apache/el/TestExpressionFactory.java @@ -17,6 +17,7 @@ package org.apache.el; import jakarta.el.ELContext; +import jakarta.el.ELException; import jakarta.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(); + String 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 b3a1d09dec..bc380db5b3 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -142,6 +142,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: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org