01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: 493784 $ last updated $Date: 2007-01-07 17:57:55 +0000 (Sun, 07 Jan 2007) $
28: */
29: public final class ClassContext extends SecurityManager {
30: /**
31: * Private constructor to prevent instantiation.
32: */
33: private ClassContext() {
34: }
35:
36: private static ClassContext _instance = new ClassContext();
37:
38: /*
39: * N.B. Both static routines pick up the instance context directly This
40: * ensures that both return the same stack depth
41: */
42:
43: /**
44: * Gets the calling context as an array of classes Class[0] is this class.
45: *
46: * @return Class[] - list of classes in the callers context
47: */
48: public static Class[] getMyClassContext() {
49: return _instance.getClassContext();
50: }
51:
52: /**
53: * Get the name of the class at a particular stack depth i=0 gives this
54: * class
55: *
56: * @param i -
57: * stack depth
58: * @return String - name of class at depth i
59: */
60: public static String getCallerClassNameAt(int i) {
61: return _instance.getClassContext()[i].getName();
62: }
63: }
|