简体   繁体   English

如何从java中的方法获取调用者类对象?

[英]How can I get the caller class object from a method in java?

I know I can get the method and classname from StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); 我知道我可以从StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();获取方法和类名StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); but that is not what I want. 但这不是我想要的。 I want the class object, so I can access his interface, annotations, etc... 我想要类对象,所以我可以访问他的界面,注释等...

It is possible? 有可能的? Class<?> classObject = getCallerClass();

I see this question, but that is just for the classname. 我看到了这个问题,但这只是针对类名。

How to get the caller class in Java 如何在Java中获取调用者类

Edit: Now I'm passing the class this way: 编辑:现在我以这种方式传递课程:

someService.dummyMethod(foo1, foo2, new Object(){}.getClass());

  someService(String foo1, int foo2, Class<?> c) {
    // stuff here to get the methodname, 
    // the interface of the class and an annotation from the interface.
}

I call someService from a lot of different classes, If is not possible I will continue this way, but If there is a way to get the caller class at runtime I prefer that way. 我从很多不同的类中调用someService,如果不可能,我将继续这种方式,但是如果有一种方法可以在运行时获取调用者类,我更喜欢这种方式。

Get the classname using the code of your linked question: How to get the caller class in Java 使用链接问题的代码获取类名: 如何使用Java获取调用者类

Then use the classname to retrieve the class, using code from here: Getting class by its name 然后使用classname来检索类,使用此处的代码: 按名称获取类

Complete code: 完整代码:

String callerName = Thread.currentThread().getStackTrace()[2].getClassName();

try {
    Class<?> caller = Class.forName(callerName);
    // Do something with it ...
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

(community answer, since only mix of existing answers). (社区答案,因为只有现有答案的混合)。

If you're using Java 9+ you can use java.lang.StackWalker . 如果您使用的是Java 9+,则可以使用java.lang.StackWalker

public void foo() {
    Class<?> caller = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE)
            .getCallerClass();
}

However, since StackWalker is thread safe it might be beneficial to create an instance and store it somewhere (rather than create a new instance every time the method is called). 但是,由于StackWalker是线程安全的,因此创建实例并将其存储在某处(而不是每次调用该方法时都创建一个新实例)可能是有益的。

Javadoc of getCallerClass() : getCallerClass() Javadoc:

Gets the Class object of the caller who invoked the method that invoked getCallerClass . 获取调用调用getCallerClass的方法的调用者的Class对象。

This method filters reflection frames, MethodHandle , and hidden frames regardless of the SHOW_REFLECT_FRAMES and SHOW_HIDDEN_FRAMES options this StackWalker has been configured with. 无论此StackWalker配置了SHOW_REFLECT_FRAMESSHOW_HIDDEN_FRAMES选项,此方法都会过滤反射帧, MethodHandle和隐藏帧。

This method should be called when a caller frame is present. 当存在调用者帧时,应该调用此方法。 If it is called from the bottom most frame on the stack, IllegalCallerException will be thrown. 如果从堆栈的最底部帧调用它,则抛出IllegalCallerException

This method throws UnsupportedOperationException if this StackWalker is not configured with the RETAIN_CLASS_REFERENCE option. 如果未使用RETAIN_CLASS_REFERENCE选项配置此StackWalker则此方法将抛出UnsupportedOperationException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM