com.buger.patcher.resolver.impl.SourceIfTargetNullFieldValueResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of object-patcher Show documentation
Show all versions of object-patcher Show documentation
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();
}
}
}