62,614
社区成员
发帖
与我相关
我的任务
分享
public final class Unsafe {
//其他方法和属性省略
private Unsafe() {}
private static final Unsafe theUnsafe = new Unsafe();
public static Unsafe getUnsafe() {
Class cc = sun.reflect.Reflection.getCallerClass(2);
if (cc.getClassLoader() != null)
throw new SecurityException("Unsafe");
return theUnsafe;
}
}
你可以在调用你的类的方法或构造器里得到是谁调用的,如果调用者不在你的class允许的列表里,可以抛出异常