From e46b29447b1da479ace4057528b4df79ed422b6b Mon Sep 17 00:00:00 2001 From: Pascal Davoust Date: Thu, 18 Nov 2021 11:33:58 +0100 Subject: [PATCH 1/3] OGNL-261 Avoid collecting stacktrace when building an OgnlException --- src/main/java/ognl/OgnlException.java | 42 ++++++--------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/src/main/java/ognl/OgnlException.java b/src/main/java/ognl/OgnlException.java index 3b7ccb4b..04656857 100644 --- a/src/main/java/ognl/OgnlException.java +++ b/src/main/java/ognl/OgnlException.java @@ -41,27 +41,11 @@ */ public class OgnlException extends Exception { - // cache initCause method - if available..to be used during throwable constructor - // to properly setup superclass. - - static Method _initCause; - static { - try { - _initCause = OgnlException.class.getMethod("initCause", new Class[] { Throwable.class}); - } catch (NoSuchMethodException e) { /** ignore */ } - } - - /** + /** * The root evaluation of the expression when the exception was thrown */ private Evaluation _evaluation; - /** - * Why this exception was thrown. - * @serial - */ - private Throwable _reason; - /** Constructs an OgnlException with no message or encapsulated exception. */ public OgnlException() { @@ -84,15 +68,7 @@ public OgnlException( String msg ) */ public OgnlException( String msg, Throwable reason ) { - super( msg ); - this._reason = reason; - - if (_initCause != null) - { - try { - _initCause.invoke(this, new Object[] { reason }); - } catch (Exception t) { /** ignore */ } - } + super( msg , reason, true, false); } /** @@ -101,7 +77,7 @@ public OgnlException( String msg, Throwable reason ) */ public Throwable getReason() { - return _reason; + return getCause(); } /** @@ -130,10 +106,10 @@ public void setEvaluation(Evaluation value) */ public String toString() { - if ( _reason == null ) + if ( getCause() == null ) return super.toString(); - return super.toString() + " [" + _reason + "]"; + return super.toString() + " [" + getCause() + "]"; } @@ -155,9 +131,9 @@ public void printStackTrace(java.io.PrintStream s) synchronized (s) { super.printStackTrace(s); - if ( _reason != null ) { + if ( getCause() != null ) { s.println( "/-- Encapsulated exception ------------\\" ); - _reason.printStackTrace(s); + getCause().printStackTrace(s); s.println( "\\--------------------------------------/" ); } } @@ -172,9 +148,9 @@ public void printStackTrace(java.io.PrintWriter s) synchronized (s) { super.printStackTrace(s); - if ( _reason != null ) { + if ( getCause() != null ) { s.println( "/-- Encapsulated exception ------------\\" ); - _reason.printStackTrace(s); + getCause().printStackTrace(s); s.println( "\\--------------------------------------/" ); } } From 6ddf10a160aa38e93fc66845a8eaf910b85e8e7f Mon Sep 17 00:00:00 2001 From: Pascal Davoust Date: Fri, 19 Nov 2021 09:59:46 +0100 Subject: [PATCH 2/3] OGNL-261 Avoid collecting stacktrace when building an OgnlException Added protected method to allow control on exception suppression and stacktrace collections to sub-classes. --- src/main/java/ognl/OgnlException.java | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/ognl/OgnlException.java b/src/main/java/ognl/OgnlException.java index 04656857..c9666f1b 100644 --- a/src/main/java/ognl/OgnlException.java +++ b/src/main/java/ognl/OgnlException.java @@ -30,9 +30,6 @@ //-------------------------------------------------------------------------- package ognl; -import java.lang.reflect.Method; - - /** * Superclass for OGNL exceptions, incorporating an optional encapsulated exception. * @@ -71,6 +68,15 @@ public OgnlException( String msg, Throwable reason ) super( msg , reason, true, false); } + /** + * Constructs an OgnlException with the given message and encapsulated exception, + * with control on exception suppression and stack trace collection. + * See @ {@code java.lang.Throwable.Throwable(String, Throwable, boolean, boolean)} for more info. + */ + protected OgnlException(String message, Throwable reason, boolean enableSuppression, boolean writableStackTrace) { + super(message, reason, enableSuppression, writableStackTrace); + } + /** * Returns the encapsulated exception, or null if there is none. * @return the encapsulated exception @@ -112,16 +118,6 @@ public String toString() return super.toString() + " [" + getCause() + "]"; } - - /** - * Prints the stack trace for this (and possibly the encapsulated) exception on - * System.err. - */ - public void printStackTrace() - { - printStackTrace( System.err ); - } - /** * Prints the stack trace for this (and possibly the encapsulated) exception on the * given print stream. From 65776a078915c70deecb2dfaa8904f0e9f2187d7 Mon Sep 17 00:00:00 2001 From: Pascal Davoust Date: Fri, 19 Nov 2021 10:05:07 +0100 Subject: [PATCH 3/3] OGNL-261 Avoid collecting stacktrace when building an OgnlException Remove printStackTrace overrides as this is now handled by base class already. Fixed typo in protected constructor's javadoc --- src/main/java/ognl/OgnlException.java | 36 +-------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/src/main/java/ognl/OgnlException.java b/src/main/java/ognl/OgnlException.java index c9666f1b..f7c216ef 100644 --- a/src/main/java/ognl/OgnlException.java +++ b/src/main/java/ognl/OgnlException.java @@ -71,7 +71,7 @@ public OgnlException( String msg, Throwable reason ) /** * Constructs an OgnlException with the given message and encapsulated exception, * with control on exception suppression and stack trace collection. - * See @ {@code java.lang.Throwable.Throwable(String, Throwable, boolean, boolean)} for more info. + * See {@code java.lang.Throwable.Throwable(String, Throwable, boolean, boolean)} for more info. */ protected OgnlException(String message, Throwable reason, boolean enableSuppression, boolean writableStackTrace) { super(message, reason, enableSuppression, writableStackTrace); @@ -117,38 +117,4 @@ public String toString() return super.toString() + " [" + getCause() + "]"; } - - /** - * Prints the stack trace for this (and possibly the encapsulated) exception on the - * given print stream. - */ - public void printStackTrace(java.io.PrintStream s) - { - synchronized (s) - { - super.printStackTrace(s); - if ( getCause() != null ) { - s.println( "/-- Encapsulated exception ------------\\" ); - getCause().printStackTrace(s); - s.println( "\\--------------------------------------/" ); - } - } - } - - /** - * Prints the stack trace for this (and possibly the encapsulated) exception on the - * given print writer. - */ - public void printStackTrace(java.io.PrintWriter s) - { - synchronized (s) - { - super.printStackTrace(s); - if ( getCause() != null ) { - s.println( "/-- Encapsulated exception ------------\\" ); - getCause().printStackTrace(s); - s.println( "\\--------------------------------------/" ); - } - } - } }