All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.StackTraceUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;



import com.hfg.util.collection.CollectionUtil;

import java.util.List;

//------------------------------------------------------------------------------
/**
 Utility functions for manipulating StackTrace information.
 
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class StackTraceUtil
{
   //##########################################################################
   // PUBLIC FUNCTIONS
   //##########################################################################

   //--------------------------------------------------------------------------
   public static String getCurrentMethodName()
   {
      StackTraceElement e[] = Thread.currentThread().getStackTrace();
      return e[2].getMethodName();
   }

   //--------------------------------------------------------------------------
   public static String getCallingMethodName()
   {
      StackTraceElement e[] = Thread.currentThread().getStackTrace();
      return e[3].getMethodName();
   }

   //--------------------------------------------------------------------------
   public static String getCurrentClassName()
   {
      StackTraceElement e[] = Thread.currentThread().getStackTrace();
      return e[2].getClassName();
   }

   //--------------------------------------------------------------------------
   public static String getCallingClassName()
   {
      StackTraceElement e[] = Thread.currentThread().getStackTrace();
      return e[3].getClassName();
   }

   //--------------------------------------------------------------------------
   public static int getCurrentLineNumber()
   {
      StackTraceElement e[] = Thread.currentThread().getStackTrace();
      return e[2].getLineNumber();
   }

   //--------------------------------------------------------------------------
   public static StackTraceElement getCallingStackTraceElement(List inExclusions)
   {
      StackTraceElement caller = null;

      StackTraceElement e[] = Thread.currentThread().getStackTrace();
      // TODO: Is using Throwable faster?
//      StackTraceElement e[] = new Throwable().getStackTrace();
      if (CollectionUtil.hasValues(inExclusions))
      {
         for (int i = 3; i < e.length; i++)
         {
            StackTraceElement element = e[i];

            boolean skip = false;
            for (StackTraceElementFilter exclusionFilter : inExclusions)
            {
               if (exclusionFilter.accept(element))
               {
                  skip = true;
                  break;
               }
            }

            if (! skip)
            {
               caller = element;
               break;
            }
         }
      }
      else
      {
         caller = e[3];
//         caller = e[2];  // for Throwable version
      }

      return caller;
   }

   //--------------------------------------------------------------------------
   /**
    Returns the root exception from an exception.
    * @param inException the exception from which to find the root exception
    * @return Throwable the root exception
    */
   public static Throwable getRootException(Throwable inException)
   {
      Throwable rootException = inException;
      while (rootException.getCause() != null)
      {
         rootException = rootException.getCause();
      }

      return rootException;
   }

   //--------------------------------------------------------------------------
   /**
    Returns a stack trace formatted similarly to the one produced by e.printStackTrace().
    * @param inException the exception from which to get the stack trace
    * @return String version of the stack trace
    */
   public static String getExceptionStackTrace(Throwable inException)
   {

      StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(System.getProperty("line.separator"));


      Throwable e = inException;
      while (e != null)
      {
         StackTraceElement[] stackTrace = e.getStackTrace();

         if (e != inException)
         {
            buffer.delimitedAppend("Caused by: ");
         }

         buffer.append(e.getClass().getName() + ": ");

         if (e.getMessage() != null)
         {
            buffer.append(e.getMessage());
         }

         for (StackTraceElement element : stackTrace)
         {
            buffer.delimitedAppend("\tat ");
            buffer.append(element.toString());
         }

         e = e.getCause();
      }

      return buffer.toString();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy