代码之家  ›  专栏  ›  技术社区  ›  nyarian

Gradle自定义任务实现:找不到参数的方法

  •  0
  • nyarian  · 技术社区  · 5 年前

    我有一个加密任务,它接收一个输入文件和一个输出文件以及要对其执行加密的密钥。奇怪的是,当我尝试提取一个执行行加密并将其作为参数接收的方法时,我会得到下一个错误: Could not find method encryptLine() for arguments [PK] on task ':presentation:encryptScenarios' of type EncryptionTask. . 当我内联这个方法时-它工作正常。

    以下是内联变量的代码:

    @TaskAction
    void encryptFile() {
        assertThatEncryptionKeyIsPresent()
        createNewOutputFileIfNotExists()
        final FileOutputStream outputStream = new FileOutputStream(outputFile)
        inputFile.eachLine { String line ->
            final byte[] inputBytes = line.getBytes()
            final byte[] secretBytes = key.getBytes()
            final byte[] outputBytes = new byte[inputBytes.length]
            int spos = 0
            for (int pos = 0; pos < inputBytes.length; ++pos) {
                outputBytes[pos] = (byte) (inputBytes[pos] ^ secretBytes[spos])
                spos += 1
                if (spos >= secretBytes.length) {
                    spos = 0
                }
            }
            outputStream.write(Base64.encodeBase64String(outputBytes).getBytes())
        }
    }
    

    下面是提取方法变量的代码:

    @TaskAction
    void encryptFile() {
        assertThatEncryptionKeyIsPresent()
        createNewOutputFileIfNotExists()
        final FileOutputStream outputStream = new FileOutputStream(outputFile)
        inputFile.eachLine { String line ->
            byte[] outputBytes = encryptLine(line)
            outputStream.write(Base64.encodeBase64String(outputBytes).getBytes())
        }
    }
    
    private byte[] encryptLine(String line) {
        final byte[] inputBytes = line.getBytes()
        final byte[] secretBytes = key.getBytes()
        final byte[] outputBytes = new byte[inputBytes.length]
        int spos = 0
        for (int pos = 0; pos < inputBytes.length; ++pos) {
            outputBytes[pos] = (byte) (inputBytes[pos] ^ secretBytes[spos])
            spos += 1
            if (spos >= secretBytes.length) {
                spos = 0
            }
        }
        outputBytes
    }
    

    如何使用这个加密行的私有方法来解决这个问题?

    1 回复  |  直到 5 年前
        1
  •  1
  •   M.Ricciuti    5 年前

    此错误似乎与此处引用的groovy问题相关: https://issues.apache.org/jira/browse/GROOVY-7797

    您试图从同一类中的一个闭包调用私有方法,但似乎不支持这种方法。

    请尝试删除 private 修饰语 encryptLine 方法定义,您应该消除这个错误。