-
Notifications
You must be signed in to change notification settings - Fork 25
Injection
In this tutorial you will learn how to non-invasively import and export designs and even read or write field parameters automatically. This feature requires the InPUT4j-*-inject.jar library and AspectJ. We use the Injection Examples project as a starting point and play with the code in here.
InPUT preaches a non-invasive design. Still, the user has to do boilerplate calls like
IDesign design = new Design("designId", "design.xml");
int var = design.get("param1");
An alternative way of setting object field parameters is by injection, which requires two things
- import the design by adding this annotation to a constructor or method definition in your program where the design should be loaded
@Input(id = "designId", file = "input.xml")
- inject the field value by adding the following annotation to the field
@Get(value = "paramId", from = "designId")
Make sure to set the @Input annotation before the first call of the object field parameter. Now, before you read the field for the first time, it will be set from the design without you having to handle designs or design spaces by hand.
Parameter values can also implicitly be injected into a design container for export. Instead of
IDesign design = new Design("designId", "design.xml");
...
design.set(var, "param1");
...
design.export(new XMLFileExporter("design2.xml"));
we can inject the parameter values back to a design by adding the following annotation to a method or constructor definition
@Set(value = "param1", of = "secondDesign", to = "var")
, after which the variable "param1" in design by id "secondDesign" should be set to the content of the object field with identifier "var". You can combine this with the explicit use of designs, and if you hold a reference to the design with id "secondDesign", try to get the value and compare it with what you expected:
int valueEqualToVar = secondDesign.getValue("param1")
To export the design, add the following annotation to a method or constructor definition after which execution you would want the design to be exported:
@Output(id = "design2", file = "design2.xml", spaceFile = "someSpace.xml")
In this tutorial we learned how to import and export designs non-invasively using annotation-based injection for more convenience. In the next tutorial, we will introduce complex parameters, parameter hierarchies, and how they can be defined in a programming language agnostic way using code mappings.