Index: java/ognl/OgnlException.java =================================================================== --- java/ognl/OgnlException.java (revision 141) +++ java/ognl/OgnlException.java (working copy) @@ -30,7 +30,10 @@ //-------------------------------------------------------------------------- package ognl; +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; + /** * Superclass for OGNL exceptions, incorporating an optional encapsulated exception. * @author Luke Blanshard (blanshlu@netscape.net) @@ -73,8 +76,44 @@ { super( msg ); this.reason = reason; + initCauseIfPossible( reason ); } + private void initCauseIfPossible( Throwable cause ) + { + Method initCauseMethod = findMethod( "initCause", new Class[] { Throwable.class } ); + if ( initCauseMethod != null ) + { + invokeMethod( initCauseMethod, this, new Object[] { cause } ); + } + } + + private void invokeMethod( Method method, Object receiver, Object[] arguments ) + { + try { + method.invoke( receiver, arguments ); + } + catch ( IllegalAccessException e ) + { + // ignore - at least we tried + } + catch ( InvocationTargetException e ) + { + // ignore - at least we tried + } + } + + private Method findMethod( String name, Class[] parameterTypes ) + { + try { + return getClass().getMethod( name, parameterTypes ); + } + catch ( NoSuchMethodException e ) + { + return null; + } + } + /** * Returns the encapsulated exception, or null if there is none. * @return the encapsulated exception