java - Groovy 字节数组被隐式转换为 ArrayList

标签 java arrays arraylist groovy casting

我的错误:

groovy.lang.MissingMethodException: No signature of method: static test.getLittleEndianSize() is applicable for argument types: (ArrayList) values: [[-128, 0, 0, 0]]

我严格使用字节数组,并且从不在任何地方使用 def/List,所以这真的让我失望。有什么想法为什么会发生这种情况吗?据我所知,groovy 不会重新定义像 byte[] 这样的基元来实现 List,而且我还没有找到类似的错误。

这最初在一个更大的项目中给我带来了麻烦,我将其简化为这段代码。

class test {
    static class Packet {
        byte[] data

        Packet(String data) {
            this.data = data.replaceAll(":", "").decodeHex()
        }

        int size() {
            return data.size()
        }
    }

    static class PacketList {
        List<Packet> packets
        long totalDataSize

        PacketList(String data) {
            packets = new ArrayList<Packet>()
            totalDataSize = 0
            addPacket(data)
        }

        void addPacket(String data) {
            packets += new Packet(data)
            totalDataSize += data.size()
        }

        long getLittleEndianSize() {
            if (packets.size() <= 0)
                return 0

            // Verifying that we're for sure using byte[]
            byte[] firstPacketBytes = packets[0].data
            if (firstPacketBytes?.size() > 0) {

                byte[] lilSizeBytes = Arrays.copyOfRange(firstPacketBytes, 0, 4)
                return getLittleEndianSize(lilSizeBytes)
            }
            return 0
        }

        // No logic in place to decrement size yet
        // No real need for this test
        //
        long getTotalDataSize() {
            return totalDataSize
        }
    }


    static long getLittleEndianSize(byte[] data) {
        if (data?.size() < 4) return -1

        long returnVal = 0
        for (int i = 0; i < 4; i++) {
            returnVal += ((long) data[i] & 0xffL) << (8 * i)
        }

        return returnVal
    }


    static void main(String[] args) {
        String testByteStr = "80:00:00:00:ff:d8:11:12:13:14:15:d9:ff"

        def packets = new PacketList(testByteStr)
        println "${packets.getTotalDataSize()} bytes | Little Endian Header: ${packets.getLittleEndianSize()}"
    }
}

最佳答案

groovy 2.4.11

有问题的简化类:

public class A{
    public static long fa(byte[]b){
        return b.length;
    }
    static class B{
        byte[] b="123".getBytes();
        long fb(){
            return fa(b); // <<< line 9
        }
    }

    public static void main(String [] arg){
        System.out.println ( ">>>" + new B().fb() );
    }
}

抛出异常:

groovy.lang.MissingMethodException: No signature of method: static A.fa() is applicable 
for argument types: (java.util.ArrayList) values: [[49, 50, 51]]
Possible solutions: fa([B), is(java.lang.Object), wait(), any(), find(), wait(long)
   at A$B.fb(ConsoleScript49:9)
   at A.main(ConsoleScript49:14)

似乎 groovy 无法从外部类中找到静态方法...

您会看到 (java.util.ArrayList) 错误,因为 groovy 尝试查找该方法的不同变体,这可能是最后一次尝试。

在java中此代码已成功编译。

要在 groovy 中修复此问题,请在外部类或内部类上添加 @groovy.transform.CompileStatic 注释,或指定确切的方法位置:

        return A.fa(b); // <<< line 9

关于java - Groovy 字节数组被隐式转换为 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51625216/

相关文章:

java - 如何用 swing 杀死一个线程?

java - 使用应用服务器的主要好处是什么?

java - 如何避免 POST Url 参数中的字符串插值?

c - 数组调试C?

javascript - Angular 获取共享指令的所有元素的列表

java - 为什么将 ArrayList 值加载到 HashMap 后不打印?

c# - C# 中的 ArrayList 性能测试

java - 如何在java nio中创建多个监听同一端口的udp客户端?

将int数组转换为C中的单个整数

java - 堆算法。非常基本,关于数组位置 0 和 1。