Skip to content

Generators

(see Generator Examples)


Sample objects can be created using @Generate.

Note: This GIF is already outdated, but it is still a good example.

prev

Prepare class to be generated

A class can be automatically initialized with random values by prepending the @Generate-annotation to at least one constructor:

public static class Person {
    private final String name;
    private final int age;

    @Generate // <-- required
    public Person(final String name, final int age) {
        this.name = name;
        this.age = age;
    }
}

A class can now be generated by writing the class to be generated in an entrypoint with a prescribed @Generate-annotation:

@Entrypoint
public void run(@Generate final Person person) {
    System.out.println(person); // Person{name='kiMf', age=2}
}
Generating single instance without an entry point

If you only want to use the Generate function, you can use the RandomFactory class to generate values:

public static void main(final String[] args) throws Exception {
    final Person person = RandomFactory.generate(Person.class);
    System.out.println(person);
}

If you want to create multiple instances, change the type to an array and add the @Fill-annotation to specify the amount of instances to be created:

@Entrypoint
public void run(@Generate @Fill(5) final Person[] person) {
    System.out.println(Arrays.toString(person)); // [Person{name='HCx', age=4}, Person{name='JTn', age=1}, Person{name='ahNF', age=2}, Person{name='VcuDX', age=1}, Person{name='vIMcBD', age=10}]
}
Generating array without an entry point

If you only want to use the Generate function, you can use the RandomFactory class to generate values:

public static void main(final String[] args) throws Exception {
    final Person[] person = new Person[5];
    RandomFactory.fill(person);
    System.out.println(Arrays.toString(person));
}

Supported Types

Natively Supported Generate Types
  • boolean / Boolean
  • char / Character
  • double / Double
  • int / Integer
  • String

Special Generators:

  • Arrays of the above types

These types can also be generated directly inline:

@Entrypoint
public void run(@Generate final int[] numbers) {
    System.out.println(Arrays.toString(numbers));
}

Last update: 2022-03-15