| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Throwables |
|
| 0.0;0 | ||||
| Throwables$1 |
|
| 0.0;0 | ||||
| Throwables$IsThrownBy |
|
| 0.0;0 |
| 1 | /* | |
| 2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
| 3 | Licensed under the Academic Free License version 3.0 | |
| 4 | */ | |
| 5 | ||
| 6 | package jaggregate; | |
| 7 | ||
| 8 | import static jaggregate.internal.ArgumentChecks.*; | |
| 9 | ||
| 10 | /** | |
| 11 | * Utility class that offers useful operations on exceptions. | |
| 12 | * | |
| 13 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 14 | * @version $Id: Throwables.java,v 1.6 2008/10/03 19:01:23 pholser Exp $ | |
| 15 | */ | |
| 16 | public class Throwables { | |
| 17 | /** | |
| 18 | * Discourages instantiation. | |
| 19 | * | |
| 20 | * @throws UnsupportedOperationException always | |
| 21 | */ | |
| 22 | 1 | protected Throwables() { |
| 23 | 1 | throw new UnsupportedOperationException(); |
| 24 | } | |
| 25 | ||
| 26 | /** | |
| 27 | * Answers a predicate which will answer {@code true} if its argument is an exception | |
| 28 | * which was raised from code in the given class. | |
| 29 | * <p/> | |
| 30 | * An exception is considered to have been raised from a given class if its zeroth | |
| 31 | * {@linkplain StackTraceElement stack trace element} mentions the class | |
| 32 | * {@linkplain StackTraceElement#getClassName() by name}. | |
| 33 | * | |
| 34 | * @param thrower the comparand to use with the returned predicate | |
| 35 | * @return a predicate that can be used to compare arguments with {@code thrower} | |
| 36 | */ | |
| 37 | public static UnaryPredicate<Throwable> isThrownBy( Class<?> thrower ) { | |
| 38 | 6 | ensureNotNull( thrower, "thrower" ); |
| 39 | ||
| 40 | 5 | return new IsThrownBy().bindSecond( thrower ); |
| 41 | } | |
| 42 | ||
| 43 | 15 | private static class IsThrownBy extends BinaryPredicate<Throwable, Class<?>> { |
| 44 | public boolean matches( Throwable first, Class<?> second ) { | |
| 45 | 5 | StackTraceElement[] frames = first.getStackTrace(); |
| 46 | 4 | if ( frames.length == 0 ) |
| 47 | 1 | return false; |
| 48 | ||
| 49 | try { | |
| 50 | 3 | Class<?> throwingClass = Class.forName( frames[ 0 ].getClassName() ); |
| 51 | 2 | return second.equals( throwingClass ); |
| 52 | } | |
| 53 | 1 | catch ( ClassNotFoundException ex ) { |
| 54 | 1 | throw new RuntimeException( ex ); |
| 55 | } | |
| 56 | } | |
| 57 | } | |
| 58 | } |