| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.io.PrintWriter; |
| 36 | | | import java.io.StringWriter; |
| 37 | | | import java.lang.reflect.InvocationTargetException; |
| 38 | | | import java.lang.reflect.Method; |
| 39 | | | import java.lang.reflect.Modifier; |
| 40 | | | import java.util.HashMap; |
| 41 | | | import java.util.Map; |
| 42 | | | import java.util.logging.Level; |
| 43 | | | import java.util.logging.Logger; |
| 44 | | | |
| 45 | | | import org.jcoderz.commons.Loggable; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | @author |
| 51 | | | |
| 52 | | | public final class ThrowableUtil |
| 53 | | | { |
| 54 | | | private static final int MAX_REASONABLE_PARAMETER_LENGTH = 10000; |
| 55 | | | |
| 56 | | | private static final String GETTER_METHOD_PREFIX = "get"; |
| 57 | | | |
| 58 | | | private static final String BOOLEAN_GETTER_METHOD_PREFIX = "is"; |
| 59 | | | |
| 60 | 100 | | private static final int GETTER_METHOD_PREFIX_LENGTH |
| 61 | | | = GETTER_METHOD_PREFIX.length(); |
| 62 | | | |
| 63 | 100 | | private static final int BOOLEAN_GETTER_METHOD_PREFIX_LENGTH |
| 64 | | | = BOOLEAN_GETTER_METHOD_PREFIX.length(); |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | private static final Method GET_CAUSE; |
| 71 | | | |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | private static final Method INIT_CAUSE; |
| 77 | | | |
| 78 | 100 | | private static final Object[] EMPTY_ARRAY = new Object[0]; |
| 79 | | | |
| 80 | 75 | | private static final String CLASSNAME = ThrowableUtil.class.getName(); |
| 81 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 82 | | | |
| 83 | | | private static final int MAX_NESTING_DEPTH = 15; |
| 84 | | | |
| 85 | | | static |
| 86 | | | { |
| 87 | 100 | | Method theGetCauseMethod = null; |
| 88 | 100 | | Method theInitCauseMethod = null; |
| 89 | | | try |
| 90 | | | { |
| 91 | 75 | | theGetCauseMethod |
| 92 | | | = Throwable.class.getDeclaredMethod("getCause", new Class[0]); |
| 93 | 71 | | theInitCauseMethod |
| 94 | | | = Throwable.class.getDeclaredMethod("initCause", |
| 95 | | | new Class[] {Throwable.class}); |
| 96 | | | } |
| 97 | 0 | | catch (Exception ex) |
| 98 | | | { |
| 99 | | | |
| 100 | 0 | | logger.log(Level.WARNING, "Could not initialize, will run without.", |
| 101 | | | ex); |
| 102 | 100 | | } |
| 103 | 100 | | GET_CAUSE = theGetCauseMethod; |
| 104 | 100 | | INIT_CAUSE = theInitCauseMethod; |
| 105 | 100 | | } |
| 106 | | | |
| 107 | | | private ThrowableUtil () |
| 108 | 0 | | { |
| 109 | | | |
| 110 | 0 | | } |
| 111 | | | |
| 112 | | | |
| 113 | | | |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | |
| 120 | | | @param |
| 121 | | | |
| 122 | | | public static void fixChaining (Throwable ex) |
| 123 | | | { |
| 124 | | | try |
| 125 | | | { |
| 126 | 100 | | Throwable current = ex; |
| 127 | 100 | | int nesting = 0; |
| 128 | | | while (INIT_CAUSE != null && current != null |
| 129 | 100 | | && !(current instanceof Loggable)) |
| 130 | | | { |
| 131 | 100 | | if (nesting++ > MAX_NESTING_DEPTH) |
| 132 | | | { |
| 133 | 0 | | logger.log(Level.FINE, |
| 134 | | | "Stopped fixing exception nesting cause max depth " |
| 135 | | | + "reached for given exception.", ex); |
| 136 | 0 | | break; |
| 137 | | | } |
| 138 | 100 | | if (current.getCause() == null) |
| 139 | | | { |
| 140 | 100 | | final Method theGetCauseMethod |
| 141 | | | = findGetCauseMethod(current.getClass().getMethods()); |
| 142 | 100 | | if (theGetCauseMethod != null) |
| 143 | | | { |
| 144 | 100 | | initCause(current, theGetCauseMethod); |
| 145 | | | } |
| 146 | | | } |
| 147 | 100 | | current = current.getCause(); |
| 148 | | | } |
| 149 | | | } |
| 150 | 0 | | catch (Exception unexpected) |
| 151 | | | { |
| 152 | | | |
| 153 | 0 | | logger.log( |
| 154 | | | Level.SEVERE, "Unexpected exception, ignored.", unexpected); |
| 155 | 100 | | } |
| 156 | 100 | | } |
| 157 | | | |
| 158 | | | |
| 159 | | | |
| 160 | | | |
| 161 | | | |
| 162 | | | <code></code> |
| 163 | | | |
| 164 | | | {@link } |
| 165 | | | |
| 166 | | | |
| 167 | | | @param |
| 168 | | | |
| 169 | | | public static void collectNestedData (Loggable loggable) |
| 170 | | | { |
| 171 | | | try |
| 172 | | | { |
| 173 | 100 | | Throwable current = loggable.getCause(); |
| 174 | 100 | | int nesting = 0; |
| 175 | 100 | | while (current != null) |
| 176 | | | { |
| 177 | 100 | | if (nesting++ > MAX_NESTING_DEPTH) |
| 178 | | | { |
| 179 | 0 | | logger.log(Level.FINE, |
| 180 | | | "Stopped collecting nested information max depth " |
| 181 | | | + "reached for given exception.", loggable); |
| 182 | 0 | | break; |
| 183 | | | } |
| 184 | 100 | | if (!(current instanceof Loggable)) |
| 185 | | | { |
| 186 | 100 | | collectParameters(loggable, current, nesting); |
| 187 | | | } |
| 188 | 100 | | current = current.getCause(); |
| 189 | | | } |
| 190 | | | } |
| 191 | 0 | | catch (Exception unexpected) |
| 192 | | | { |
| 193 | | | |
| 194 | 0 | | logger.log( |
| 195 | | | Level.SEVERE, "Unexpected exception, ignored.", unexpected); |
| 196 | 100 | | } |
| 197 | 100 | | } |
| 198 | | | |
| 199 | | | |
| 200 | | | |
| 201 | | | |
| 202 | | | @param |
| 203 | | | @return |
| 204 | | | |
| 205 | | (1)(2) | public static Map getProperties(Throwable thr) |
| 206 | | | { |
| 207 | 0 | | final Map result = new HashMap(); |
| 208 | 0 | | final Method[] methods = thr.getClass().getMethods(); |
| 209 | 0 | | for (int i = 0; i < methods.length; i++) |
| 210 | | | { |
| 211 | 0 | | final int modifier = methods[i].getModifiers(); |
| 212 | 0 | | if (methods[i].getDeclaringClass() != Throwable.class |
| 213 | | | && methods[i].getDeclaringClass() != Object.class |
| 214 | | | && methods[i].getParameterTypes().length == 0 |
| 215 | | | && Modifier.isPublic(modifier) |
| 216 | | | && !Modifier.isStatic(modifier) |
| 217 | | | && !methods[i].getReturnType().equals(Void.TYPE)) |
| 218 | | | { |
| 219 | | | try |
| 220 | | | { |
| 221 | 0 | | if (methods[i].getName().startsWith(GETTER_METHOD_PREFIX)) |
| 222 | | | { |
| 223 | 0 | | final Object value |
| 224 | | | = methods[i].invoke(thr, (Object[]) null); |
| 225 | 0 | | final String key |
| 226 | | | = methods[i].getName().substring( |
| 227 | | | GETTER_METHOD_PREFIX_LENGTH); |
| 228 | 0 | | result.put(key, value); |
| 229 | 0 | | } |
| 230 | 0 | | else if (methods[i].getName().startsWith( |
| 231 | | | BOOLEAN_GETTER_METHOD_PREFIX) |
| 232 | | | && (methods[i].getReturnType().equals(Boolean.class) |
| 233 | | | || methods[i].getReturnType().equals( |
| 234 | | | java.lang.Boolean.TYPE))) |
| 235 | | | { |
| 236 | 0 | | final Object value |
| 237 | | | = methods[i].invoke(thr, (Object[]) null); |
| 238 | 0 | | final String key |
| 239 | | | = methods[i].getName().substring( |
| 240 | | | BOOLEAN_GETTER_METHOD_PREFIX_LENGTH); |
| 241 | 0 | | result.put(key, value); |
| 242 | | | } |
| 243 | | | } |
| 244 | 0 | | catch (InvocationTargetException e) |
| 245 | | | { |
| 246 | | | |
| 247 | | | } |
| 248 | 0 | | catch (IllegalArgumentException e) |
| 249 | | | { |
| 250 | | | |
| 251 | | | } |
| 252 | 0 | | catch (IllegalAccessException e) |
| 253 | | | { |
| 254 | | | |
| 255 | 0 | | } |
| 256 | | | } |
| 257 | | | } |
| 258 | 0 | | return result; |
| 259 | | | } |
| 260 | | | |
| 261 | | | |
| 262 | | | |
| 263 | | | @param |
| 264 | | | @return |
| 265 | | | @see |
| 266 | | | |
| 267 | | | public static String toString (Throwable thr) |
| 268 | | | { |
| 269 | 100 | | final StringWriter sw = new StringWriter(); |
| 270 | 100 | | PrintWriter pw = null; |
| 271 | | | try |
| 272 | | | { |
| 273 | 100 | | pw = new PrintWriter(sw); |
| 274 | 100 | | thr.printStackTrace(pw); |
| 275 | | | } |
| 276 | | | finally |
| 277 | | | { |
| 278 | 50 | | IoUtil.close(pw); |
| 279 | 50 | | IoUtil.close(sw); |
| 280 | 100 | | } |
| 281 | 100 | | return sw.toString(); |
| 282 | | | } |
| 283 | | | |
| 284 | | | static Method findGetCauseMethod (final Method[] methods) |
| 285 | | | { |
| 286 | 100 | | Method theGetCauseMethod = null; |
| 287 | 100 | | for (int i = 0; i < methods.length; i++) |
| 288 | | | { |
| 289 | 75 | | if (methods[i].getDeclaringClass() == Throwable.class) |
| 290 | | | { |
| 291 | 100 | | continue; |
| 292 | | | } |
| 293 | 100 | | final int modifier = methods[i].getModifiers(); |
| 294 | 85 | | if (methods[i].getParameterTypes().length == 0 |
| 295 | | | && Modifier.isPublic(modifier) |
| 296 | | | && !Modifier.isStatic(modifier) |
| 297 | | | && Throwable.class.isAssignableFrom(methods[i].getReturnType())) |
| 298 | | | { |
| 299 | | | |
| 300 | | | |
| 301 | 100 | (3) | if (methods[i].getName().equals("getCause")) |
| 302 | | | { |
| 303 | 100 | | theGetCauseMethod = methods[i]; |
| 304 | 100 | | break; |
| 305 | | | } |
| 306 | 0 | | if (theGetCauseMethod != null) |
| 307 | | | { |
| 308 | | | |
| 309 | 0 | | logger.fine("Found 2 matching methods " + theGetCauseMethod |
| 310 | | | + " or " + methods[i] + "."); |
| 311 | 0 | | theGetCauseMethod = null; |
| 312 | 0 | | break; |
| 313 | | | } |
| 314 | 0 | | theGetCauseMethod = methods[i]; |
| 315 | | | } |
| 316 | | | } |
| 317 | 100 | | return theGetCauseMethod; |
| 318 | | | } |
| 319 | | | |
| 320 | | | |
| 321 | | | private static void initCause (Throwable current, Method theGetCauseMethod) |
| 322 | | | { |
| 323 | | | try |
| 324 | | | { |
| 325 | 100 | | final Throwable cause |
| 326 | | | = (Throwable) theGetCauseMethod.invoke( |
| 327 | | | current, EMPTY_ARRAY); |
| 328 | 100 | | INIT_CAUSE.invoke(current, new Object[] {cause}); |
| 329 | | | } |
| 330 | 100 | | catch (Exception e) |
| 331 | | | { |
| 332 | 100 | | logger.log(Level.FINEST, "Failed to init cause for " + current |
| 333 | | | + " using " + theGetCauseMethod + " got a exception.", e); |
| 334 | 100 | | } |
| 335 | 100 | | } |
| 336 | | | |
| 337 | | | private static void collectParameters ( |
| 338 | | | Loggable loggable, Throwable thr, int nesting) |
| 339 | | | throws IllegalAccessException, InvocationTargetException |
| 340 | | | { |
| 341 | 100 | | final Method[] methods = thr.getClass().getMethods(); |
| 342 | 100 | | for (int i = 0; i < methods.length; i++) |
| 343 | | | { |
| 344 | 100 | | final int modifier = methods[i].getModifiers(); |
| 345 | 92 | | if (methods[i].getDeclaringClass() != Throwable.class |
| 346 | | | && methods[i].getDeclaringClass() != Object.class |
| 347 | | | && methods[i].getParameterTypes().length == 0 |
| 348 | | | && methods[i].getExceptionTypes().length == 0 |
| 349 | | | && Modifier.isPublic(modifier) |
| 350 | | | && !Modifier.isStatic(modifier) |
| 351 | | | && methods[i].getName().startsWith(GETTER_METHOD_PREFIX) |
| 352 | | | && !methods[i].getReturnType().equals(Void.TYPE)) |
| 353 | | | { |
| 354 | 100 | | final Object result = methods[i].invoke(thr, (Object[]) null); |
| 355 | 100 | | if (result != null && result != thr.getCause()) |
| 356 | | | { |
| 357 | 100 | | loggable.addParameter( |
| 358 | | | "CAUSE_" + nesting + "_" + thr.getClass().getName() |
| 359 | | | + "#" + methods[i].getName().substring( |
| 360 | | | GETTER_METHOD_PREFIX_LENGTH), |
| 361 | | | asString(result)); |
| 362 | | | } |
| 363 | | | } |
| 364 | | | } |
| 365 | 100 | | } |
| 366 | | | |
| 367 | | | private static String asString (Object obj) |
| 368 | | | { |
| 369 | | | String result; |
| 370 | 100 | | if (obj instanceof Object[]) |
| 371 | | | { |
| 372 | 0 | | result = ArraysUtil.toString((Object[]) obj); |
| 373 | | | } |
| 374 | | | else |
| 375 | | | { |
| 376 | 100 | | result = String.valueOf(obj); |
| 377 | | | } |
| 378 | 100 | | return StringUtil.trimLength( |
| 379 | | | result, |
| 380 | | | MAX_REASONABLE_PARAMETER_LENGTH); |
| 381 | | | } |
| 382 | | | } |