| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| OgnlException |
|
| 1.6;1.6 |
| 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 | 1 | initCause = OgnlException.class.getMethod( "initCause", new Class[] { Throwable.class } ); |
| 44 | } | |
| 45 | 0 | catch ( NoSuchMethodException e ) |
| 46 | { | |
| 47 | /** ignore */ | |
| 48 | 1 | } |
| 49 | 1 | } |
| 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 | 0 | this( null, null ); |
| 67 | 0 | } |
| 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 | 108 | this( msg, null ); |
| 77 | 108 | } |
| 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 | 125 | super( msg ); |
| 88 | 125 | this.reason = reason; |
| 89 | ||
| 90 | 125 | if ( initCause != null ) |
| 91 | { | |
| 92 | try | |
| 93 | { | |
| 94 | 125 | initCause.invoke( this, reason ); |
| 95 | } | |
| 96 | 0 | catch ( Exception ignored ) |
| 97 | { | |
| 98 | /** ignore */ | |
| 99 | 125 | } |
| 100 | } | |
| 101 | 125 | } |
| 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 | 1 | 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 | 0 | 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 | 0 | evaluation = value; |
| 131 | 0 | } |
| 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 | 0 | if ( reason == null ) |
| 142 | { | |
| 143 | 0 | return super.toString(); |
| 144 | } | |
| 145 | ||
| 146 | 0 | 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 | 0 | printStackTrace( System.err ); |
| 156 | 0 | } |
| 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 | 0 | synchronized ( s ) |
| 165 | { | |
| 166 | 0 | super.printStackTrace( s ); |
| 167 | 0 | if ( reason != null ) |
| 168 | { | |
| 169 | 0 | s.println( "/-- Encapsulated exception ------------\\" ); |
| 170 | 0 | reason.printStackTrace( s ); |
| 171 | 0 | s.println( "\\--------------------------------------/" ); |
| 172 | } | |
| 173 | 0 | } |
| 174 | 0 | } |
| 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 | 0 | synchronized ( s ) |
| 183 | { | |
| 184 | 0 | super.printStackTrace( s ); |
| 185 | 0 | if ( reason != null ) |
| 186 | { | |
| 187 | 0 | s.println( "/-- Encapsulated exception ------------\\" ); |
| 188 | 0 | reason.printStackTrace( s ); |
| 189 | 0 | s.println( "\\--------------------------------------/" ); |
| 190 | } | |
| 191 | 0 | } |
| 192 | 0 | } |
| 193 | } |