Idea: feature to add accept() methods for Visitor classes
The specific use-case I'm thinking of here involves object upcasting. For example, if I have three versions of a User object schema, v1, v2, v3, which get converted to Pojos v1.User, v2.User, v3.User, and I want to create an UpcastingVisitor that will allow me to take any version object and upcast it to v3.
interface UpcastingVisitor {
void visit(v1.User user); /* upcast v1 - v3 */
void visit(v2.User user); /* upcast v2 - v3 */
void visit(v3.User user); /* no-op */
}This pattern requires each User object to have an accept() method, eg. for a v1.User:
class v1.User {
/* ... */
void accept(UpcastingVisitor visitor) {
visitor.visit(this);
}
}This is a relatively simple example, there are lots of other potential uses for this; in fact almost anything that requires functionality to be added to the JSON object is a candidate.
In terms of the implementation, I was pondering whether we could add acustomVisitorMapFactory class reference to the configuration, where the Factory creates a Map<Class,Collection<VisitorDefinition>> of Visitor definitions to apply to a given class being generated (essentially VisitorDefinition is a [visitor-class] to be used for the accept() method - or none if the class isn't supposed to be instrumented).
I've been trying to find ways of making this happen with the customRuleFactory option, but so far it seems more difficult than I was expecting. I guess another possibility might be to look at changing the way that works to make what I'm doing more feasible when taking that route. Or maybe I'm just missing something completely obvious.
Anyway, throwing this out there as an idea for discussion, I'm happy to make a pass at the implementation whichever way it falls out, but will only do that if there's general interest in the idea, and once it's been a little more concretely bedded down in discussion.
Source: joelittlejohn/jsonschema2pojo