【问题标题】:how to fix groovy.lang.MissingMethodException: No signature of method:如何修复 groovy.lang.MissingMethodException:没有方法签名:
【发布时间】:2013-07-01 18:27:59
【问题描述】:

我正在尝试在没有闭包的情况下使用此方法

def copyAndReplaceText(source, dest, targetText, replaceText){
    dest.write(source.text.replaceAll(targetText, replaceText))
}

def source = new File('C:/geretd/resumebak.txt') //Hello World
def dest = new File('C:/geretd/resume.txt') //blank

copyAndReplaceText(source, dest){
    it.replaceAll('Visa', 'Passport!!!!')
}

但是当我运行它时,我收到以下错误:

groovy.lang.MissingMethodException: No signature of method: ConsoleScript3.copyAndReplaceText() is applicable for argument types: (java.io.File, java.io.File, ConsoleScript3$_run_closure1) values: [C:\geretd\resumebak.txt, C:\geretd\resume.txt, ...]
Possible solutions: copyAndReplaceText(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object)

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)

at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)

我做错了什么?

【问题讨论】:

  • 来自 groovy 中的菜鸟:我有同样的例外,但原因不同。我定义了一个比我想在代码开头使用的方法更晚的方法。当我更换它时,它起作用了。
  • 另一个警告:如果你在没有完全限定外部方法的情况下从内部类调用外部类的静态方法:static class Abc { private void test() { foo(); } } private static void foo() { }

标签: groovy


【解决方案1】:

因为您将三个参数传递给一个四参数方法。另外,您没有使用传递的闭包。

如果您想指定在source 内容之上进行的操作,请使用闭包。应该是这样的:

def copyAndReplaceText(source, dest, closure){
    dest.write(closure( source.text ))
}

// And you can keep your usage as:
copyAndReplaceText(source, dest){
    it.replaceAll('Visa', 'Passport!!!!')
}

如果您总是交换字符串,请同时传递两个字符串,因为您的方法签名已经说明:

def copyAndReplaceText(source, dest, targetText, replaceText){
    dest.write(source.text.replaceAll(targetText, replaceText))
}

copyAndReplaceText(source, dest, 'Visa', 'Passport!!!!')

【讨论】:

  • 非常感谢,这是我第一次使用 groovy,我需要帮助,快速提问如果我使用下面的代码,我将传递 4 个参数对吗? def copyAndReplaceText(source, dest, targetText, replaceText){ dest.write(source.text.replaceAll(targetText, replaceText)) } def source = new File('C:/geretd/resumebak.txt') //Hello World def dest = new File('C:/geretd/resume.txt') //blank copyAndReplaceText(source, dest, 'Visa', 'Passport!!!!')
  • 是的。 copyAndReplaceText() 是方法名称,您传递了四个参数:第一个 source、第二个 dest、第三个 'Visa' 和第四个 'Passport!!!!'
  • 老实说,直到周五我才听说过 groovy,我不得不自己研究,现在我被困在这个要求上。搜索并替换到名为 xxxx~ 的临时文件,旧文本与新文本不知道该怎么做
  • 但是我发现 groovy 非常好,它不像 java 那样死板,尽管它是基于它的。
  • @geretd Tim's approach 对于您之前的问题,除了您现在使用的问题之外,一旦您熟悉了 Groovy 的语义和语法,将会很有用。
【解决方案2】:

帮助其他寻虫者。我有这个错误,因为该功能不存在。

我有一个拼写错误。

【讨论】:

  • 感谢您为我指明正确的方向。我的方法名称不一样。糟糕!
  • 当你认为你正在处理真正的错误时......
【解决方案3】:

就我而言,只是我有一个与函数同名的变量。

例子:

def cleanCache = functionReturningABoolean()

if( cleanCache ){
    echo "Clean cache option is true, do not uninstall previous features / urls"
    uninstallCmd = ""
    
    // and we call the cleanCache method
    cleanCache(userId, serverName)
}
...

后来在我的代码中,我有了这个功能:

def cleanCache(user, server){

 //some operations to the server

}

显然 Groovy 语言不支持这一点(但其他语言如 Java 支持)。 我刚刚将我的函数重命名为 executeCleanCache 并且它运行良好(或者您也可以将变量重命名为您喜欢的任何选项)。

【讨论】:

    【解决方案4】:

    如果您传递给该方法的对象乱序,您也可能会收到此错误。换句话说,假设您的方法按顺序采用字符串、整数和日期。如果你传递一个日期,然后是一个字符串,然后是一个整数,你会得到同样的错误信息。

    【讨论】:

    • 只要您的参数列表对于方法签名不正确,您就会收到此错误...
    【解决方案5】:

    这也可能是因为你给类名的所有字母都是小写的,这是 groovy(知道版本 2.5.0)不支持的。

    类名 - 接受用户但不接受用户。

    【讨论】:

      【解决方案6】:

      在我的情况下,问题是我在 build.gradle 文件中注释了 firebase 依赖项,但仍在调试构建类型中使用它:

      firebaseCrashlytics {
         mappingFileUploadEnabled true
      }
      

      只需对其进行评论以使其正常工作。

      【讨论】:

        【解决方案7】:

        这是 ionic/android 应用的主要问题之一

        请按照以下步骤操作。

        1. 从根文件夹中移除/删除节点模块
        2. npm 卸载 cordova-android
        3. npm install cordova-android@10.1.1
        4. 现在使用以下方法删除 android 平台:ionic cordova rm platform android
        5. 现在添加平台:ionic cordova platform add android
        6. 构建 Android 项目:ionic cordova build android

        问题将得到解决

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-11
          • 1970-01-01
          • 1970-01-01
          • 2017-04-08
          相关资源
          最近更新 更多