KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > util > ClassContext


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/util/ClassContext.java,v 1.5 2004/03/30 18:07:21 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.util;
20
21 /**
22  * Class to get access to the protected getClassContext() method of
23  * SecurityManager, thus obtaining the call stack.
24  *
25  * May not work with applications that install their own security managers.
26  *
27  * @version $Revision: 1.5 $ last updated $Date: 2004/03/30 18:07:21 $
28  */

29 public final class ClassContext extends SecurityManager JavaDoc
30 {
31     /**
32      * Private constructor to prevent instantiation.
33      */

34     private ClassContext()
35     {
36     }
37
38     private static ClassContext _instance = new ClassContext();
39
40     /*
41      * N.B. Both static routines pick up the instance context directly
42      * This ensures that both return the same stack depth
43      */

44
45     /**
46      * Gets the calling context as an array of classes
47      * Class[0] is this class.
48      *
49      * @return Class[] - list of classes in the callers context
50      */

51     public static Class JavaDoc[] getMyClassContext()
52     {
53         return _instance.getClassContext();
54     }
55     /**
56      * Get the name of the class at a particular stack depth
57      * i=0 gives this class
58      *
59      * @param i - stack depth
60      * @return String - name of class at depth i
61      */

62     public static String JavaDoc getCallerClassNameAt(int i)
63     {
64         return _instance.getClassContext()[i].getName();
65     }
66 }
67
Popular Tags