Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CallStack |
|
| 1.0;1 | ||||
CallStack$GetCallerStrategy |
|
| 1.0;1 | ||||
CallStack$SecurityManagerMethod |
|
| 1.0;1 | ||||
CallStack$SunReflection |
|
| 1.0;1 |
1 | /* | |
2 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
3 | * you may not use this file except in compliance with the License. | |
4 | * You may obtain a copy of the License at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * Unless required by applicable law or agreed to in writing, software | |
9 | * distributed under the License is distributed on an "AS IS" BASIS, | |
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
11 | * See the License for the specific language governing permissions and | |
12 | * limitations under the License. | |
13 | */ | |
14 | package org.apache.commons.classscan.util; | |
15 | ||
16 | 0 | public abstract class CallStack { |
17 | ||
18 | interface GetCallerStrategy { | |
19 | Class<?> getCallerClass(int callStackDepth); | |
20 | } | |
21 | ||
22 | private static GetCallerStrategy strategy; | |
23 | ||
24 | static class SunReflection implements GetCallerStrategy { | |
25 | 0 | SunReflection() { |
26 | // cause load of sun.reflect.Reflection class | |
27 | 0 | sun.reflect.Reflection.getCallerClass(0); |
28 | 0 | } |
29 | ||
30 | @Override | |
31 | public Class<?> getCallerClass(int callStackDepth) { | |
32 | 0 | return sun.reflect.Reflection.getCallerClass(callStackDepth); |
33 | } | |
34 | } | |
35 | ||
36 | 0 | static class SecurityManagerMethod extends SecurityManager implements GetCallerStrategy { |
37 | @Override | |
38 | public Class<?> getCallerClass(int callStackDepth) { | |
39 | 0 | return getClassContext()[callStackDepth]; |
40 | } | |
41 | } | |
42 | ||
43 | static { | |
44 | try { | |
45 | 0 | strategy= new SunReflection(); |
46 | } | |
47 | 0 | catch(Throwable t) { |
48 | 0 | strategy= new SecurityManagerMethod(); |
49 | 0 | } |
50 | 0 | } |
51 | ||
52 | /** | |
53 | * Return the Class of the caller. | |
54 | * @param callStackDepth The number of stack frames to skip. | |
55 | * A value of 0 will return the calling Class of this method. | |
56 | * A value of 1 will return the calling Class of the caller of this method. | |
57 | * @return The Class at callStackDepth down the call stack | |
58 | */ | |
59 | public static Class<?> getCallerClass(int callStackDepth) { | |
60 | 0 | return strategy.getCallerClass(callStackDepth+3); |
61 | } | |
62 | ||
63 | /** | |
64 | * Return the ClassLoader of the caller. | |
65 | * @param callStackDepth The number of stack frames to skip. | |
66 | * A value of 0 will return the ClassLoader of the calling Class of this method. | |
67 | * A value of 1 will return the ClassLoader of the calling Class of the caller of this method. | |
68 | * @return The ClassLoader of the Class at callStackDepth down the call stack | |
69 | */ | |
70 | public static ClassLoader getClassLoader(int callStackDepth) { | |
71 | 0 | Class<?> rc= strategy.getCallerClass(callStackDepth+3); |
72 | 0 | return rc.getClassLoader(); |
73 | } | |
74 | } |