我想覆盖Grails中的方法定义。我正在尝试使用Groovy元编程,因为我想覆盖的类属于一个框架。
下面是原始的类。
class SpringSocialSimpleSignInAdapter implements SignInAdapter {
private RequestCache requestCache
SpringSocialSimpleSignInAdapter(RequestCache requestCache) {
this.requestCache = requestCache;
}
String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) {
SignInUtils.signin localUserId
extractOriginalUrl request
}
}我正在尝试覆盖如下内容
SpringSocialSimpleSignInAdapter.metaClass.signIn = {java.lang.String str, org.springframework.social.connect.Connection conn, org.springframework.web.context.request.NativeWebRequest webreq ->
println 'coming here....' // my implementation here
return 'something'
}但出于某种原因,重写并不是一种有效的方法。我想不出来。任何帮助都会得到极大的重视。
谢谢
发布于 2012-08-13 08:14:36
是啊,看起来像是那只虫子。我不知道你的整个场景,但不管怎样,这是我做的一个小的变通方法:
下面是我用JIRA bug编写的一个小脚本来证明这一点:
interface I {
def doIt()
}
class T /*implements I*/ {
def doIt() { true }
}
def t = new T()
assert t.doIt()
t.metaClass.doIt = { -> false }
// here the coercion happens and the assertion works fine
def i = t as I
assert !i.doIt()
assert !t.doIt()
// here the polymorphism happens fine
def iOnlyAcceptInterface(I i) { assert !i.doIt() }
iOnlyAcceptInterface(i)https://stackoverflow.com/questions/11892620
复制相似问题