添加扩展模块到groovy类 [英] add extension module to groovy class

查看:245
本文介绍了添加扩展模块到groovy类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建简单的扩展模块



我创建了 Main。 groovy file

  class Item {
String item
}

new Item()。sayHello()//这个方法应该是扩展名



我创建了 ItemExtension.groovy class

  class ItemExtension {
def sayHello(Item self){
printlnhello world
}
}

这是我的 org.codehaus.groovy.runtime.ExtensionModule 描述符

  moduleName =项目扩展模块
moduleVersion = 1.0
extensionClasses = ItemExtension

使用 ItemExtension.groovy > groovyc (为了得到 Item class),我预编译了 Main.groovy p>

  groovyc ItemExtension.groovy 

用<$ c编译 ItemExtension.class $ c> META-INF 我放入 .jar 文件(重命名为 .rar ) 。
它的结构看起来像这样

  META-INF\services\org.codehaus.groovy.runtime.ExtensionModule 
ItemExtension.class

我把 ItemExtension.jar > Main.groovy 并重新编译,但是使用 cp 参数添加<$ c $> c $ c> jar 。

  groovyc -cp ItemExtension.jar Main.groovy 

但是当我尝试运行它时

  groovy主要

我得到这个异常

 抓住:groovy.lang.MissingMethodException:没有方法的签名:Item.sayHell 
o()适用于参数类型:()values:[]
groovy.lang.MissingMethodException:方法没有签名:Item.sayHello()是参数类型的
应用:()values:[]
Main.run(Main.groovy:6 )

我做错了什么?

决方案

您的扩展方法必须是静态的才能被识别(因为扩展模块必须是无状态的)并且 Item 类必须存在自己的源文件被识别(因为否则它是脚本的内部类)。



这是一个bash脚本,它可以满足您的需求:

 #!/ bin / bash 

echoCreate Item.groovy
cat> 'Item.groovy'<< -EOF
class Item {
String item
}
EOF

echoCompile Item.groovy
groovyc Item.groovy -d类

echo创建扩展模块类
cat> 'ItemExtension.groovy'<< -EOF
class ItemExtension {
static def sayHello(Item self){
printlnhello world
}
}
EOF

echo创建扩展模块描述符
mkdir -p classes / META-INF / services
cat> classes / META-INF / services / org.codehaus.groovy.runtime.ExtensionModule<< -EOF
moduleName =项目扩展模块
moduleVersion = 1.0
extensionClasses = ItemExtension
EOF

echo编译扩展模块
groovyc -cp类ItemExtension.groovy -d类

echoCreating script.groovy
cat> ; 'script.groovy'<< -EOF
new Item()。sayHello()
EOF

echo运行脚本
groovy -cp类脚本.groovy


I am trying to create simple extension module.

I created Main.groovy file

class Item {
   String item
}

new Item().sayHello() // this method supposed to be extension

I compiled it (not ran).

I created ItemExtension.groovy class

class ItemExtension {
    def sayHello(Item self) {
        println "hello world"
    }
}

This is my org.codehaus.groovy.runtime.ExtensionModule descriptor

moduleName=Item extension module
moduleVersion=1.0
extensionClasses=ItemExtension

I compiled ItemExtension.groovy using groovyc (I precompiled Main.groovy in order to get Item class)

groovyc ItemExtension.groovy

Compiled ItemExtension.class with META-INF I put into .jar file (renamed .rar). Its structure looks like this

META-INF\services\org.codehaus.groovy.runtime.ExtensionModule
ItemExtension.class

I put ItemExtension.jar in the same folder as Main.groovy and compiled it again but with cp argument to add jar.

groovyc -cp ItemExtension.jar Main.groovy

But when I try to run it

groovy Main

I get this exception

Caught: groovy.lang.MissingMethodException: No signature of method: Item.sayHell
o() is applicable for argument types: () values: []
groovy.lang.MissingMethodException: No signature of method: Item.sayHello() is a
pplicable for argument types: () values: []
    at Main.run(Main.groovy:6)

What I did wrong?

解决方案

Your extension method has to be static to be recognized (because extension modules have to be stateless) and the Item class has to live into its own source file to be recognized (because otherwise it's an inner class of the script).

Here is a bash script that does what you want:

#!/bin/bash

echo "Create Item.groovy"
cat > 'Item.groovy' <<-EOF
class Item {
    String item
}
EOF

echo "Compile Item.groovy"
groovyc Item.groovy -d classes

echo "Create extension module class"
cat > 'ItemExtension.groovy' <<-EOF
class ItemExtension {
    static def sayHello(Item self) {
        println "hello world"
    }
}
EOF

echo "Create extension module descriptor"
mkdir -p classes/META-INF/services
cat > classes/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule <<-EOF
moduleName=Item extension module
moduleVersion=1.0
extensionClasses=ItemExtension
EOF

echo "Compile extension module"
groovyc -cp classes ItemExtension.groovy -d classes

echo "Creating script.groovy"
cat > 'script.groovy' <<-EOF
new Item().sayHello()
EOF

echo "Run script"
groovy -cp classes script.groovy

这篇关于添加扩展模块到groovy类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆