Commit 10d911ad authored by Alex Segers's avatar Alex Segers

[AFP-91] 🎨 Create bean 'fieldCombiner' to copy fields from one class to another (@asegers)

parent c9c621d4
package com.afp.ordermanagement.config;
import com.afp.ordermanagement.model.Manager;
import com.github.javafaker.Faker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Field;
import java.util.Arrays;
import static java.util.Objects.isNull;
@Configuration
public class BeanConfig {
@Bean
public static Faker faker() {
return new Faker();
}
@Bean
public static FieldCombiner fieldCombiner() { return new FieldCombiner(); }
public static class FieldCombiner {
public Object combine(Object target, Object source, String[] omitFields) {
try {
for (Field field: Manager.class.getDeclaredFields()) {
String fieldName = field.getName();
field.setAccessible(true);
Object sourceValue = field.get(source);
if (!isNull(sourceValue) && Arrays.binarySearch(omitFields, fieldName) <= 0)
field.set(target, sourceValue);
}
} catch (Exception ignore) { }
return target;
}
public Object combine(Object target, Object source) {
return this.combine(target, source, new String[0]);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment