7
그루비 스크립트에서 클로저를 추출하려고합니다. 나는 내 그루비 파일 Groovy에서 Java로 클로저를 가져 오는 방법은 무엇입니까?
def printMe = {str ->println str}
과 폐쇄를 정의하고 다음과 같이 바인딩에서 잡아하여 사용하려고 :
GroovyScriptEngine gse = new GroovyScriptEngine(new String[] { "scripts" });
Binding binding = new Binding();
gse.run("test.groovy", binding);
Closure cls = (Closure) binding.getVariable("printMe");
cls.call("foo");
하지만이 프로그램을 실행할 때 나는 다음과 같은 오류가 발생합니다.
groovy.lang.MissingPropertyException: No such property:
printMe for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:55)
at GroovyTry.main(GroovyTry.java:19)
그루비 스크립트에서 클로저 (또는 일반 방법)를 가져 오는 방법이 있습니까?
등에 대한 Scoping and the Semantics of "def" 감사 이봐,이에 자리였다 읽기 바인딩
에가는 것보다, 스크립트에 지방이된다 생각합니다. – brice