org.wildfly.clustering.marshalling.protostream.reflect.UnaryFieldMarshaller Maven / Gradle / Ivy
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.clustering.marshalling.protostream.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.function.Function;
/**
* Generic marshaller based on a single non-public field.
* @param the marshaller target type
* @param the field type
* @author Paul Ferraro
*/
public class UnaryFieldMarshaller extends UnaryMemberMarshaller {
public UnaryFieldMarshaller(Class extends T> targetClass, Class fieldClass, Function factory) {
super(targetClass, Reflect::getValue, Reflect::findField, fieldClass, factory);
}
public UnaryFieldMarshaller(Class extends T> targetClass, Class fieldClass) {
this(targetClass, fieldClass, Reflect.getConstructor(targetClass, fieldClass));
}
private UnaryFieldMarshaller(Class extends T> targetClass, Class fieldClass, Constructor extends T> constructor) {
this(targetClass, fieldClass, new Function<>() {
@Override
public T apply(F value) {
return Reflect.newInstance(constructor, value);
}
});
}
}