定义 Thread 扩展方法 , 下面的扩展方法
class ThreadExt {
public static Thread hello(Thread self, Closure closure) {
closure()
return self
}
}
在 src\main\groovy\manifest\META-INF\services\org.codehaus.groovy.runtime.ExtensionModule 配置文件中 , 只配置了
moduleName=groovyExt
moduleVersion=1.0
extensionClasses=ThreadExt
对象实例扩展方法 , 在 Groovy 脚本中调用 Thread 静态扩展方法 ,
Thread.hello{
printf "Hello"
}
报如下错误 :
HelloCaught: groovy.lang.MissingMethodException: No signature of method: static java.lang.Thread.hello() is applicable for argument types: (ThreadExtApplication$_run_closure2) values: [ThreadExtApplication$_run_closure2@1ab06251]
Possible solutions: hello(groovy.lang.Closure), getId(), yield(), sleep(long), sleep(long), split(groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: static java.lang.Thread.hello() is applicable for argument types: (ThreadExtApplication$_run_closure2) values: [ThreadExtApplication$_run_closure2@1ab06251]
Possible solutions: hello(groovy.lang.Closure), getId(), yield(), sleep(long), sleep(long), split(groovy.lang.Closure)
at ThreadExtApplication.run(ThreadExtApplication.groovy:5)
在 src\main\groovy\manifest\META-INF\services\org.codehaus.groovy.runtime.ExtensionModule 配置文件中 , 同时配置静态和实例扩展方法 ;
moduleName=groovyExt
moduleVersion=1.0
extensionClasses=ThreadExt
staticExtensionClasses=ThreadExt
执行
groovyc -d classes ThreadExt.groovy
编译扩展类 , 执行
jar -cf thread.jar -C classes . -C manifest/ .
将编译后的扩展类字节码文件进行打包 , 执行
groovy -classpath thread.jar ThreadExtApplication.groovy
命令 , 执行 ThreadExtApplication.groovy 脚本文件 ;