Advertisement
Guest User

ExceptionUtils.java

a guest
Mar 30th, 2010
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. import static com.google.common.collect.Lists.newArrayList;
  2. import static com.google.common.collect.Sets.newHashSet;
  3.  
  4. import java.io.PrintWriter;
  5. import java.io.StringWriter;
  6. import java.util.List;
  7. import java.util.Set;
  8.  
  9. public class ExceptionUtils {
  10.   private static final String INDENT = "\t";
  11.   private static List<String> _suppressedPackages = newArrayList("org.h2", "org.hibernate", "$Proxy", "org.junit", "java.lang.reflect.Method", "sun.", "org.eclipse", "junit.framework");
  12.  
  13.   public static String getFilteredStackTrace(Throwable t) {
  14.     return getFilteredStackTrace(t, true);
  15.   }
  16.  
  17.   public static String getFilteredStackTrace(Throwable t, boolean shouldFilter) {
  18.     try {
  19.       StringWriter sw = new StringWriter();
  20.       PrintWriter pw = new PrintWriter(sw);
  21.       writeCleanStackTrace(t, pw, shouldFilter);
  22.       return sw.getBuffer().toString();
  23.     }
  24.     catch (Exception e) {
  25.       e.printStackTrace();
  26.       return e.toString();
  27.     }
  28.   }
  29.  
  30.   private static void writeCleanStackTrace(Throwable t, PrintWriter s, boolean wantsFilter) {
  31.     s.print("Exception: ");
  32.     printExceptionChain(t, s);
  33.     Set<String> skippedPackages = newHashSet();
  34.     int skippedLines = 0;
  35.     boolean shouldFilter = wantsFilter && filtersEnabled();
  36.     for (StackTraceElement traceElement : getBottomThrowable(t).getStackTrace()) {
  37.       String forbiddenPackageName = null;
  38.       if (shouldFilter) {
  39.         forbiddenPackageName = tryGetForbiddenPackageName(traceElement);
  40.       }
  41.  
  42.       if (forbiddenPackageName == null) {
  43.         if (skippedPackages.size() > 0) {
  44.           //37 lines skipped for [org.h2, org.hibernate, sun., java.lang.reflect.Method, $Proxy]
  45.           s.println(getSkippedPackagesMessage(skippedPackages, skippedLines));
  46.         }
  47.         //at hib.HibExample.test(HibExample.java:18)
  48.         s.println(INDENT + "at " + traceElement);
  49.         skippedPackages.clear();
  50.         skippedLines = 0;
  51.       }
  52.       else {
  53.         skippedLines++;
  54.         skippedPackages.add(forbiddenPackageName);
  55.       }
  56.     }
  57.     if (skippedLines > 0) {
  58.       s.println(getSkippedPackagesMessage(skippedPackages, skippedLines));
  59.     }
  60.   }
  61.  
  62.   //37 lines skipped for [org.h2, org.hibernate, sun., java.lang.reflect.Method, $Proxy]
  63.   private static String getSkippedPackagesMessage(Set<String> skippedPackages, int skippedLines) {
  64.     return INDENT + skippedLines + " line" + (skippedLines == 1 ? "" : "s") + " skipped for " + skippedPackages;
  65.   }
  66.  
  67.   private static Throwable getBottomThrowable(Throwable t) {
  68.     while (t.getCause() != null) {
  69.       t = t.getCause();
  70.     }
  71.     return t;
  72.   }
  73.  
  74.   /**
  75.    * Check configuration to see if filtering is enabled system-wide
  76.    */
  77.   private static boolean filtersEnabled() {
  78.     return true;
  79.   }
  80.  
  81.   private static void printExceptionChain(Throwable t, PrintWriter s) {
  82.     s.println(t);
  83.     if (t.getCause() != null) {
  84.       s.print("Caused by: ");
  85.       printExceptionChain(t.getCause(), s);
  86.     }
  87.   }
  88.  
  89.   /**
  90.    * Checks to see if the class is part of a forbidden package. If so, it returns the package name from the list of suppressed packages that matches, otherwise it returns null.
  91.    */
  92.   private static String tryGetForbiddenPackageName(StackTraceElement traceElement) {
  93.     String classAndMethod = traceElement.getClassName() + "." + traceElement.getMethodName();
  94.     for (String pkg : _suppressedPackages) {
  95.       if (classAndMethod.startsWith(pkg)) {
  96.         return pkg;
  97.       }
  98.     }
  99.     return null;
  100.   }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement