Java's reflection API is obviously a very powerful tool, but not particularly object-oriented. What are some situations where it is appropriate (and conversely, inappropriate) to use reflection?
In my opinion...
Appropriate (clean):
Proxy
to create a proxy or a mock implementation (may be better at compile time).Appropriate hacks:
Inappropriate:
Generally reflection is much easier than generating source which is much easier than creating bytecode.
I would suggest taking a look at the book: Java Reflection in Action.
It is very good, it is detailed, and practically a complete reference about this advanced concept.
Also it's much more than we could possibly explain here, with scenarios, usages and concepts that first come clear when you simply read it :).
Like with any technology, the common sence should be the driver in determining which usage is appropriate. Not "patterns", blocking brain's ability of thinking.
Don't think of reflection as of "technology". It is just a metadata object associated with any datatype and available in runtime. Simple object, implemented in optimum possible way (just think how you would implement it and you will most likely guess the actual implementation).
From just simple considerations, it is clear that all methods on Class/Method/Field of type get-something-by-name cause a map/index lookup every time they are invoked, so this operation is somewhat worth doing just once in the application life. But once obtained, the performance of method.invoke(object, args) is pretty much the same as that of object.method(args), because it introduces only one more level of indirection, not involving any additional lookups.
Examples where I would use reflection based approach:
initialize/copy/serialize/print bean's properties by name, this task arises in most of business applications and, once you use a dynamic programming (meaning do name-based lookups only once to initialize converters/loaders and then apply the same converter along the application lifetime) adds not much overhead but adds an exellent flexibility to the software (see commons-beanutils, spring dependency injection).
transaction/session/authorization/metrics/mock proxies (using Dynamic Proxy API from JDK)
I can't think out an example where I woudn't use reflection, just don't use it where metadata-based solution is not optimal (from just analyzing what metadata object is).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With