简体   繁体   中英

groovy error: Primitive type literal: byte cannot be used as a method name

I am used to python-style REPL testing of code on a shell and I am learning Java. I recently learnt that almost all Java code can be executed in a REPL manner via groovy. So far the groovy console has helped me quickly test my Java code snippets.

I am trying to run the following Java code in the groovy console (which I happened to get from another Stackoverflow question ):

String md5(String s)
{
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<messageDigest.length; i++)
        hexString.append(Integer.toHexString(0xFF & messageDigest[i]));

        return hexString.toString();
}

String md5hash = md5("a test message");

When I try to execute this, I am getting the following error message:

Primitive type literal: byte cannot be used as a method name at line: 5 column: 13. File: ConsoleScript0 at line: 6, column: 13

I searched for the error message here on SO and elsewhere, but I couldn't get any clues. It seems to be valid Java code, so why does groovy think that I'm trying to use "byte" as a method name?

I'm using Groovy Version: 1.8.4 JVM: 1.6.0_26

I don't know why it's giving exactly that error message, but try the more idiomatic way of declaring the variable:

byte[] messageDigest = digest.digest();

(I'd also strongly recommend that you don't use String.getBytes() without specifying a character encoding. I assume you don't really want the result to be platform-specific.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM