Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection API pros and cons  RSS feed

 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to decide should I use the Reflection API or not in one of my projects.
And I would like to hear the pros and cons of using Reflection for a J2EE web app. from your experiences?

The main reason for me to use the Reflection API is to create new Class fields (members) during runtime.
I need to define new fields for the class I coded before runtime. (compiled class) or to create new classes.

Is that operation allowed or provided by Refection API or Can I only call Classes' compiled methods such as private methods and fields during runtime?

Regards,



Tuna TÖRE
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection only allows you to use pre-existing classes, methods and fields. It is possible to create classes on the fly but I can't remember the name of the library. Adding new fields to existing classes may be possible with Aspect4J but I'm not sure.
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestions ?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's not so much the Reflection API you'd use, but rather dynamic proxies. Or maybe that's considered part of the Reflection API these days. Personally, I'd consider using a library like Javassist instead, which has the ability to handle source code generated at runtime; I don't think dynamic proxies can do that.

There's also cglib, but that hasn't seen updates for almost 3 years, so I'd be hesitant to introduce it into a project.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!