【问题标题】:Unable to get Groovy extension module to work无法让 Groovy 扩展模块工作
【发布时间】:2018-03-09 05:07:31
【问题描述】:

我正在尝试创建一个扩展模块,然后在不同的项目/脚本中使用它,但无法让它工作。这是我正在做的事情:

第 1 步:创建一个名为 TemperatureUtils.groovy 的文件,它是一个类似类别的类。以下是出处:

package utils

class TemperatureUtils {

    Double toFahrenheit(Number celcius) {
        (9 * celcius / 5) + 32
    }

    Double toCelcius(Number fahrenheit) {
        (fahrenheit - 32) * 5 / 9
    }
}

第二步:创建扩展模块描述符——org.codehaus.groovy.runtime.ExtensionModule,内容如下:

moduleName=Some-Utils
moduleVersion=1.0
extensionClasses=utils.TemperatureUtils
staticExtensionClasses=

Step-3:编译类并手动创建一个jar文件,结构如下:

extensionUtils.jar
  |-- utils
  |     |-- TemperatureUtils.class
  |
  |-- META-INF
        |-- services
              |-- org.codehaus.groovy.runtime.ExtensionModule

第 4 步:创建一个新脚本来使用扩展模块。脚本来源:

import org.codehaus.groovy.control.CompilerConfiguration

def groovyScript = '''
//Following line just confirms that the jar file is indeed on the classpath of this script
assert 25 == (new utils.TemperatureUtils()).toCelcius(77)

//Actually using the category now
assert 77.toCelcius()       == 25
assert 25.toFahrenheit()    == 77
'''

def compilerConfig = new CompilerConfiguration()

compilerConfig.setClasspath(/E:\temp\jar\extensionUtils.jar/)

def shell = new GroovyShell(compilerConfig)
shell.evaluate(groovyScript)

第 5 步:执行脚本。在这里,我得到以下异常:

groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.toCelcius() is applicable for argument types: () values: []
    at Script1.run(Script1.groovy:6)
    at ConsoleScript2.run(ConsoleScript2:16)

现在,我尝试了一些方法,但无法正常工作:

  • 从扩展模块描述符中删除了最后一行 - “staticExtensionClasses=",但它不起作用。
  • 通过使用 @Category(Number) 注释并从两个方法中删除参数(并在方法中使用“this”而不是“celcius”和“fahrenheit”参数名称,将 TemperatureUtils.groovy 类更改为实际类别' 机构),但它仍然没有工作。
  • 用谷歌搜索,但没有找到太多信息。还偶然发现了this,但这对我也没有帮助。

非常感谢精彩的 stackoverflow 社区可以提供的任何帮助! :)

【问题讨论】:

    标签: groovy extension-methods groovyshell groovy-console extension-modules


    【解决方案1】:

    以下对我有用,使用 Groovy 2.4.5。基于this post

    首先,将TemperatureUtils 更改为static 方法:

    包工具

    class TemperatureUtils {
        static Double toFahrenheit(Number celcius) {
            (9 * celcius / 5) + 32
        }
    
        static Double toCelcius(Number fahrenheit) {
            (fahrenheit - 32) * 5 / 9
        }
    }
    

    然后,我不会使用CompilerConfiguration,而是简单地设置CLASSPATH。例如

    $ export CLASSPATH=../utils/build/libs/temp.jar
    $ groovy client.groovy
    

    Client.groovy 只是:

    def groovyScript = '''
    //Following line just confirms that the jar file is indeed on the classpath of this script
    assert 25 == (new utils.TemperatureUtils()).toCelcius(77)
    
    //Actually using the category now
    assert 77.toCelcius()       == 25
    assert 25.toFahrenheit()    == 77
    '''
    
    def shell = new GroovyShell() 
    shell.evaluate(groovyScript)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-28
      • 2016-06-22
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 2018-08-03
      • 2015-05-31
      • 2017-07-18
      相关资源
      最近更新 更多