net.jqwik.engine.properties.arbitraries.randomized.AbstractFunctionGenerator Maven / Gradle / Ivy
The newest version!
package net.jqwik.engine.properties.arbitraries.randomized;
import java.lang.invoke.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.*;
import net.jqwik.api.*;
import net.jqwik.api.Tuple.*;
import net.jqwik.api.support.*;
import net.jqwik.engine.support.*;
abstract class AbstractFunctionGenerator implements RandomGenerator {
final Class functionalType;
final RandomGenerator resultGenerator;
final List>, Function super List>, ? extends R>>> conditions;
AbstractFunctionGenerator(
Class functionalType,
RandomGenerator resultGenerator,
List>, Function super List>, ? extends R>>> conditions
) {
this.functionalType = functionalType;
this.resultGenerator = resultGenerator;
this.conditions = conditions;
}
@SuppressWarnings("unchecked")
F createFunctionProxy(InvocationHandler handler) {
return (F) Proxy.newProxyInstance(functionalType.getClassLoader(), new Class[]{functionalType}, handler);
}
public Shrinkable createConstantFunction(Shrinkable shrinkableConstant) {
return shrinkableConstant.map(this::constantFunction);
}
private F constantFunction(R constant) {
InvocationHandler handler = (proxy, method, args) -> {
if (JqwikReflectionSupport.isEqualsMethod(method)) {
return handleEqualsMethod(proxy, args);
}
if (JqwikReflectionSupport.isToStringMethod(method)) {
return handleToStringOfConstantMethod(constant);
}
if (JqwikReflectionSupport.isHashCodeMethod(method)) {
return HashCodeSupport.hash(constant);
}
if (method.isDefault()) {
return handleDefaultMethod(proxy, method, args);
}
return conditionalResult(args).orElse(new Object[]{constant})[0];
};
return createFunctionProxy(handler);
}
protected Object handleEqualsMethod(final Object proxy, Object[] args) {
return proxy == args[0];
}
private Object handleToStringOfConstantMethod(final R constant) {
return String.format(
"Constant Function<%s>(%s)",
functionalType.getSimpleName(),
JqwikStringSupport.displayString(constant)
);
}
// Returns result wrapped in array to allow null as result
protected Optional