All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.javarig.generator.EnumGenerator Maven / Gradle / Ivy

Go to download

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 lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Type;

@Getter
@Setter
public class EnumGenerator extends TypeGenerator {

    public EnumGenerator(Type type, RandomInstanceGenerator randomInstanceGenerator) {
        super(type, randomInstanceGenerator);
    }

    @Override
    public Object generate() {
        Object[] enumConstants = ((Class) getType()).getEnumConstants();
        if (enumConstants.length == 0) {
            return null;
        }
        return enumConstants[getRandom().nextInt(0, enumConstants.length)];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy