public class ReflectiveInitializer<T>
extends Object
This class constructs new instances of another class, then fills in the new instance's fields using
key-value pairs of Strings. For the moment these may come from query parameters or Jackson JSON nodes,
but in principle they could come from anywhere.
The intended use is for objects representing incoming requests that have large numbers of parameters. Rather than
referencing every field by name when setting up defaults, then referencing them all again when handling each request
(possibly even multiple times in different HTTP method handlers), we assume that all query parameters and JSON config
fields will have exactly the same names as the Java object fields. This is getting uncomfortably close to Spring
configuration, and we should be careful to only use it in places where it truly makes code more readable.
TODO we should probably also use this for RoutingResource to make the system uniform between JSON and QParams.
TODO make this stateless (a static method) if it's not too slow
An instance of the requested class is first instantiated via its 0-argument constructor. Any initialization and
defaults should be handled at this point (in the constructor or in field initializer expressions).
Next, field and setter method names are matched with query parameters in the incoming
HttpRequest. Fields whose declared type has a constructor taking a single String argument
(including String itself) will be set from the query parameter having the same name, if one
exists. Setter methods will also be considered if 1) they have a single argument, and 2) that
argument's class has a constructor with a single String argument.
Query parameters are matched with setter methods according to the usual convention:
changing the first character to upper case and prepending 'set'. A setter method invocation will
be preferred to directly setting the field with the corresponding name, if it exists.
- Author:
- abyrd