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

And*_*ndy 1 java aspectj

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

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

Bar bar = Foo.foo();
Run Code Online (Sandbox Code Playgroud)

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

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

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

Object作为返回类型返回.如何确定该调用foo()应该实际返回实例Bar

Kow*_*ser 5

我没有检查过,但我相信这应该有效.

Method method = ((MethodSignature) thisJoinPoint.getSignature()).getMethod();
Type type = method.getGenericReturnType();
System.out.println("type = " + type);
Run Code Online (Sandbox Code Playgroud)

请在这里查看javadoc:Method#getGenericReturnType()