0

我想覆盖 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'
    }

但由于某种原因,压倒一切并没有变好。我无法弄清楚。任何帮助将不胜感激。

谢谢

4

1 回答 1

0

是的,好像是那个bug。我不知道您的整个情况,但无论如何,这是我提出的一个小解决方法:

  1. 在您的类定义中,您没有实现接口
  2. 你创建你的对象并做你的超魔
  3. 使用 groovy coercion 使其充当接口,然后您可以传递它

这是我使用 JIRA 错误制作的一个小脚本来证明它:

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)
于 2012-08-13T00:14:36.333 回答