2012-02-07 85 views
0

我是個傻瓜或者其他什麼東西,我不知道如何將groovy添加到src/groovy並使其工作。比方說,我在我的引導一些元的東西,我想搬到一個類的電話,我可以從單元測試調用或任何基於這樣一個問題:Correct way to metaprogram in grails so its available in unit tests如何在Groils項目中包含groovy

所以,如果我把這個在我的引導(其中有import myproject.*在頂部)它的工作原理。

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

所以我用STS和我進入的src/Groovy和說:「新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()'?

UPDATE:基於@mkoryak答案我試圖改變的方法聲明中MetaThangs.groovy到:

static def doMetaThangs(){ 
    ... 
} 

它沒有在工作第一,但最終還是來到身邊。

回答

1

doMetaThangs不是靜態的,但是您將它稱爲好像它一樣。

要麼將​​靜態修飾符添加到該方法,要麼將其稱爲類的實例,而不是類。

+0

我該如何「將靜態修飾符添加到方法」? – Mikey 2012-02-07 22:33:51

+0

我不知道如何去做這些解決方案。 – Mikey 2012-02-07 22:37:20

+0

@Mikey通過將「靜態」一詞放在它的前面,就像異常消息顯示的那樣,使得函數成爲靜態的。要調用一個實例的方法,創建一個實例並調用它的方法,比如說''foo「.toUpperCase()'。在進一步深入研究之前,您可能需要退一步,找出一些基本的Java/Groovy。 – 2012-02-07 22:41:27

相關問題