I remember a long time ago that I saw in a blog post that "reflection" is a great essence of Java. At that time did not understand, now participated in the internship, contact more and more after the project, only slowly feel the magical magic of reflection.
What is reflection?
Reflection in Java refers to the reflection mechanism, in the Baidu Encyclopedia : Java has a very prominent dynamic correlation mechanism: Reflection, used in Java refers to the runtime we can load, detect, using the classes is completely unknown during compilation. In other words, a Java program can load a runtime to know the name of a class, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods.
Well, I didn't know it was a very good idea.
Let's take a look at the idea: we all know that Java from writing to running, it must go through two periods: Compile period, run time,
The above two words that add black is that when compiling the class file, you can also not determine what type of class. For a chestnut: Take the basedaoimpl<t> I mentioned in my previous blog post (details can be seen in blog: http://blog.csdn.net/kklt21cn/article/details/42040597)
In the class file generated after the compilation, there is no reference to which entity class object, in other words, it is said that the class file does not run up the time do not know which entity to invoke.
However, we have to know the name of the entity that we invoke at runtime, because we need to know the name so that we can write its HQL statement. How can it be done? The answer is reflection!
In fact, in Java, we can understand it as a class file, which is also an object, which is a class object. When we can get the class object, we can use the reflection of the class object to run all of its methods, or to get its properties (field). In this way, we can write the code without having to determine the type at compile time, and we can wait until the run time to determine it.
What can reflection do?
The use of reflection is in fact ubiquitous in our daily code. As I said earlier, Basedaoimpl can reduce the amount of redundant code, such as the components inside the framework, such as Ommons-lang.jar. Inside one I prefer to use a tool class Beanutils, where an assignment method Copyproperties (object source, object target). It uses reflection to assign a JavaBean value to another javabean. There are also reflections that can be used to complete annotations. If you are interested in the annotations of children's shoes can look at this link http://www.zhihu.com/question/24401191. In the first answer, Daniel has explained how it is done from the bottom.
In fact, reflection is just one of the features of the Java language. I personally feel that understanding these advanced features slowly is a language into the door. Can also think of the value of a language from the perspective of language design. Only in this way can you be a real engineer, not just a code farmer who will copy the stickers!
If there is a mistake, please correct me.
The fun of Java "reflection"