Get StackTraceElement : Exception « Reflection « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Reflection » Exception 




Get StackTraceElement
  
//----------------------------------------------------------------------------//
//                                                                            //
//                             C l a s s U t i l                              //
//                                                                            //
//  Copyright (C) Herve Bitteur 2000-2009. All rights reserved.               //
//  This software is released under the GNU General Public License.           //
//  Please contact [email protected] to report bugs & suggestions. //
//----------------------------------------------------------------------------//
//


/**
 * Class <code>ClassUtil</code> provides utilities related to Class handling.
 *
 @author Herv&eacute Bitteur
 @version $Id: ClassUtil.java,v 1.3 2009/03/03 19:45:51 hbitteur Exp $
 */
public class ClassUtil
{
    //~ Methods ----------------------------------------------------------------

    //-----------------//
    // getCallingFrame //
    //-----------------//
    /**
     * Infer the calling frame, skipping the given classes if so provided.
     * Code was derived from a private method found in the JDK Logger class
     *
     @param skippedClasses the classes to skip
     @return the frame found, just before the skipped classes (or just before
     * the caller of this method)
     */
    public static StackTraceElement getCallingFrame (Class... skippedClasses)
    {
        // Get the current stack trace.
        StackTraceElement[] stack = (new Throwable()).getStackTrace();

        // Simple case, no classes to skip, just return the caller of the caller
        if (skippedClasses.length == 0) {
            return stack[2];
        }

        // More complex case, return the caller, just before the skipped classes
        // First, search back to a method in the skipped classes, if any
        int ix;
        searchingForSkipped: 
        for (ix = 0; ix < stack.length; ix++) {
            StackTraceElement frame = stack[ix];
            String            cname = frame.getClassName();

            for (Class skipped : skippedClasses) {
                if (cname.equals(skipped.getName())) {
                    break searchingForSkipped;
                }
            }
        }

        // Now search for the first frame before the skipped classes
        searchingForNonSkipped: 
        for (; ix < stack.length; ix++) {
            StackTraceElement frame = stack[ix];
            String            cname = frame.getClassName();

            for (Class skipped : skippedClasses) {
                if (cname.equals(skipped.getName())) {
                    continue searchingForNonSkipped;
                }
            }

            // We've found the relevant frame.
            return frame;
        }

        // We haven't found a suitable frame
        return null;
    }

    //--------//
    // nameOf //
    //--------//
    /**
     * Report the full name of the object class, without the package information
     *
     @param obj the object to name
     @return the concatenation of (enclosing) simple names
     */
    public static String nameOf (Object obj)
    {
        StringBuilder sb = new StringBuilder();

        for (Class cl = obj.getClass(); cl != null;
             cl = cl.getEnclosingClass()) {
            if (sb.length() 0) {
                sb.insert(0"-");
            }

            sb.insert(0, cl.getSimpleName());
        }

        return sb.toString();
    }
}

   
    
  














Related examples in the same category
1.whether method declares the given exception or one of its superclasses
2.Is Checked Exception
3.Check whether given exception is compatible with the exceptions declared in a throws clause
4.Utilities to use Java reflection without all of the checked exceptions
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.