io.javarig.generator.ArrayGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-random-generator Show documentation
Show all versions of java-random-generator Show documentation
This project aims to provide a random instances generator for any class in java, it iterates through setters of an empty object and sets the fields values (using Java reflection api) randomly
The newest version!
package io.javarig.generator;
import io.javarig.RandomInstanceGenerator;
import io.javarig.exception.InstanceGenerationException;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.List;
@Setter
@Getter
public class ArrayGenerator extends TypeGenerator {
public ArrayGenerator(Type type, RandomInstanceGenerator randomInstanceGenerator) {
super(type, randomInstanceGenerator);
}
@Override
public Object generate() throws InstanceGenerationException {
Class> arrayParameterType = ((Class>) getType()).componentType();
if (arrayParameterType.isPrimitive()) {
return generatePrimitiveArray(arrayParameterType);
} else {
return generateArray(arrayParameterType);
}
}
private Object generatePrimitiveArray(Class> primitiveType) {
Class> wrapperType = ClassUtils.primitiveToWrapper(primitiveType);
return ArrayUtils.toPrimitive(generateArray(wrapperType));
}
private Object[] generateArray(Class> arrayParameterType) {
List