| Line | Hits | Note | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * $Id: StackTraceInfo.java 1011 2008-06-16 17:57:36Z amandel $ | ||
| 3 | * | ||
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above | ||
| 13 | * copyright notice, this list of conditions and the following | ||
| 14 | * disclaimer in the documentation and/or other materials | ||
| 15 | * provided with the distribution. | ||
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of | ||
| 17 | * its contributors may be used to endorse or promote products | ||
| 18 | * derived from this software without specific prior written | ||
| 19 | * permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS | ||
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | ||
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | package org.jcoderz.commons.logging; | ||
| 34 | |||
| 35 | import java.nio.CharBuffer; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * This helper class wraps the information of one stack trace line. | ||
| 39 | * | ||
| 40 | */ | ||
| 41 | (1) | public class StackTraceInfo | |
| 42 | { | ||
| 43 | private static final String CURRENT_LINE_ERROR_TEXT | ||
| 44 | = "The current line is not a "; | ||
| 45 | |||
| 46 | private final CharBuffer mStackTraceLine; | ||
| 47 | |||
| 48 | /** Flag whether an 'at ...' line. */ | ||
| 49 | private final boolean mIsLocationLine; | ||
| 50 | /** Flag whether an 'Caused by: ' line. */ | ||
| 51 | private final boolean mIsCauseLine; | ||
| 52 | /** Flag whether an exception message line. */ | ||
| 53 | private final boolean mIsExceptionMessageLine; | ||
| 54 | /** Flag whether an '... nnn more ' line. */ | ||
| 55 | private final boolean mIsMoreLine; | ||
| 56 | |||
| 57 | private final CharBuffer mClassName; | ||
| 58 | private final CharBuffer mMethodName; | ||
| 59 | private final int mLine; | ||
| 60 | private final int mMoreLines; | ||
| 61 | |||
| 62 | private final CharBuffer mExceptionText; | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Constructs this and sets the information of an 'at ...' stack trace line. | ||
| 66 | * | ||
| 67 | * @param lineData Contains the original line data. | ||
| 68 | * @param className The parsed class name. | ||
| 69 | * @param methodName The parsed method name. | ||
| 70 | * @param line The parsed line number, is <= 0 if not available. | ||
| 71 | */ | ||
| 72 | StackTraceInfo ( | ||
| 73 | final CharBuffer lineData, | ||
| 74 | final CharBuffer className, | ||
| 75 | final CharBuffer methodName, | ||
| 76 | final int line) | ||
| 77 | 0 | { | |
| 78 | 0 | mStackTraceLine = lineData; | |
| 79 | |||
| 80 | 0 | mIsLocationLine = true; | |
| 81 | 0 | mIsCauseLine = false; | |
| 82 | 0 | mIsExceptionMessageLine = false; | |
| 83 | 0 | mIsMoreLine = false; | |
| 84 | 0 | mClassName = className; | |
| 85 | 0 | mMethodName = methodName; | |
| 86 | 0 | mLine = line; | |
| 87 | 0 | mMoreLines = -1; | |
| 88 | 0 | mExceptionText = null; | |
| 89 | 0 | } | |
| 90 | |||
| 91 | /** | ||
| 92 | * Constructs this and sets the information of an '... nnn more' stack trace | ||
| 93 | * line. | ||
| 94 | * | ||
| 95 | * @param lineData Contains the original line data. | ||
| 96 | * @param lines The parsed number of lines being omitted. | ||
| 97 | */ | ||
| 98 | StackTraceInfo ( | ||
| 99 | final CharBuffer lineData, | ||
| 100 | final int lines) | ||
| 101 | 0 | { | |
| 102 | 0 | mStackTraceLine = lineData; | |
| 103 | |||
| 104 | 0 | mIsLocationLine = false; | |
| 105 | 0 | mIsCauseLine = false; | |
| 106 | 0 | mIsExceptionMessageLine = false; | |
| 107 | 0 | mIsMoreLine = true; | |
| 108 | 0 | mClassName = null; | |
| 109 | 0 | mMethodName = null; | |
| 110 | 0 | mLine = -1; | |
| 111 | 0 | mMoreLines = lines; | |
| 112 | 0 | mExceptionText = null; | |
| 113 | 0 | } | |
| 114 | |||
| 115 | /** | ||
| 116 | * Constructs this and sets the information of an stack trace line, which | ||
| 117 | * contains the exception message. | ||
| 118 | * | ||
| 119 | * @param lineData Contains the original line data. | ||
| 120 | * @param message The exception message as it is parsed from | ||
| 121 | * <code>lineData</code>. | ||
| 122 | * @param isCause Flag whether the exception is a cause, i.e. whether the | ||
| 123 | * message ahs a 'Caused by: ' prefix. | ||
| 124 | */ | ||
| 125 | StackTraceInfo ( | ||
| 126 | final CharBuffer lineData, | ||
| 127 | final CharBuffer message, | ||
| 128 | final boolean isCause) | ||
| 129 | 0 | { | |
| 130 | 0 | mStackTraceLine = lineData; | |
| 131 | |||
| 132 | 0 | mIsLocationLine = false; | |
| 133 | 0 | mIsCauseLine = isCause; | |
| 134 | 0 | (2) | mIsExceptionMessageLine = ! isCause; |
| 135 | 0 | mIsMoreLine = false; | |
| 136 | 0 | mClassName = null; | |
| 137 | 0 | mMethodName = null; | |
| 138 | 0 | mLine = -1; | |
| 139 | 0 | mMoreLines = -1; | |
| 140 | 0 | mExceptionText = message; | |
| 141 | 0 | } | |
| 142 | |||
| 143 | /** | ||
| 144 | * Returns the wrapped stack trace line as string. | ||
| 145 | * | ||
| 146 | * @return The stack trace line. | ||
| 147 | * | ||
| 148 | * @see java.lang.Object#toString() | ||
| 149 | */ | ||
| 150 | public String toString () | ||
| 151 | { | ||
| 152 | 0 | return String.valueOf(mStackTraceLine); | |
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Gets flag whether the current line is a caused-by line. | ||
| 157 | * | ||
| 158 | * @return true if the current line is a caused-by line; false, else. | ||
| 159 | */ | ||
| 160 | boolean isCauseLine () | ||
| 161 | { | ||
| 162 | 0 | return mIsCauseLine; | |
| 163 | } | ||
| 164 | |||
| 165 | /** | ||
| 166 | * Gets flag whether the current line contained the exception message with or | ||
| 167 | * without the 'Caused by:' prefix. So whenever {@linkplain #isCauseLine()} | ||
| 168 | * returns true, this returns true as well. | ||
| 169 | * | ||
| 170 | * @return true if the current line contained the exception message with or | ||
| 171 | * without the cause-by prefix; false, else | ||
| 172 | */ | ||
| 173 | boolean isExceptionMessageLine () | ||
| 174 | { | ||
| 175 | 0 | return (mIsExceptionMessageLine || mIsCauseLine); | |
| 176 | } | ||
| 177 | |||
| 178 | /** | ||
| 179 | * Gets the exception message if the last line has been a cause line or a | ||
| 180 | * exception message line. | ||
| 181 | * | ||
| 182 | * @return Returns the exception message. | ||
| 183 | * | ||
| 184 | * @throws IllegalStateException if the recent line was not a cause line or | ||
| 185 | * exception message line. | ||
| 186 | */ | ||
| 187 | String getExceptionMessage () | ||
| 188 | { | ||
| 189 | 0 | if (! (mIsCauseLine || mIsExceptionMessageLine)) | |
| 190 | { | ||
| 191 | 0 | throw new IllegalStateException("The current line did not contain the " | |
| 192 | + "exception message"); | ||
| 193 | } | ||
| 194 | 0 | return mExceptionText.toString(); | |
| 195 | } | ||
| 196 | |||
| 197 | /** | ||
| 198 | * Gets flag whether the current line is an at ... line. | ||
| 199 | * | ||
| 200 | * @return true if the current line is an at ... line; false, else. | ||
| 201 | */ | ||
| 202 | boolean isLocationLine () | ||
| 203 | { | ||
| 204 | 0 | return mIsLocationLine; | |
| 205 | } | ||
| 206 | |||
| 207 | /** | ||
| 208 | * Gets the class name from an at-line. | ||
| 209 | * | ||
| 210 | * @return class name of an at-line | ||
| 211 | * | ||
| 212 | * @throws IllegalStateException if the recent line is not an at-line. | ||
| 213 | */ | ||
| 214 | String getClassName () | ||
| 215 | { | ||
| 216 | 0 | (3) | if (! mIsLocationLine) |
| 217 | { | ||
| 218 | 0 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT | |
| 219 | + "'at ...' line"); | ||
| 220 | } | ||
| 221 | 0 | return mClassName.toString(); | |
| 222 | } | ||
| 223 | |||
| 224 | /** | ||
| 225 | * Gets the method name from an at-line. | ||
| 226 | * | ||
| 227 | * @return method of an at-line | ||
| 228 | * | ||
| 229 | * @throws IllegalStateException if the recent line is not an at-line. | ||
| 230 | */ | ||
| 231 | String getMethodName () | ||
| 232 | { | ||
| 233 | 0 | if (! mIsLocationLine) | |
| 234 | { | ||
| 235 | 0 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT | |
| 236 | + "'at ...' line"); | ||
| 237 | } | ||
| 238 | 0 | return mMethodName.toString(); | |
| 239 | } | ||
| 240 | |||
| 241 | /** | ||
| 242 | * Gests the line info of an at-line. This might not be accessible, in which | ||
| 243 | * case 0 is returned. | ||
| 244 | * | ||
| 245 | * @return line information of at-line or 0 if no such. | ||
| 246 | * | ||
| 247 | * @throws IllegalStateException if the recent line is not an at-line. | ||
| 248 | */ | ||
| 249 | int getLine () | ||
| 250 | { | ||
| 251 | 0 | if (! mIsLocationLine) | |
| 252 | { | ||
| 253 | 0 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT | |
| 254 | + "'at ...' line"); | ||
| 255 | } | ||
| 256 | 0 | return mLine; | |
| 257 | } | ||
| 258 | |||
| 259 | /** | ||
| 260 | * Gets flag whether the current line is an ... nnn more line. | ||
| 261 | * | ||
| 262 | * @return true if the current line is an ... more line; false, else. | ||
| 263 | */ | ||
| 264 | boolean isMoreLine () | ||
| 265 | { | ||
| 266 | 0 | return mIsMoreLine; | |
| 267 | } | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Gets the number of stack trace items being concentrated into the more | ||
| 271 | * line. | ||
| 272 | * | ||
| 273 | * @return number of stack trace lines being neglected. | ||
| 274 | * | ||
| 275 | * @throws IllegalStateException if the recent line was not a more-line. | ||
| 276 | */ | ||
| 277 | int getMoreLines () | ||
| 278 | { | ||
| 279 | 0 | if (! mIsMoreLine) | |
| 280 | { | ||
| 281 | 0 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT | |
| 282 | + "'... nnn more' line"); | ||
| 283 | } | ||
| 284 | 0 | return mMoreLines; | |
| 285 | } | ||
| 286 | } |