如何在grails项目中包含groovy

时间:2012-02-07 22:15:37

标签: grails groovy metaprogramming

我是一个白痴或者其他什么,我不知道如何将groovy添加到src / groovy中并使其工作。让我说我的引导程序中有一些元数据,我想将这些调用移动到我可以从单元测试或基于这个问题的任何地方调用的类:Correct way to metaprogram in grails so its available in unit tests

所以如果我把它放在我的引导程序中(顶部有import myproject.*),它就可以了。

ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
assert 3.gimmeAP() == 'p'

所以我正在使用STS,我进入src / groovy并说“New GroovyClass”将它添加到myproject包中并填写如下:

package yakit

class MetaThangs {
  def doMetaThangs() {
    ExpandoMetaClass.enableGlobally()

    Integer.metaClass.gimmeAP = {->return 'p'}
  }
}

然后我在bootstrap中调用它:

MetaThangs.doMetaThangs()
assert 3.gimmeAP() == 'p'

我收到错误:

Running Grails application..
2012-02-07 14:12:13,332 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)
at grails.util.Environment.executeForEnvironment(Environment.java:244)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:220)
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
at RunApp$_run_closure1.doCall(RunApp:33)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at BootStrap$_closure1.doCall(BootStrap.groovy:5)
... 26 more
Application context shutting down...
Application context shutdown.

它真的告诉我我应该输入'doMetaThangs()'而不是'doMetaThangs()'吗?

更新:基于@mkoryak回答我尝试将MetaThangs.groovy中的方法声明更改为:

static def doMetaThangs(){
  ...
}

起初它没有起作用,但最终它出现了。

1 个答案:

答案 0 :(得分:1)

doMetaThangs不是静态的,但你正在调用它,就像它一样。

将static修饰符添加到方法中,或者在类的实例上调用它,而不是类。