I'm looking for clever ways to build dynamic Java classes, that is classes where you can add/remove fields at runtime. Usage scenario: I have an editor where users should be able to add fields to the model at runtime or maybe even create the whole model at runtime.
Some design goals:
- Type safe without casts if possible for custom code that works on the dynamic fields (that code would come from plugins which extend the model in unforeseen ways).
- Good performance (can you beat
HashMap
? Maybe use an array and assign indexes to the fields during setup?) - Field "reuse" (i.e. if you use the same type of field in several places, it should be possible to define it once and then reuse it).
- Calculated fields which depend on the value of other fields
- Signals should be sent when fields change value (no necessarily via the Beans API)
- "Automatic" parent child relations (when you add a child to a parent, then the parent pointer in the child should be set for "free").
- Easy to understand
- Easy to use
Note that this is a "think outside the circle" question. I'll post an example below to get you in the mood :-)