Skip to content

readClassAndObject for generic type error for version 5.1.1 #840

@KolorYan

Description

@KolorYan

Describe the bug

Exception in thread "main" com.esotericsoftware.kryo.KryoException: Read type is incompatible with the field type: int -> Integer (xxx$RangeStruct#left)
	at com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer.read(CompatibleFieldSerializer.java:168)
	at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:880)
	at xxx.main(IndexApiContract.java:303)

com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer.read(Kryo, Input, Class<? extends T>):

    if (cachedField.valueClass != null && !Util.isAssignableTo(valueClass, cachedField.field.getType())) {
	String message = "Read type is incompatible with the field type: " + className(valueClass) + " -> "
		+ className(cachedField.valueClass) + " (" + getType().getName() + "#" + cachedField + ")";
	if (!chunked) throw new KryoException(message);
	if (DEBUG) debug("kryo", message);
	inputChunked.nextChunk();
	continue;
    }

test code:

public static void main(String[] args) throws IOException, ClassNotFoundException {

        RangeStruct<Integer> range = new RangeStruct<>(0, null);
        System.out.println(range);

        Kryo kryo = new Kryo();
        kryo.setRegistrationRequired(false);

        // this code will throw Exception in thread "main" com.esotericsoftware.kryo.KryoException: Read type is incompatible with the field type: int -> Integer (xxxx RangeStruct#left)
        kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
        // this code set chunked to true will ignore exception, but the field is ignored too.
        //        CompatibleFieldSerializerConfig config = new CompatibleFieldSerializerConfig();
        //        config.setChunkedEncoding(true);
        //        kryo.setDefaultSerializer(new CompatibleFieldSerializerFactory(config));

        Output output = getOutput();
        kryo.writeClassAndObject(output, range);

        range = (RangeStruct<Integer>) kryo.readClassAndObject(new Input(output.toBytes()));

        System.out.println(range);
    }

    private static Output getOutput() {
        return new Output(new ByteArrayOutputStream(2048)) {
            @Override
            public byte[] toBytes() {
                if (position() > 0) {
                    flush();
                }
                return ((ByteArrayOutputStream) getOutputStream()).toByteArray();
            }
        };
    }

    public static class RangeStruct<T extends Comparable<T>> {

        private T left;
        private T right;

        public RangeStruct() {
        }

        public RangeStruct(T left, T right) {
            this.left = left;
            this.right = right;
        }

        public T getLeft() {
            return left;
        }

        public void setLeft(T left) {
            this.left = left;
        }

        public T getRight() {
            return right;
        }

        public void setRight(T right) {
            this.right = right;
        }

        @Override
        public String toString() {
            return "(" + left + ", " + right + ")";
        }
    }

To Reproduce

Environment:

  • OS: [win10]
  • JDK Version: [8]
  • Kryo Version: [5.1.1]

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions