org.simpleflatmapper.reflect.impl.BuilderInstantiator Maven / Gradle / Ivy
package org.simpleflatmapper.reflect.impl;
import org.simpleflatmapper.reflect.Instantiator;
import org.simpleflatmapper.reflect.MethodGetterPair;
import org.simpleflatmapper.util.ErrorHelper;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public final class BuilderInstantiator implements Instantiator {
private final Instantiator builderInstantiator;
private final MethodGetterPair[] chainedArguments;
private final MethodGetterPair[] unchainedArguments;
private final Method buildMethod;
private final boolean ignoreNullValues;
public BuilderInstantiator(
Instantiator builderInstantiator,
MethodGetterPair[] chainedArguments,
MethodGetterPair[] unchainedArguments,
Method buildMethod, boolean ignoreNullValues) {
this.builderInstantiator = builderInstantiator;
this.chainedArguments = chainedArguments;
this.unchainedArguments = unchainedArguments;
this.buildMethod = buildMethod;
this.ignoreNullValues = ignoreNullValues;
}
@Override
@SuppressWarnings("unchecked")
public T newInstance(S s) throws Exception {
try {
Object builder = builderInstantiator.newInstance(null);
for (MethodGetterPair argument : chainedArguments) {
Object v = argument.getGetter().get(s);
if (!ignoreNullValues || v != null) {
builder = argument.getMethod().invoke(builder, v);
}
}
for (MethodGetterPair argument : unchainedArguments) {
Object v = argument.getGetter().get(s);
if (!ignoreNullValues || v != null) {
argument.getMethod().invoke(builder, v);
}
}
return (T) buildMethod.invoke(builder);
} catch (InvocationTargetException e) {
return ErrorHelper.rethrow(e.getCause());
}
}
}