org.simpleflatmapper.reflect.BuilderBiInstantiator Maven / Gradle / Ivy
package org.simpleflatmapper.reflect;
import org.simpleflatmapper.util.ErrorHelper;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public final class BuilderBiInstantiator implements BiInstantiator {
public final Instantiator builderInstantiator;
public final MethodBiFunctionPair[] chainedArguments;
public final MethodBiFunctionPair[] unchainedArguments;
public final Method buildMethod;
public final boolean ignoreNullValues;
private boolean notStatic;
public BuilderBiInstantiator(
Instantiator builderInstantiator,
MethodBiFunctionPair[] chainedArguments,
MethodBiFunctionPair[] unchainedArguments,
Method buildMethod,
boolean ignoreNullValues) {
this.builderInstantiator = builderInstantiator;
this.chainedArguments = chainedArguments;
this.unchainedArguments = unchainedArguments;
this.buildMethod = buildMethod;
this.ignoreNullValues = ignoreNullValues;
this.notStatic = !Modifier.isStatic(buildMethod.getModifiers());
}
@Override
@SuppressWarnings("unchecked")
public T newInstance(S1 s1, S2 s2) throws Exception {
try {
Object builder = newInitialisedBuilderInstace(s1, s2);
if (notStatic) {
return (T) buildMethod.invoke(builder);
} else {
return (T) buildMethod.invoke(null, builder);
}
} catch (InvocationTargetException e) {
return ErrorHelper.rethrow(e.getCause());
}
}
public Object newInitialisedBuilderInstace(S1 s1, S2 s2) throws Exception {
Object builder = builderInstantiator.newInstance(null);
for (MethodBiFunctionPair argument : chainedArguments) {
Object v = argument.getFunction().apply(s1, s2);
if (!ignoreNullValues || v != null) {
builder = argument.getMethod().invoke(builder, v);
}
}
for (MethodBiFunctionPair argument : unchainedArguments) {
Object v = argument.getFunction().apply(s1, s2);
if (!ignoreNullValues || v != null) {
argument.getMethod().invoke(builder, v);
}
}
return builder;
}
}