All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wildfly.clustering.marshalling.protostream.reflect.FieldMarshaller Maven / Gradle / Ivy

/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.marshalling.protostream.reflect;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.function.Supplier;

import org.infinispan.protostream.descriptors.WireType;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter;

/**
 * A very generic marshaller for use with classes whose state is not publicly available for reading or writing except by pure reflection.
 * @author Paul Ferraro
 */
public class FieldMarshaller implements ProtoStreamMarshaller {

    private final Class type;
    private final Supplier factory;
    private final Field[] fields;

    public FieldMarshaller(Class type, Class... memberTypes) {
        this(type, defaultFactory(type), memberTypes);
    }

    private static  Supplier defaultFactory(Class type) {
        Constructor constructor = Reflect.getConstructor(type);
        return () -> Reflect.newInstance(constructor);
    }

    public FieldMarshaller(Class type, Supplier factory, Class... memberTypes) {
        this.type = type;
        this.factory = factory;
        this.fields = new Field[memberTypes.length];
        for (int i = 0; i < this.fields.length; ++i) {
            this.fields[i] = Reflect.findField(type, memberTypes[i]);
        }
    }

    @Override
    public Class getJavaClass() {
        return this.type;
    }

    @Override
    public T readFrom(ProtoStreamReader reader) throws IOException {
        T result = this.factory.get();
        while (!reader.isAtEnd()) {
            int tag = reader.readTag();
            int index = WireType.getTagFieldNumber(tag);
            if ((index > 0) || (index <= this.fields.length)) {
                Reflect.setValue(result, this.fields[index - 1], reader.readAny());
            } else {
                reader.skipField(tag);
            }
        }
        return result;
    }

    @Override
    public void writeTo(ProtoStreamWriter writer, T source) throws IOException {
        for (int i = 0; i < this.fields.length; ++i) {
            Object value = Reflect.getValue(source, this.fields[i]);
            if (value != null) {
                writer.writeAny(i + 1, value);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy