Reflection in java and java reflection

Source: Internet
Author: User

Reflection in java and java reflection

1. What is reflection?

To put it simply, the java reflection mechanism is actually an encapsulation version of the I/O Stream, which is used to quickly read class files on the hard disk. The class file is equivalent to an ID card. When operating on an object, the JVM needs to obtain the attributes and functions and methods of the object based on the ID card. This kind of dynamically obtained information and the function of dynamically calling object methods is called the reflection mechanism of java language.

 

Ii. Official reflection mechanism API:

Class file: java. lang. reflect. Class class: class file in memory description

Attribute (data storage unit): java. lang. reflect. Field class: Describes the attributes in the class file in memory.

Method: Construtcor class: class file construction method described in memory

Functional Method: java. lang. reflect. Method class: describes the functional methods of class files in memory.

Iii. Attach the class file on the hard disk in three ways:

1. Class. forName ();

2. class Name. class;

3. Object. getClass ();

4. Obtain the attributes contained in the class files in the memory:

Class manages class files in the memory. Provides the corresponding method to read the attribute information in the class file.

1. Read the attributes defined only in the current class file:

GetDeclaredFields ();

2. Read the public attributes defined in the current class file and inherited from the parent class:

GetFields ();

V. Advantages and disadvantages of the reflection mechanism in java:

Advantages: 1. Ability to dynamically obtain class information at runtime, greatly providing flexibility and scalability of the program;

2. Combined with the dynamic compilation function, you can achieve super powerful functions.

Disadvantages: 1. resource-consuming, greatly reducing the running performance;

2. destroys program encapsulation. You can obtain private attributes of a class through reflection, which reduces program security.

Conclusion: Everything has its two sides. Without good or bad, it is critical to be able to use it flexibly in different scenarios.

6. Let the code tell you everything:

1 package com. bjpowernode. util; 2 3 import java. lang. reflect. field; 4 5 public class ReflectUtil {6/** 7 * if the current project java project: classPath 8 * @ param classPath 9 * @ throws ClassNotFoundException 10 */11 public static void load (String classPath) throws ClassNotFoundException {12 Class classObj = Class. forName (classPath); 13} 14/*** a class is running, it will only be loaded to the memory once 16 * how to locate the class file in the memory to notify JVM17 * To find studentVo in the memory. class file 18 * @ param classObj19 */20 public static void load (Class classObj) {21 22} 23/** 24 * notify JVM to locate the location in the memory of the class file of the current Object 25 * @ param obj26 */27 public static void load (Object obj) {28 obj. getClass (); 29} 30 31/** 32 * function: Get the total attributes of the current Object 33 * @ param obj34 */35 public static void getInfo (Object obj) {36 Class classObj = obj. getClass (); 37 Field fieldArray [] = classObj. getFields (); 38 for (int I = 0; I <fieldArray. length; I ++) {39 Field fieldObj = fieldArray [I]; 40 String fieldName = fieldObj. getName (); 41 System. out. println (fieldName); 42} 43} 44 45 public static void getInfo2 (Object obj) {46 Class classObj = obj. getClass (); 47 Field fieldArray [] = classObj. getDeclaredFields (); 48 for (int I = 0; I <fieldArray. length; I ++) {49 Field fieldObj = fieldArray [I]; 50 String fieldName = fieldObj. getName (); 51 String typeName = fieldObj. getType (). getName (); 52 System. out. println ("attribute name" + fieldName + "data type name" + typeName); 53} 54} 55 56 57}View Code1 package com. bjpowernode. entity; 2 3 public class Person {4 public String name; 5 6}View Code1 package com. bjpowernode. entity; 2 3 public class StudentVO extends Person {4 public int sid; 5 protected int age; 6 private int score; 7 8}View Code 1 package com. bjpowernode. def; 2 3 import com. bjpowernode. entity. studentVO; 4 import com. bjpowernode. util. reflectUtil; 5 6 public class get a total of attribute names in the unfamiliar Object {7 8 public static void main (String [] args) {9 10 Object stu = new StudentVO (); 11 ReflectUtil. getInfo (stu); 12 13} 14 15}View Code 1 package com. bjpowernode. def; 2 3 import com. bjpowernode. entity. studentVO; 4 import com. bjpowernode. util. reflectUtil; 5 6 public class get all attributes defined by the unfamiliar Object {7 8 public static void main (String [] args) {9 Object obj = new StudentVO (); 10 ReflectUtil. getInfo2 (obj); 11 12} 13 14}View Code

 

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.