Why can Java support reflection? The answer is that the Java runtime still has type information, which contains everything: what fields it has, what methods it has, what level of protection it has, and so on, and what classes this class relies on. In Java, class information is stored as an object, which is a meta object, and their type is class. With this information, it is easy to create objects dynamically or to call some methods. In C + +, with Rtti (runtime type recognition), we can also know some information about the class, but why there is no reflection in C + + because the type information is incomplete. Rtti The name itself tells us that C + + type information is used for type recognition, so it does not require additional information. Not C + + is not able to do this, but C + + does not want to add additional burden to users. There are gains, there must be lost, therefore, C + + abandoned the Meta object. In this regard, the father of C + + Bjarne Stroustrup in his "C + + language design and Evolution," section 14.2.8 in the in-depth discussion.
The Meta object is the material foundation of Java reflection, then what is its spiritual foundation? Why should Java support reflection? After the above discussion, we take this question further, why does Java provide the Meta object?
To discuss this issue, we have to pull back 10 years ago, when Java has just come to the stage of formal history. Java was actually born in the years before, when it was called oak, and the environmental restrictions made this epoch-making masterpiece was shelved. When Netscape kicked off the internet drama, Java became a Phoenix Nirvana, one of the important reasons is that Java is network-centric.
careful observation, we will find that the entire Java infrastructure is designed for network services. The first is the most famous Cross-platform platform in Java. In fact, before the Java era, people also need to consider the portability between platforms, but this kind of transplant is concentrated in the source level, which is the C language can be popular one of the reasons, in a stand-alone environment, platform differences are not so obvious. The advent of the network highlights the differences between platforms because the network may connect a wide variety of computers and devices. Yes, there are devices, and you probably know that Java's initial development is related to embedded devices. Once applications are cross-platform, program development and post management maintenance will be greatly simplified, and portability is promoted from the source level to the binary (Java byte-code). So Cross-platform is actually laying the groundwork for the web. Another important point in Java--the relationship between security and the Web is closer, and anyone can think of a couple of reasons to relate the two.
Take a look at how the Java infrastructure supports the network. Remember how Java was originally attracted to people? Yes, applets. Familiar with the principle of friends know that the applet is run to the remote class files downloaded to the local to execute. Compared to the local hard drive, the network gives us the feeling that a word ———— slow. If the Java adoption of traditional executable file organization, that is, a complete executable file, the entire applet to download the operation, only to wait until the flowers also thanked. Java uses the technique is to take the file apart, the class for the organization, this is the class file we see today. In this way, the execution process becomes the first class to be downloaded before it can be run, saving the initial wait time greatly. A good design divides a program into several modules, so most programs cannot be written in a class. Therefore, the class file must contain the classes it uses. For the boot section, we can get it started in a specific way, for example, the familiar main method is placed in a particular byte, but there is no way to specify any method without a method, and the method of one class calling another class is so arbitrary that the class file must contain information about the class method. Further field information is added so that almost a complete class of information comes out, and the information corresponds to a meta object. Therefore, the Meta object appears in the Java infrastructure.
With the Meta object, reflection also became a natural thing. With the Reflection,java also has the ability to dynamically expand, so you can greatly improve the flexibility of the program.
A few more words about the Java infrastructure's support for the network. Class files are carefully designed to be very compact in their own right to facilitate transmission over the network, and the presence of jar files is designed to facilitate network transmission, because if you only transfer one class at a time, a lot of it is wasted in establishing a network connection. The jar file makes it possible to transfer multiple classes at a time, and we also know that the data in the jar file is compressed, which can further reduce the download time. Java infrastructure support for the network, "dive into the Java Virtual Machine" (second edition) of the 4.3 section is well elaborated, interested may wish to look at.
Thinking about reflection gives me a chance to think deeply about the design of Java itself. A good software design needs a core idea as a support, everything revolves around the core, and for Java, the core is the network.
An interesting thought experience!