Java Reflection mechanism: class information at run time (bugs exist for the framework service)

Source: Internet
Author: User


  • reflection mechanism : the Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any one of its methods This dynamic acquisition of information and the ability to dynamically invoke the object's methods is called the reflection mechanism of the Java language.

  • JVM simply checks the class to see which particular class it belongs to (like Rtti). You must load the class object for that class before you can do anything else with it. Therefore, the .class file for JVM Must be available, either on the local machine or on the network. So the real difference between rtti and reflection is only that, for rtti , when the compiler compiles again open and check .class file. (In other words, we can call all methods of an object in "normal" mode) and for reflection mechanism , .class file is not available at compile time, so open and check at runtime .class file.

  • For a project example:
    We started the project after the start, if we need to change the DAO of this project somewhere, according to the OCP principle of our design pattern: to increase the openness, to close the modification, so we are not easy to modify the code, because we usually use a configuration file to set what kind of DAO our project needs Such as:

    Factory = Shop.dao.PropertiesFactory
    Userdao = Shop.dao.UserDao
    Addressdao = Shop.dao.AddressDao

So we'll be able to modify what we need in the future, and we don't need to change the code in the project, but the question is, if we get the class we need by string, that's one of the causes of the reflection.

 Properties prop = Propertiesutil.getdaoprop (); String fs = Prop.getproperty ( "            Factory ");            Class CLZ = Class.forName (FS); String mn =  "getinstance"             ;            Method m = Clz.getmethod (MN); f = (Ifactorydao) m.invoke (CLZ);  

In this code we can see: Class CLZ = Class.forName (FS), we can get a class through the Class.forName () method (others can see the next i paste the Chinese SDK Document link learning). This allows us to dynamically acquire a class, which is one of the benefits of the reflection mechanism.

    • Reflection mechanism can actually do a lot of, in the framework of learning is also a great use. Next is the hard dish, the personal feeling to look at the document, there is definitely a great advantage. The class class in Java supports the concept of reflection with the Java.lang.reflect class library
      Class
      Java.lang.reflect
      An example that lets you understand the Java reflection mechanism

Java Reflection mechanism: class information at run time (bugs exist for the framework service)

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.