I started the implementation of PODAM using TDD. I immediately identified the following strategy: if, for any given POJO, there is one or more setter method with a corresponding attribute in the POJO, this will be set according to PODAM standards.
If, however, no setters can be identified for a POJO, to have its attributes set, the POJO will need to annotate a constructor with a @PodamConstructor annotation, otherwise no attributes will be set. Such constructor then needs to set the POJO attributes to their state.
A setter is considered as such only if it adheres to JavaBeans naming standards, e.g. for a field firstName a setters MUST be named setFirstName. Names are case sensitive.
The above strategy should cover inheritance, where for instance a class MyClient extends a class Client. In case the class Client has got setters to set instance variables, and such setters are accessible from MyClient, then an instance of MyClient will be created and setters for the Client parent will be invoked. If, however, Client does not expose any setters, MyClient will need to provide a constructor which invokes the super constructor and annotate such constructor with @PodamConstructor.
The idea is that, given a POJO, PODAM creates a Map of classes vs class info for each class in the POJO hierarchy, until no further parent if found (e.g. for the Object class). For this purpose I created a new class, ClassInfo, which contains the following information:
- Class name
- A Set of declared field names (e.g. only fields declared in this class)
- A Set of setters matching field names.
So in the case above, the tool will produce a Map with:
- Client -> ClassInfo[Client, [firstName, lastName, etc], [setFirstName, setLastName, etc]]
- MyClient -> ClassInfo[MyClient, [myClientId], [setMyClientId]]
The second step (for another article) will be for the tool to retrieve all valid setters from the Map, to create an instance of the POJO being created, and to invoke all setters with appropriate values. If no setters are available, PODAM will look at a constructor annotated with @PodamConstructor. For the POJO being filled with dummy values, also private fields for which a setter method is not available will be set with dummy values, since getters might be present and used by the application. This "favour treatment", however, won't be given to superclasses, since private fields with no setters are not accessible and therefore their values cannot be set.
Happy technology.
Recent Comments