Grails Groovy元编程

Grails Groovy元编程,grails,groovy,overriding,metaprogramming,metaclass,Grails,Groovy,Overriding,Metaprogramming,Metaclass,我想覆盖Grails中的方法定义。我试图使用Groovy元编程,因为我想要覆盖的类属于一个框架 下面是原始类 class SpringSocialSimpleSignInAdapter implements SignInAdapter { private RequestCache requestCache SpringSocialSimpleSignInAdapter(RequestCache requestCache) { this.requestCache = req

我想覆盖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
   }
}
但由于某些原因,覆盖不是变形。我想不出来。任何帮助都将不胜感激


谢谢

是的,看起来像是个虫子。我不知道你的整个情况,但无论如何,我做了一个小的变通方法:

  • 在类定义中,不实现接口
  • 你可以创建你的对象,并进行元魔术
  • 使用groovy强制使其充当接口,然后您可以传递它
  • 下面是我用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)
    

    看来我遇到了这个问题
    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)