Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy方法缺失_Groovy_Mop - Fatal编程技术网

Groovy方法缺失

Groovy方法缺失,groovy,mop,Groovy,Mop,我在一个对象Foo内有一个闭包,在闭包内我定义了一个名为“myStaticMethod”的方法,一旦在对象Foo外调用闭包,我就要解析这个方法。我还碰巧在我的对象Foo中有一个同名的“故意”静态方法。当我调用闭包时,我将“resolve strategy”设置为DELEGATE_,仅拦截对闭包中定义的myStaticMethod的调用 我试图通过missingMethod实现这一点,但该方法从未被拦截。当我将Foo.myStaticMethod设置为非静态时,该方法被截获。我不太明白为什么会发生

我在一个对象Foo内有一个闭包,在闭包内我定义了一个名为“myStaticMethod”的方法,一旦在对象Foo外调用闭包,我就要解析这个方法。我还碰巧在我的对象Foo中有一个同名的“故意”静态方法。当我调用闭包时,我将“resolve strategy”设置为DELEGATE_,仅拦截对闭包中定义的myStaticMethod的调用

我试图通过missingMethod实现这一点,但该方法从未被拦截。当我将Foo.myStaticMethod设置为非静态时,该方法被截获。我不太明白为什么会发生这种情况,尽管我的解决策略设置为仅授权。让Foo.myStaticMethod保持静态与否应该无关紧要,否则我会错过一些东西

class Foo {
   static myclosure = {
       myStaticMethod()
   }

   static def myStaticMethod() {}
}


class FooTest {
  def c = Foo.myclosure
  c.resolveStrategy = Closure.DELEGATE_ONLY
  c.call()

  def missingMethod(String name, def args) {
    println $name
  }
}

遗憾的是,闭包属性解析不会拦截静态方法。我知道截取这些的唯一方法是在拥有闭包的类上重写静态元类invokeMethod,例如:

class Foo {
   static myclosure = {
       myStaticMethod()
   }

    static myStaticMethod() {
       return false
   }
}

Foo.metaClass.'static'.invokeMethod = { String name, args ->
    println "in static invokeMethod for $name"
    return true
}

def closure = Foo.myclosure
assert true == closure()

遗憾的是,闭包属性解析不会拦截静态方法。我知道截取这些的唯一方法是在拥有闭包的类上重写静态元类invokeMethod,例如:

class Foo {
   static myclosure = {
       myStaticMethod()
   }

    static myStaticMethod() {
       return false
   }
}

Foo.metaClass.'static'.invokeMethod = { String name, args ->
    println "in static invokeMethod for $name"
    return true
}

def closure = Foo.myclosure
assert true == closure()

为了解决这个问题,我在FooTests中调用闭包之前覆盖了invokeMethod

Foo.metaClass.'static'.invokeMethod = { String name, args ->
     println "Static Builder processing $name "
}
在试图解决这个问题时,我发现了一种截获丢失的静态方法的非常奇怪的方法。将来可能对你们中的一些人有用

 static $static_methodMissing(String name, args) {
    println "Missing static $name"
}

-Ken

为了解决这个问题,我在FooTests中调用闭包之前覆盖了invokeMethod

Foo.metaClass.'static'.invokeMethod = { String name, args ->
     println "Static Builder processing $name "
}
在试图解决这个问题时,我发现了一种截获丢失的静态方法的非常奇怪的方法。将来可能对你们中的一些人有用

 static $static_methodMissing(String name, args) {
    println "Missing static $name"
}

-Ken

+1我不知道$static\u methodMissing技巧,这可能对静态$static\u methodMissingString名称args技巧有用+1!这让我很开心!我能够用它实现一个很好的配置DSL+1我不知道$static\u methodMissing技巧,这对于静态$static\u methodMissingString名称args技巧可能有用+1!这让我很开心!我能够用它实现一个很好的配置DSL