java方法返回类型

发布于 2024-12-25 04:34:31 字数 939 浏览 4 评论 0 原文

我有这样的方法:

public File method1(){
    method2()
}

public method2(){
do something..
and get method1 return type(in this case File)
}

我如何得到它?我尝试这样..

Throwable t = new Throwable();
StackTraceElement[] elements = t.getStackTrace();

并获取元素的所有方法。之后,getReturnType,但它不起作用。我也尝试过

public File method1(){
    method2(this.getClass());
}

public method2(Class<?> className){
   //problem here
}

,但这里的问题是我无法比较两个元素,即堆栈上的元素和来自 classname.getMethods() 的元素。

有什么方法可以将方法返回类型发送到 method2 吗?我需要这个是因为要制作一些类似历史的日志。我知道可以用aspectJ 来完成,但我必须像这样做。

编辑:

我遇到的主要问题是我可以获得堆栈输出,并查看调用我的 method2 的方法 - 这是我需要的一个片段!我还需要该方法的返回类型,但堆栈不保存该信息。现在,我可以从“调用method2的方法”所在的类中获取所有方法。这些方法的列表包含所有内容、返回类型、输入参数……但这是一个相当大的列表,有 63 个方法。所以我必须以某种方式比较它们,找出哪一个是来自堆栈的。我无法使用名称来比较它们,因为有些返回类型不同,哈希码不同 - 这就是我陷入困境的地方。

I have methods like this:

public File method1(){
    method2()
}

public method2(){
do something..
and get method1 return type(in this case File)
}

How do I get it? i tried like this..

Throwable t = new Throwable();
StackTraceElement[] elements = t.getStackTrace();

and get all the methods for the elements. And after that, getReturnType, but it doesn't work. I also tried

public File method1(){
    method2(this.getClass());
}

public method2(Class<?> className){
   //problem here
}

But here the problem is that i can't compare two elements, the one on the stack and the one from classname.getMethods().

Is there any way that I can send method return type to a method2? I need this because of making some history-like log. I know it can be done with aspectJ but I have to do it somehow like this.

EDIT:

The main problem I have is that I can get stack output, and see the method who called my method2 - that's one fragment I need! Also I need that method's return type, but the stack doesnt hold that information. Now, I can get all the methods from the class where the "method who called method2" is. The list of those methods, hold everything, return type, input parameters.. but that's a pretty big list, 63 methods. So I have to compare them somehow to find out which one is the one FROM STACK. I can't comapre them using name, because some differ with return type, hashcode is different - that's where I'm stuck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半透明的墙 2025-01-01 04:34:31

更新

如果您确实需要从堆栈跟踪中执行此操作(我强烈建议避免这样做),我认为您不能。堆栈跟踪可以告诉您类和方法名称,但它不包括方法的参数类型,因此如果方法重载,您将无法判断哪个方法称为 method2

我建议您重新审视您的设计。如果 method2 需要知道调用它的方法的返回类型,那么相关方法应该将该信息传递给 method2。尝试从运行时堆栈收集该信息不仅效率低下,而且是一个设计危险信号。

示例:

public File method1(File f) {
    // ...

    method2(File.class);
}

public String method1(String s) {
    // ...

    method2(String.class);
}

public Foo method1(Foo f) {
    // ...

    method2(Foo.class);
}

我们有 method1 的三个重载。这对于 method2 来说不是问题,因为它们都告诉 method2 它的返回类型是什么——或者更准确地说,我希望,什么它需要 method2 来为其创建或其他什么。

原始答案

对于您列出的具体情况(尤其是问题末尾),您可以执行以下操作:

public File method1(){
    method2(File.class);
}

File.classClass File 类的实例。

对于查找类中方法的返回值类型的一般情况,您可以使用 反射 API。通过 Class.forName,使用 Class 实例上的方法href="http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getMethod%28java.lang.String,%20java.lang.Class...%29" rel ="nofollow">Class#getMethod,然后使用Method#getReturnType< /code> 在该 Method 上查找返回类型的类。

Update

If you really need to do this from a stack trace (which I would strongly recommend avoiding), I don't think you can. The stack trace can tell you the class and method names, but it doesn't include the method's argument types, and so if the method is overloaded you can't tell which one called method2.

I recommend you revisit your design. If method2 needs to know the return type of the method that calls it, then the method in question should pass that information into method2. Attempting to gather that information from a runtime stack is not only inefficient, but it's a design red flag.

Example:

public File method1(File f) {
    // ...

    method2(File.class);
}

public String method1(String s) {
    // ...

    method2(String.class);
}

public Foo method1(Foo f) {
    // ...

    method2(Foo.class);
}

There we have three overloads of method1. This is not a problem for method2, because each of them tells method2 what its return type is — or more accurately, I hope, what it needs method2 to create for it or whatever.

Original answer

For the specific case you list (esp. toward the end of your question), you can do this:

public File method1(){
    method2(File.class);
}

File.class is the Class instance for the File class.

For the general case of finding the type of the return value of a method in a class, you can use the reflection API. Get the Class instance for the class containing the method via Class.forName, look up the method on that Class instance using Class#getMethod, then use Method#getReturnType on that Method to find out the return type's class.

酒绊 2025-01-01 04:34:31

为什么反思这么难?

public File method1() {
    method2()
}

public void method2() {
    Class<?> returnType = this.getClass().getMethod("method1").getReturnType();
}

当然,您必须处理异常情况。

Why is it so difficult with reflection?

public File method1() {
    method2()
}

public void method2() {
    Class<?> returnType = this.getClass().getMethod("method1").getReturnType();
}

Of course, you'll have to handle the exceptions though.

花伊自在美 2025-01-01 04:34:31

如果您返回的类有一个空构造函数,您可以在 method2() 中本地构建返回类型的实例并使用它,但这很危险并且不通用。

您可以执行 x.getClass().getName() 并执行切换操作。

不过,我建议您考虑重新构建它以使用 AspectJ 并在其中放置一个切入点在你的堆栈太低之前,你的父母会打电话来收集你真正想要的信息。我对您的意图进行了一些猜测,但是每当您解析堆栈输出以获取类型信息时,我认为该设计都值得再看一下。

If the class you are returning has an empty constructor, you could build a instance of the return type locally in method2() and use that, but that is dangerous and not generic.

You could do x.getClass().getName() and perform a switch operation.

What I would suggest, though, is that you consider rearchitecting this to use AspectJ and put a cut point in your parent call to collect the information you really want before you get too low in the stack. I'm guessing a little bit as to your intent, but any time you are parsing the stack output for type information I think the design deserves a second look.

意中人 2025-01-01 04:34:31

更新答案

结合使用 StackTrace 和 Reflection

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Vector;

public class CallingMethodReturnType
{
    public static void main( String[] args )
    {
        CallingMethodReturnType app = new CallingMethodReturnType();
        app.method1();
        app.method3();
    }

    public File method1(){
        method2();
        return null;
    }

    public ArrayList method3(){
        method4();
        return null;
    }

    public Vector method4() {
        method2();
        return null;
    }

    public void method2(){
        Method method;
        try {
            StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
            StackTraceElement ste = stackTraceElements[2];
            String callerMethodName = ste.getMethodName();
            method = this.getClass().getMethod(callerMethodName, null);
            Class returnType = method.getReturnType();
            System.out.println(returnType);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }
}

另请检查问题 如果您关心性能,如何找到使用堆栈跟踪或反射的方法的调用者?

原始答案

您可以像这样使用反射API

    public method2(){
     //do something and get method1 return type(in this case File)
     Method method = this.getClass().getMethod("method1", null);
     Class returnType = method.getReturnType();
    }

Updated answer

Using combination of StackTrace and Reflection

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Vector;

public class CallingMethodReturnType
{
    public static void main( String[] args )
    {
        CallingMethodReturnType app = new CallingMethodReturnType();
        app.method1();
        app.method3();
    }

    public File method1(){
        method2();
        return null;
    }

    public ArrayList method3(){
        method4();
        return null;
    }

    public Vector method4() {
        method2();
        return null;
    }

    public void method2(){
        Method method;
        try {
            StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
            StackTraceElement ste = stackTraceElements[2];
            String callerMethodName = ste.getMethodName();
            method = this.getClass().getMethod(callerMethodName, null);
            Class returnType = method.getReturnType();
            System.out.println(returnType);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }
}

Also check question How do I find the caller of a method using stacktrace or reflection? if you are concerned with performance.

Original answer

You can use reflection API like this

    public method2(){
     //do something and get method1 return type(in this case File)
     Method method = this.getClass().getMethod("method1", null);
     Class returnType = method.getReturnType();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文