Debian Codegraph @

src/com/bulenkov/iconloader/util/ReflectionUtil.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.bulenkov.iconloader.util;
18
19 import org.jetbrains.annotations.Nullable;
20
21 public class ReflectionUtil {
22 @Nullable
23 public static Class getGrandCallerClass() {
24 int stackFrameCount = 3;
25 Class callerClass = findCallerClass(stackFrameCount);
26 while (callerClass != null && callerClass.getClassLoader() == null) { // looks like a system class
27 callerClass = findCallerClass(++stackFrameCount);
28 }
29 if (callerClass == null) {
30 callerClass = findCallerClass(2);
31 }
32 return callerClass;
33 }
34
35 /**
36 * Returns the class this method was called 'framesToSkip' frames up the caller hierarchy.
37 *
38 * NOTE:
39 * <b>Extremely expensive!
40 * Please consider not using it.
41 * These aren't the droids you're looking for!</b>
42 */
43 @Nullable
44 public static Class findCallerClass(int framesToSkip) {
45 try {
46 Class[] stack = MySecurityManager.INSTANCE.getStack();
47 int indexFromTop = 1 + framesToSkip;
48 return stack.length > indexFromTop ? stack[indexFromTop] : null;
49 }
50 catch (Exception e) {
51 // LOG.warn(e);
52 return null;
53 }
54 }
55
56 private static class MySecurityManager extends SecurityManager {
57 private static final MySecurityManager INSTANCE = new MySecurityManager();
58 public Class[] getStack() {
59 return getClassContext();
60 }
61
62 }
63
64 private ReflectionUtil() { }
65 }