| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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) { |
| 27 |
callerClass = findCallerClass(++stackFrameCount); |
| 28 |
} |
| 29 |
if (callerClass == null) { |
| 30 |
callerClass = findCallerClass(2); |
| 31 |
} |
| 32 |
return callerClass; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 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 |
} |