나는 바보 같은 무엇인가로, 그루비를 src/groovy에 추가하고 작동시키는 법을 모른다. 내 부트 스트랩의 일부 메타 물건을 가지고 말을하고 난 클래스에 이러한 호출 내가 단위 테스트에서 호출 또는 어디든지이 질문을 기반으로 할 수 이동할 수 있습니다 : import myproject.*
이있는 (그래서 Correct way to metaprogram in grails so its available in unit tests그레이비를 grails 프로젝트에 포함시키는 방법
내 부트 스트랩에 넣고 경우를 상단에서) 작동합니다. 그래서
ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
assert 3.gimmeAP() == 'p'
나는 STS를 사용하고 있는데 나는 SRC/그루비로 가서 "새 GroovyClass는"MyProject를 패키지하고 그래서 그것을 채우기 위해 추가 말 :
이package yakit
class MetaThangs {
def doMetaThangs() {
ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return '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.
,369 :
MetaThangs.doMetaThangs()
assert 3.gimmeAP() == 'p'
내가 오류
'doMetaThangs()'대신 'doMetaThangs()'를 입력해야한다고 실제로 말하고 있습니까?
UPDATE : @mkoryak 답변에 따라 나는에 MetaThangs.groovy에 메소드 선언을 변경 시도 :
static def doMetaThangs(){
...
}
그것은 처음에는 작동하지 않았다,하지만 결국은 주위왔다.
"어떻게 정적 수정자를 메서드에 추가합니까?" – Mikey
이러한 솔루션 중 하나를 수행하는 방법을 모르겠습니다. – Mikey
@Mikey 예외 메시지가 보여 주듯이 함수 앞에 static이라는 단어를 넣어 정적으로 만든다. 인스턴스에서 메소드를 호출하려면, 인스턴스를 생성하고 그 인스턴스에서 메소드를 호출하십시오 (예 : foo ".toUpperCase()'). 너무 많은 것을하기 전에 한 걸음 뒤로 물러나서 기본적인 Java/Groovy 항목을 찾아 내고 싶을 수도 있습니다. –