Source Code Cross Referenced for StackIntrospector.java in  » Profiler » MessAdmin » clime » messadmin » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Profiler » MessAdmin » clime.messadmin.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * 
003:         */package clime.messadmin.utils;
004:
005:        /**
006:         * A set of utilities to inspect current stack frame.
007:         * Heavily inspired by, and based on, Apache LogKit's org.apache.log.util.StackIntrospector
008:         * 
009:         * Note: this information is also available since Java 1.4 via
010:         * {@link Throwable#getStackTrace()}
011:         * 
012:         * @author Cédrik LIME
013:         */
014:        public final class StackIntrospector {
015:            /* Magic number CALL_CONTEXT_OFFSET identifies our caller */
016:            private static final int CALL_CONTEXT_OFFSET = 2; // may need to change if this class is redesigned
017:
018:            /**
019:             * Hack to get the call stack as an array of classes.
020:             * The SecurityManager class provides this information as a protected method,
021:             * so all we have to do is violate OO encapsulation principles and
022:             * permit its access through a new public method!
023:             */
024:            private static final class ClassContext extends SecurityManager {
025:                /**
026:                 * @throws SecurityException if an existing SecurityManager disallows construction of another SecurityManager
027:                 */
028:                ClassContext() {
029:                    super ();
030:                }
031:
032:                /**
033:                 * Returns the current execution stack as an array of classes.<br>
034:                 * The length of the array is the number of methods on the execution
035:                 * stack. The element at index <code>0</code> is the class of the
036:                 * currently executing method, the element at index <code>1</code> is
037:                 * the class of that method's caller, and so on.
038:                 * 
039:                 * @return the execution stack.
040:                 * @see @link SecurityManager#getClassContext()
041:                 */
042:                public Class[] get() {
043:                    return getClassContext();
044:                }
045:            }
046:
047:            /**
048:             * Create Hack SecurityManager to get ClassContext
049:             * @throws SecurityException if an existing SecurityManager disallows construction of another SecurityManager
050:             */
051:            private static final ClassContext CLASS_CONTEXT = new ClassContext();
052:
053:            /**
054:             * Private constructor to block instantiation.
055:             */
056:            private StackIntrospector() {
057:                /* assert false; */
058:            }
059:
060:            /**
061:             * Find our caller's caller.
062:             * May return null if caller not found on execution stack.
063:             */
064:            public static Class getCallerClass() {
065:                Class[] stack = CLASS_CONTEXT.get();
066:                if (stack.length <= CALL_CONTEXT_OFFSET + 1) {
067:                    return null;
068:                }
069:                Class c = stack[CALL_CONTEXT_OFFSET + 1];
070:                return c;
071:            }
072:
073:            /**
074:             * Find the caller of the passed in Class.
075:             * May return null if caller not found on execution stack.
076:             *
077:             * @param clazz the Class to search for on stack to find caller of
078:             * @return the Class of object that called parrameter class
079:             */
080:            public static Class getCallerClass(final Class clazz)
081:                    throws SecurityException {
082:                // return getCallerClass(clazz, 0); // can't do that, as this adds a stack frame...
083:                final Class[] stack = CLASS_CONTEXT.get();
084:
085:                if (stack.length <= CALL_CONTEXT_OFFSET) {
086:                    return null;
087:                }
088:                // Traverse the call stack until we find clazz
089:                for (int i = CALL_CONTEXT_OFFSET; i < stack.length; ++i) {
090:                    if (clazz.isAssignableFrom(stack[i])) {
091:                        // Found: the caller is the previous stack element
092:                        return i + 1 >= stack.length ? null : stack[i + 1];
093:                    }
094:                }
095:
096:                //Unable to locate class in call stack
097:                return null;
098:            }
099:
100:            /**
101:             * Find the caller of the passed in Class.
102:             * May return null if caller not found on execution stack.
103:             *
104:             * @param clazz the Class to search for on stack to find caller of
105:             * @param stackDepthOffset Offset call-stack depth to find caller
106:             * @return the Class of object that called parrameter class
107:             */
108:            public static Class getCallerClass(final Class clazz,
109:                    final int stackDepthOffset) {
110:                final Class[] stack = CLASS_CONTEXT.get();
111:
112:                if (stack.length <= stackDepthOffset + CALL_CONTEXT_OFFSET) {
113:                    return null;
114:                }
115:                // Traverse the call stack until we find clazz
116:                for (int i = stackDepthOffset + CALL_CONTEXT_OFFSET; i < stack.length; ++i) {
117:                    if (clazz.isAssignableFrom(stack[i])) {
118:                        // Found: the caller is the previous stack element
119:                        return i + 1 >= stack.length ? null : stack[i + 1];
120:                    }
121:                }
122:
123:                //Unable to locate class in call stack
124:                return null;
125:            }
126:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.