Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Copyright 2004-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT c;pWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.codehaus.groovy.runtime;
 
 
 import java.util.List;
 
Originally was grails.utils.GrailsUtils, removed some grails specific stuff. Utility methods removing internal lines from stack traces

Author(s):
Graeme Rocher
Version:
$Revision: 5544 $ First Created: 02-Jun-2006 Last Updated: $Date: 2007-09-21 13:53:07 -0500 (Fri, 21 Sep 2007) $
Since:
0.2
 
 public class StackTraceUtils {
 
     public static final String STACK_LOG_NAME = "StackTrace";
     private static final Logger STACK_LOG;
     // set log to consume traces by default, end user can override later
     static {
         outer: do {
             Enumeration existingLogs = LogManager.getLogManager().getLoggerNames();
             while (existingLogs.hasMoreElements()) {
                 if (.equals(existingLogs.nextElement())) {
                      = Logger.getLogger();
                     break outer;
                 }
             }
              = Logger.getLogger();
             .setUseParentHandlers(false);
         } while (false);
     }
 
     private static final String[] GROOVY_PACKAGES =
             System.getProperty("groovy.sanitized.stacktraces",
                 "groovy.," +
                 "org.codehaus.groovy.," +
                 "java.," +
                 "javax.," +
                 "sun.," +
                 "gjdk.groovy.,"
             ).split("(\\s|,)+");
 
     private static List<Closuretests = new ArrayList<Closure>();
    
    

Adds a groovy.lang.Closure to test whether the stack trace element should be added or not.

The groovy.lang.Closure will be given the class name as parameter. the return value decides if the element will be added or not.

  • true - trace element will be added to the trace
  • false - trace element will not be added to the trace
  • null - continue with next test
Groovy truth will be used to determine true and false, null is excluded from defaulting to false here. If all tests have been executed and all of them skipped, then the groovy standard filtering will take place.

Parameters:
test the testing groovy.lang.Closure
 
     public static void addClassTest(Closure test) {
       .add(test);  
     }    
    
    

Remove all apparently groovy-internal trace entries from the exception instance

This modifies the original instance and returns it, it does not clone

Parameters:
t
Returns:
The exception passed in, after cleaning the stack trace
 
     public static Throwable sanitize(Throwable t) {
         // Note that this getBoolean access may well be synced...
         if (!Boolean.getBoolean("groovy.full.stacktrace")) {
            StackTraceElement[] trace = t.getStackTrace();
            List newTrace = new ArrayList();
            for (int i = 0; i < trace.lengthi++) {
                StackTraceElement stackTraceElement = trace[i];
                if (isApplicationClass(stackTraceElement.getClassName())) {
                    newTrace.addstackTraceElement);
                }
            }
            // We don't want to lose anything, so log it
  	        .log(."Sanitizing stacktrace:"t);
            StackTraceElement[] clean = new StackTraceElement[newTrace.size()];
            newTrace.toArray(clean);
            t.setStackTrace(clean);
        }
        return t;
    }
    public static void printSanitizedStackTrace(Throwable tPrintWriter p) {
        t = StackTraceUtils.sanitize(t);
        StackTraceElement[] trace = t.getStackTrace();
        for (int i = 0; i < trace.lengthi++) {
            StackTraceElement stackTraceElement = trace[i];
            p.println(  "at "+stackTraceElement.getClassName()
                        +"("+stackTraceElement.getMethodName()
                        +":"+stackTraceElement.getLineNumber()+")");
        }
    }
    public static void printSanitizedStackTrace(Throwable t) {
    }
    public static boolean isApplicationClass(String className) {
        for(Closure test : ) {
          Object result = test.call(className);
          if(result != null) {
              return DefaultTypeTransformation.castToBoolean(result);
          }
        }
        
        for (int i = 0; i < .i++) {
            String groovyPackage = [i];
            if (className.startsWith(groovyPackage)) {
                return false;
            }
        }
        return true;
    }

    

Extracts the root cause of the exception, no matter how nested it is

Parameters:
t
Returns:
The deepest cause of the exception that can be found
    public static Throwable extractRootCause(Throwable t) {
        Throwable result = t;
        while (result.getCause() != null) {
            result = result.getCause();
        }
        return result;
    }

    

Get the root cause of an exception and sanitize it for display to the user

This will MODIFY the stacktrace of the root cause exception object and return it

Parameters:
t
Returns:
The root cause exception instance, with its stace trace modified to filter out groovy runtime classes
    public static Throwable sanitizeRootCause(Throwable t) {
        return StackTraceUtils.sanitize(StackTraceUtils.extractRootCause(t));
    }

    

Sanitize the exception and ALL nested causes

This will MODIFY the stacktrace of the exception instance and all its causes irreversibly

Parameters:
t
Returns:
The root cause exception instances, with stack trace modified to filter out groovy runtime classes
    public static Throwable deepSanitize(Throwable t) {
        Throwable current = t;
        while (current.getCause() != null) {
            current = StackTraceUtils.sanitize(current.getCause());
        }
        return StackTraceUtils.sanitize(t);
    }
New to GrepCode? Check out our FAQ X