Modification

OBJECTO

Objecto provides a powerful mechanism to customize and adapt generated objects based on specific requirements or scenarios. This flexibility enables developers to tailor objects to their exact needs, ensuring that the generated data aligns with the desired context. Objecto supports the modification of generated objects through both parameters of factory methods and modifier methods.

@Modifier

Parameters

Specify modification parameters using @Modifier annotation in factory methods to alter specific fields or invoke methods during generation.

When the code is compiled with the -parameters flag, the parameter names are retained in the compiled bytecode. This means that Objecto can infer the parameters without the explicit use of @Modifier. Developers can choose whether to use @Modifier for clarity or rely on parameter names when compiling with the -parameters flag.

Example:

// Using @Modifier for clarity
Issue createIssue(@Modifier("type") Type type);
Issue createIssue(@Modifier("type=?") Type type);
Issue createIssue(@Modifier("setType(?)") Type type);

// When compiled with -parameters, the parameter name "type" is inferred
Issue createIssue(Type type);

Methods

Define @Modifier methods with a return type of the factory interface itself.

Example:

@Modifier("type")
IssueFactory withType(Type type);

@Modifier("type=?")
IssueFactory withType(Type type);

@Modifier("setType(?)")
IssueFactory withType(Type type);

Complex Modifiers

Modifiers can also be complex, allowing modification of nested fields or collections.

Examples:

// Modifying the status of all subtasks
@Modifier("subtasks[*].status")
private IssueFactory withAllSubtaskStatuses(Status status);

// Modifying the commenter of the first comment
@Modifier("comments[0].commenter=?")
private IssueFactory withFirstCommenter(User commenter);

@Modifier("dependencies.put(?, ?)")
IssueFactory withDependency(DependencyType type, Issue issue);

Applying Modifications to Custom Factory Methods

Developers can apply modifications to custom factory methods using the modifier methods provided by the factory interface.

Example:

Issue modifiedDefaultIssue = issueFactory
                                .withStatus(Status.RESOLVED)
                                .createDefaultIssue(assigneeUser);

Last updated