[jdom-interest] converting Elements to objects

Tatu Saloranta cowtowncoder at yahoo.com
Thu Feb 17 21:14:44 PST 2005

--- "Gregory S. Hill" <ghill at vmtllc.com> wrote:

> I know almost nothing about Reflection, (other than
> it's supposed to be 
> this really nifty-slick technology that solves all
> problems magically!) 
> but would there be a way of using it to analyze the
> setters 
> (transformers?  I get lost in the terminology
> sometimes) and 
> automatically set the values according to the
> element names in the XML? 
>   Am I completely talking out of my ass right now?
> :)

Nope -- using Reflection is the obvious way to create
automated Object/XML bindings dynamically, along the
lines you are thinking of. It can dynamically figure
out available methods, and call them. There is some
overhead in doing so (both regarding convenience of
method calls and performance), but that's just part of
the trade-off; it buys flexibility.
So if you want to make a truly flexible system, that's
one obvious way to go, and used by most existing
binding system. Using Reflection API is bit cumbersome
(since it was bolted on to JVMs afterwards, not an
initial part of Java), but not all that complicated.
Another bit more advanced alternative is to generate
equivalent byte-code (optimally in run-time), usually
based on Reflection-obtained or configuration file

There are other ways to solve the problem, and there's
often the compromise between writing/generating glue
code (to get better static type safety, but do more
monkey work one way or the other) and losing
type-safety (using generic 'getString("author");',
'getInt("salary")' etc methods).

-+ Tatu +-

Do you Yahoo!? 
Take Yahoo! Mail with you! Get it on your mobile phone. 

More information about the jdom-interest mailing list