Rnet
Rnet

Reputation: 5040

Java reflection and metaprogramming

I've an array of beans and use reflection to retrieve the values within them through their getter methods. All the beans are of the same type, so instead of using reflection for every object, is there any way of generating code(or object) which will have the calls and I can regenerate this object for a different type of bean? I've heard of something like this in Lisp macros, is it possible in Java?

Upvotes: 1

Views: 308

Answers (3)

Miki
Miki

Reputation: 7188

Generics may help you in reducing code repetition before you compile and release the class.

You can also compile and deploy class at run-time, see How can I compile and deploy a java class at runtime?

Upvotes: 1

You could take a look at JAXP (http://download.oracle.com/javase/tutorial/jaxp/index.html) where you can generate beans from xml definitions. For heavy reflection work I find BeanUtils (http://commons.apache.org/beanutils/) helpful.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533472

Yes, You can use Velocity (and other tools) to generate the code. However unless performance is critical, it 10x simpler to use reflection.

Upvotes: 0

Related Questions