private TypeEncoder findTypeEncoder()

in protonj2/src/main/java/org/apache/qpid/protonj2/codec/encoders/primitives/ArrayTypeEncoder.java [291:354]


    private TypeEncoder<?> findTypeEncoder(ProtonBuffer buffer, EncoderState state, Object value) {
        // Scan the array until we either determine an appropriate TypeEncoder or find
        // that the array is such that encoding it would be invalid.

        if (!value.getClass().isArray()) {
            throw new IllegalArgumentException("Expected Array type but got: " + value.getClass().getSimpleName());
        }

        TypeEncoder<?> typeEncoder = null;

        if (value.getClass().getComponentType().isPrimitive()) {
            Class<?> componentType = value.getClass().getComponentType();

            if (componentType == Boolean.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Boolean.class);
            } else if (componentType == Byte.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Byte.class);
            } else if (componentType == Short.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Short.class);
            } else if (componentType == Integer.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Integer.class);
            } else if (componentType == Long.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Long.class);
            } else if (componentType == Float.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Float.class);
            } else if (componentType == Double.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Double.class);
            } else if (componentType == Character.TYPE) {
                typeEncoder = state.getEncoder().getTypeEncoder(Character.class);
            } else {
                throw new IllegalArgumentException(
                    "Cannot write arrays of type " + componentType.getName());
            }
        } else {
            Object[] array = (Object[]) value;

            if (array.length == 0) {
                if (value.getClass().getComponentType().equals((Object.class))) {
                    throw new IllegalArgumentException(
                        "Cannot write a zero sized untyped array.");
                } else {
                    typeEncoder = state.getEncoder().getTypeEncoder(value.getClass().getComponentType());
                }
            } else {
                if (array[0].getClass().isArray()) {
                    typeEncoder = this;
                } else {
                    if (array[0].getClass().equals((Object.class))) {
                        throw new IllegalArgumentException(
                            "Cannot write a zero sized untyped array.");
                    }

                    typeEncoder = state.getEncoder().getTypeEncoder(array[0].getClass());
                }
            }
        }

        if (typeEncoder == null) {
            throw new IllegalArgumentException(
                "Do not know how to write Objects of class " + value.getClass().getName());
        }

        return typeEncoder;
    }