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

com.buger.patcher.resolver.impl.SourceIfTargetNullFieldValueResolver Maven / Gradle / Ivy

Go to download

Java Object Patcher is simple, dependency-free and fast library which makes patching/merging objects simple.

The newest version!
package com.buger.patcher.resolver.impl;

import com.buger.patcher.exception.FieldValueResolverException;
import com.buger.patcher.resolver.FieldValueResolver;

import java.lang.reflect.Field;

/**
 * @author Created by Buheria Oleksii {@literal [email protected]}
 * @version 1.0
 * @since 29-09-2020
 */
public class SourceIfTargetNullFieldValueResolver implements FieldValueResolver {

    private final Field field;

    public SourceIfTargetNullFieldValueResolver(Field field) {
        this.field = field;
    }

    public Object resolveFieldValue(Object source, Object target) throws FieldValueResolverException {
        try {
            Object targetFieldValue = field.get(target);
            Object result;
            if (targetFieldValue == null) {
                result = field.get(source);
            } else {
                result = targetFieldValue;
            }
            return result;
        } catch (IllegalAccessException e) {
            throw new FieldValueResolverException();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy