Anatomy of the Java Reflection mechanism (i)-Introduction

Source: Internet
Author: User

by the previous dynamic agent's learning again exposure to the knowledge point of reflection, the second contact so do a little bit in-depth understanding. So, for the reflection of this part of the content I intend to divide three blog to summarize. This blog first makes a general understanding of reflection, including reflection-related rtti, the understanding of definitions, and the introduction of other knowledge involved.
ReviewPrior to Java, I was exposed to the knowledge that it was in the abstract factory pattern of the big talk design that the abstraction factory was optimized by reflection + configuration files to improve its flexibility in responding to changes in requirements. The perception of reflection at the time was just that it was a technique, a technique of instantiating objects, a technique that instantiated objects that did not depend on the code that was written dead. Simply put, it is a way to get rid of an object that is instantiated with new, and it is obvious that its ability to cope with the transformation of objects is powerful.
In fact, reflection in any language is the same, but the means of implementation is different. Prior to the agent model in-depth learning process encountered a reflection, so decided this time to explore.
definitionJava can load, detect, and use classes that are completely unknown during compilation at run time. In a nutshell, Java can get information about any one by one classes at run time, build class objects (emphasizing: objects that are not the class, the class types that are later raised), generating class instances, calling methods. The information in this class includes its base class, interface, methods, and so on.
Personally, it is difficult to understand that this is " completely unknown during compilation ." So, give me a special explanation. The code as below. First of all, this note class does not exist, which means that this code is wrong. Each of the two methods can be seen separately, in fact, the method 2 compile will not pass, the hint class is not recognized type, because it does not exist in this class. While Method 1 can be compiled, execution can print "Method execution". However, an error is reported when instantiating a null pointer. The errors generated at these two different moments illustrate the term "completely unknown during compilation".
public class Client {public     static void Main (string[] args)      throws Instantiationexception, Illegalaccessexception, classnotfoundexception{                       System.out.println ("Method in execution! "); Print instructions Enter the method                       //Method 1: Instantiate class Noteclass C=class.forname ("Com.zjjReflect.Note") by reflection;       Object o=c.newinstance ();       Method 2: Directly instantiate the class note       //object o=new note ();}     }
RTTIRtti is the abbreviation for Run-time type information, which means run-time type information. , Rtti provides a way to determine the object type at run time. However, Rtti is not a technology, it should be a concept. Because different languages do not have the same form of RTTI implementation. The simple thing is to get the information of the class that the object corresponds to when the program is running. This is a bit vague, or a combination of rtti in a language to understand the implementation of the better. In Java, there are: type conversion, class type class, instanceof embodies the rtti.
class type Java.lang.ClassClass is an abstraction of all classes and interfaces, such as the name of a class, the interface that a class implements, methods, properties, and so on. You can get information about a class by using the object's class object. This is not a detailed introduction, there are separate blog additions.
Class LoaderThe process of loading a class is to load the. class into memory. Why do you mention ClassLoader here? Because the process of reflection is used in ClassLoader, and different classes need to be loaded with corresponding ClassLoader. That is, the ClassLoader is paired with the class, and the reflection is characterized by the knowledge of the class at run time.
So we also need to understand this part of the content. It is also to be able to understand the reflection better and deeper. There are four types of Java ClassLoader, respectively:
Bootstrap ClassLoader: Boot (also known as primitive) class loader.
Extension classloader: Extension class loader.
Application ClassLoader: Application class loader.
User Defined ClassLoader: Custom class loader.
Summary: when the program runs through class type class to obtain information of the target class, and then on the basis of the class information using the corresponding class loader loaded into memory, then the class in the instantiation, method invocation, and so the whole process of use. Is the detailed statement of reflection!!

Anatomy of the Java Reflection mechanism (i)-Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.