【发布时间】:2013-08-16 22:39:44
【问题描述】:
我正在尝试检查一些来自 Java 的 CGLib 代理 Groovy 类上的 Groovy 生成的方法,以了解方法的返回和参数类型。例如,考虑这个 Groovy 类:
class Person {
String name
}
Groovy 为 name 属性生成 getName() 和 setName() 方法。 getName() 大概返回一个 String 和 setName() 大概需要一个 String。
但是当通过 CGLib 代理此类并使用 CGLib 的 MethodInterceptor 拦截对 getName 的调用时,method.getName() 返回 getMetaClass 和 method.getReturnType() 返回 groovy.lang.MetaClass。
有没有办法从MethodInterceptor 中了解实际的方法名称和返回类型?
编辑:这是拦截 Person.getName() 调用时的调用堆栈:
ExplicitMappingInterceptor.intercept(Object, Method, Object[], MethodProxy) line: 42
GroovyMMTester$A$$EnhancerByCGLIB$$915b5b4.getMetaClass() line: not available
CallSiteArray.createPogoSite(CallSite, Object, Object[]) line: 144
CallSiteArray.createCallSite(CallSite, Object, Object[]) line: 161
CallSiteArray.defaultCall(CallSite, Object, Object[]) line: 45
AbstractCallSite.call(Object, Object[]) line: 108
AbstractCallSite.call(Object) line: 112
GroovyMMTester$Map.configure() line: 18 <-- Person.getName() call is in here, but doesn't show
【问题讨论】:
-
使用CGLib而不是Metaclass的原因是什么?
-
我猜你调用的是 Person 对象上的 getName(),而不是直接 Person.getName()。对吗?
标签: java reflection groovy proxy-classes cglib