One*_*rld 3 java language-features
我只是好奇.我有时会请求该功能,但后来我用更多的代码解决了它.(调用类在调用方法时说出了它的名字)
private Class getCallingClass() {
return new SecurityManager() {
protected Class[] getClassContext(){return super.getClassContext();}
}.getClassContext()[2];
}
Run Code Online (Sandbox Code Playgroud)
要么
public class Foo {
public static final void main(final String[] args) {
test();
}
private static void test() {
Throwable e = new Throwable();
StackTraceElement[] elements = e.getStackTrace();
System.out.println(elements.length > 1 ? elements[1].toString() : "(no caller)");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2502 次 |
| 最近记录: |