gpt4 book ai didi

java - 在 AspectJ 中获取泛型方法调用的返回类型

转载 作者:行者123 更新时间:2023-11-29 08:13:08 25 4
gpt4 key购买 nike

我有通用方法Foo.foo():

class Foo {
static native T <T> foo();
}

Bar bar = Foo.foo();

我需要的是使用 AspectJ 替换对此方法的调用。问题是要从方面返回类型 T 的值,我需要知道 T 是什么。我如何使用 AspectJ 做到这一点?

这是我尝试过的一种解决方案:

Object around() : call(* Foo.foo(..)) {
Class target = ((MethodSignature) thisJoinPoint.getSignature()).getReturnType();
System.out.println("class = " + class);
}

它返回 Object 作为返回类型的类。我如何确定对 foo() 的调用实际上应该返回 Bar 的实例?

最佳答案

我还没有检查过,但我相信这应该可行。

Method method = ((MethodSignature) thisJoinPoint.getSignature()).getMethod();
Type type = method.getGenericReturnType();
System.out.println("type = " + type);

请在此处查看 javadoc:Method#getGenericReturnType()

关于java - 在 AspectJ 中获取泛型方法调用的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6707535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com