nyarian
nyarian

Reputation: 4375

Gradle custom task implementation: could not find method for arguments

I have an encryption task that receives an input file and an output file along with the key to perform encryption against. The strange thing is that when I try in extract a method that performs a line encryption and receives it as an argument, I get the next error: Could not find method encryptLine() for arguments [PK] on task ':presentation:encryptScenarios' of type EncryptionTask.. When I inline this method - it works ok.

Here is a code of the inlined variant:

@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())
    }
}

And here is the code of the extracted method variant:

@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
}

How to resolve this issue using this private method that encrypts a line?

Upvotes: 0

Views: 1850

Answers (1)

M.Ricciuti
M.Ricciuti

Reputation: 12106

This error seems to be connected to a Groovy issue referenced here : https://issues.apache.org/jira/browse/GROOVY-7797

You are trying to call a private method from a closure within the same class and it looks like this is not supported.

Please try to remove the private modifier from encryptLine method definition, and you should get rid of this error.

Upvotes: 1

Related Questions