【问题标题】:invokeMethod from Groovy with no parameters来自 Groovy 的无参数调用方法
【发布时间】:2010-08-16 22:59:33
【问题描述】:

我有一些似乎可以正常工作的 Java 代码:

/**
 * Helper method
 * 1. Specify args as Object[] for convenience
 * 2. No error if method not implemented 
 * (GOAL: Groovy scripts as simple as possible)
 * 
 * @param name
 * @param args
 * @return
 */
Object invokeGroovyScriptMethod(String name, Object[] args) {
    Object result = null;
    try {
        result = groovyObject.invokeMethod(name, args);         
    } catch (exception) { // THIS HAS BEEN GROVIED...
        if (exception instanceof MissingMethodException) {
            if (log.isDebugEnabled()) {
                log.debug("invokeGroovyScriptMethod: ", exception);
            }
        } else {
            rethrow exception;
        }
    }
    return result;
}

Object invokeGroovyScriptMethod(String name) {
    return invokeGroovyScriptMethod(name, [ null ] as Object[]);
}

Object invokeGroovyScriptMethod(String name, Object arg0) {
    return invokeGroovyScriptMethod(name, [ arg0 ] as Object[]);
}

Object invokeGroovyScriptMethod(String name, Object arg0, Object arg1) {
    return invokeGroovyScriptMethod(name, [ arg0, arg1 ] as Object[]);
}

但我的方法有问题:

Object invokeGroovyScriptMethod(String name) {
    return invokeGroovyScriptMethod(name, [ null ] as Object[]);
}


groovy.lang.MissingMethodException: No signature of method: MyClass.getDescription() is   applicable for argument types: (null) values: [null]
Possible solutions: getDescription(), setDescription(java.lang.Object)

有什么提示吗?

谢谢 米莎

【问题讨论】:

    标签: groovy


    【解决方案1】:

    我有一个快速的去(摆脱日志位并用 println 替换它,因为我没有在我的测试中设置日志),我想出了这个不需要重载版本调用GroovyScriptMethod:

    Object invokeGroovyScriptMethod( String name, Object... args = null ) {
      try {
        args ? groovyObject."$name"( args.flatten() ) : groovyObject."$name"()
      } catch( exception ) {
        if( exception instanceof MissingMethodException ) {
          println "invokeGroovyScriptMethod: $exception.message"
        } else {
          throw exception;
        }
      }
    }
    
    groovyObject = 'hi'
    assert    'HI' == invokeGroovyScriptMethod( 'toUpperCase' )
    assert     'i' == invokeGroovyScriptMethod( 'getAt', 1 )
    assert '***hi' == invokeGroovyScriptMethod( 'padLeft', 5, '*' )
    
    // Assert will pass (as we catch the exception, print the error and return null)
    assert    null == invokeGroovyScriptMethod( 'shouldFail' )
    

    编辑

    又看了一遍问题,你说这是一个Java类?但问题似乎表明这是 Groovy 代码......

    如果这是 Java,我担心我可能会让你走错路......

    【讨论】:

    • 哇,这太棒了。我发现我的代码有什么问题 btw Object invokeGroovyScriptMethod(String name) { return invokeGroovyScriptMethod(name, [ null ] as Object[]); } 应该是 Object invokeGroovyScriptMethod(String name) { return invokeGroovyScriptMethod(name, [ ] as Object[]); } 米莎
    • 换句话说 [ null ] as Object[] 应该是 [ ] as Object[]
    • 如果可以的话我把你的代码sn-p贴在这里groovy.329449.n5.nabble.com/…
    • 别担心 :-) 祝你一切顺利 :-)
    • @tim_yates:请看类似question here
    猜你喜欢
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2017-08-15
    • 2010-09-27
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多