Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Why should I use reflection?

Does it allow access to private members which would otherwise be inaccessible, thus breaking the intentions of the original design ?

Is it really slow ?

Or something else ?

share|improve this question
1  
I love commentless downvoting –  NimChimpsky Oct 21 '12 at 13:09
1  
Although the question is phrased differently, the answers (especially the accepted answer) address all of the points brought up in this question. It seems reasonable to direct people to what appears to be considered a quality question. –  Thomas Owens Oct 21 '12 at 18:42
add comment

marked as duplicate by Morons, Mark Trapp, Walter, Thomas Owens Oct 21 '12 at 18:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

Does it allow access to private members which would otherwise be inaccessible, thus breaking the intentions of the original design ?

Yes it does. However, this is only a problem if someone does this. And as a general rule people don't do it ... unless there is a very good reason.

Is it really slow ?

Yes. Most reflective operations are least an order of magnitude slower than their non-reflective versions.

Or something else ?

Another couple of reasons:

  • More thorough testing and debugging is required because you are replacing compile-time type checking with run-time type checking.

  • Reflective code is more fragile in the face of API changes.

  • You can't allow non-trusted code to use reflection. (This is a corollary of your first reason.) Fortunately, the SecurityManager can be used to enforce this,

share|improve this answer
    
what does it mean "an order of magnitude slower"? –  Louis Rhys Oct 21 '12 at 14:48
4  
An order of magnitude is essentially ten, or one-tenth. See en.wikipedia.org/wiki/Order_of_magnitude –  Robert Harvey Oct 21 '12 at 15:36
add comment

Why is Java's reflection package considered the dark side of the force?

It is incorrect to describe a language feature somehow inferior without deciding on its application first. There are applications for reflection where the feature is indispensable: for example, configuring parts of your applications based on textual descriptions or XML takes much less code if you use reflection.

On the other hand, an ability to break encapsulation makes some purists cringe. However, the criticism usually comes from people who did not find a good use for reflection yet. The same is true about other language features: I am sure you have heard similar rant about recursion from people who have not mastered the feature beyond the recursive factorial example.

share|improve this answer
2  
Let alone the ternary operator ?:... –  Michael Kjörling Oct 21 '12 at 16:15
2  
@MichaelKjörling: A great example. The loudest rants about ?: are usually from people who don't understand the difference between a statement and an expression. Sometimes you really need an expression. –  Goran Jovic Oct 21 '12 at 16:32
add comment

The reflection package offers... reflection. It implies you can see, use and even modify the private members of the classes, whether they are your own classes or the classes of somebody else.

It obviously breaks the data wrapping system, that can be dangerous if the original developer considered it as a kind of protection against the external unallowed usage. Imagine you can run the private doItWithoutAnyCheck() method...

You can even shortcut some weak security systems based on a trusted entity being in charge of an authentication or access delivery. Thanks to the reflection, you can change the behaviour of this trusted element in way that advantage you.

This is for the bad side. The good news is, thanks to reflection, you get an important power of what you can do with your program. For instance, you can get a deep understanding of the entities involved in a program as well as their interactions. You can then reify your application (that is, making something a first-class citizen). The reflection is heavily used in program analysers (for instance, for performance inspection). The idea here is to understand the execution of an other program as deeply as possible: what are the instancied objects? On which methods do we wait most of the time? In order to answer these questions, you need to manipulate concrete entities representing abstract entities, such as objects and methods.

The reflection can also be used in order to dynamically change the behaviour of your program. For instance, you can use a monitor controling if your objects are not performing illegal operations and correct them if needed. You can also modify your objects during the execution so that their behaviour depends on a global context (here is a phd thesis focused on this topic).

As many tools, the reflexion package is not inherantly good or bad. It depends how you use it.

Until Java 1.6, the reflection framework encounters a real performance problem. Using reflection significantly slowed down the execution. But recent efforts have made it more efficient.

share|improve this answer
add comment

Does it allow access to private members which would otherwise be inaccessible, thus breaking the intentions of the original design ?

exactly

you can access private fields with getDeclaredField(String) which returns a Field instance

and after you call setAccessible(true) on it you can change the field on any object (with that field) to whatever you like with the set methods of the Field instance

share|improve this answer
    
I love commentless downvoting –  ratchet freak Oct 21 '12 at 14:43
    
I didn't downvote, but your answer isn't really instructive; it more or less says part of what the OP already said, but with a little more detail. –  Robert Harvey Oct 21 '12 at 15:37
add comment

Reflection isn't all bad, and it's not all good... It's a useful tool that can be misused.

On the one hand, testing frameworks are a good place to use it (not the only good use).

On the other hand, when someone has mastered it creating a test framework, some I've worked with have then been tempted to use it in application code to demonstrate how clever they are, resulting in code that's hard to understand and hard to maintain.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.