如何在grails项目中包含groovy

如何在grails项目中包含groovy,grails,groovy,metaprogramming,Grails,Groovy,Metaprogramming,我是个白痴之类的人,我不知道如何将groovy添加到src/groovy中并使其工作。假设我的引导程序中有一些元内容,我想将这些调用转移到一个可以从单元测试调用的类,或者基于这个问题的任何地方: 因此,如果我把它放在我的引导程序中(顶部有importmyproject.*),它就可以工作了 ExpandoMetaClass.enableGlobally() Integer.metaClass.gimmeAP = {->return 'p'} assert 3.gimmeAP() == 'p

我是个白痴之类的人,我不知道如何将groovy添加到src/groovy中并使其工作。假设我的引导程序中有一些元内容,我想将这些调用转移到一个可以从单元测试调用的类,或者基于这个问题的任何地方:

因此,如果我把它放在我的引导程序中(顶部有
importmyproject.*
),它就可以工作了

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'}
  }
}
然后我在引导中称之为:

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 answer,我尝试将MetaThangs.groovy中的方法声明更改为:

static def doMetaThangs(){
  ...
}

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

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


要么将静态修饰符添加到方法中,要么对类的实例而不是类调用它。

如何“将静态修饰符添加到方法中”?我不知道如何执行这两种解决方案。@Mikey您可以通过将单词
static
放在函数前面使函数静态,就像异常消息所示。要在实例上调用方法,请创建一个实例,然后在其上调用该方法,比如说
“foo”.toUpperCase()
。在进一步讨论之前,您可能需要退一步,了解一些基本的Java/Groovy内容。