View Javadoc

1   package org.apache.commons.ognl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Method;
23  
24  /**
25   * Superclass for OGNL exceptions, incorporating an optional encapsulated exception.
26   * 
27   * @author Luke Blanshard (blanshlu@netscape.net)
28   * @author Drew Davidson (drew@ognl.org)
29   */
30  public class OgnlException
31      extends Exception
32  {
33      // cache initCause method - if available..to be used during throwable constructor
34      // to properly setup superclass.
35  
36      private static final long serialVersionUID = -842845048743721078L;
37  
38      static Method initCause;
39      static
40      {
41          try
42          {
43              initCause = OgnlException.class.getMethod( "initCause", new Class[] { Throwable.class } );
44          }
45          catch ( NoSuchMethodException e )
46          {
47              /** ignore */
48          }
49      }
50  
51      /**
52       * The root evaluation of the expression when the exception was thrown
53       */
54      private Evaluation evaluation;
55  
56      /**
57       * Why this exception was thrown.
58       * 
59       * @serial
60       */
61      private Throwable reason;
62  
63      /** Constructs an OgnlException with no message or encapsulated exception. */
64      public OgnlException()
65      {
66          this( null, null );
67      }
68  
69      /**
70       * Constructs an OgnlException with the given message but no encapsulated exception.
71       * 
72       * @param msg the exception's detail message
73       */
74      public OgnlException( String msg )
75      {
76          this( msg, null );
77      }
78  
79      /**
80       * Constructs an OgnlException with the given message and encapsulated exception.
81       * 
82       * @param msg the exception's detail message
83       * @param reason the encapsulated exception
84       */
85      public OgnlException( String msg, Throwable reason )
86      {
87          super( msg );
88          this.reason = reason;
89  
90          if ( initCause != null )
91          {
92              try
93              {
94                  initCause.invoke( this, reason );
95              }
96              catch ( Exception ignored )
97              {
98                  /** ignore */
99              }
100         }
101     }
102 
103     /**
104      * Returns the encapsulated exception, or null if there is none.
105      * 
106      * @return the encapsulated exception
107      */
108     public Throwable getReason()
109     {
110         return reason;
111     }
112 
113     /**
114      * Returns the Evaluation that was the root evaluation when the exception was thrown.
115      * 
116      * @return The {@link Evaluation}.
117      */
118     public Evaluation getEvaluation()
119     {
120         return evaluation;
121     }
122 
123     /**
124      * Sets the Evaluation that was current when this exception was thrown.
125      * 
126      * @param value The {@link Evaluation}.
127      */
128     public void setEvaluation( Evaluation value )
129     {
130         evaluation = value;
131     }
132 
133     /**
134      * Returns a string representation of this exception.
135      * 
136      * @return a string representation of this exception
137      */
138     @Override
139     public String toString()
140     {
141         if ( reason == null )
142         {
143             return super.toString();
144         }
145 
146         return super.toString() + " [" + reason + "]";
147     }
148 
149     /**
150      * Prints the stack trace for this (and possibly the encapsulated) exception on System.err.
151      */
152     @Override
153     public void printStackTrace()
154     {
155         printStackTrace( System.err );
156     }
157 
158     /**
159      * Prints the stack trace for this (and possibly the encapsulated) exception on the given print stream.
160      */
161     @Override
162     public void printStackTrace( java.io.PrintStream s )
163     {
164         synchronized ( s )
165         {
166             super.printStackTrace( s );
167             if ( reason != null )
168             {
169                 s.println( "/-- Encapsulated exception ------------\\" );
170                 reason.printStackTrace( s );
171                 s.println( "\\--------------------------------------/" );
172             }
173         }
174     }
175 
176     /**
177      * Prints the stack trace for this (and possibly the encapsulated) exception on the given print writer.
178      */
179     @Override
180     public void printStackTrace( java.io.PrintWriter s )
181     {
182         synchronized ( s )
183         {
184             super.printStackTrace( s );
185             if ( reason != null )
186             {
187                 s.println( "/-- Encapsulated exception ------------\\" );
188                 reason.printStackTrace( s );
189                 s.println( "\\--------------------------------------/" );
190             }
191         }
192     }
193 }