• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reflection API pros and cons

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any suggestions ?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic