Reflection mechanism details

Source: Internet
Author: User

The cornerstone of reflection-Class, with uppercase letters; this is a Class; reflection is to map various components in the java Class into the corresponding java Class. Are various java classes in java programs belonging to the same thing? Can they be described with the same thing? The Class name is the Class. The JAVA reflection mechanism is in the running state. For any Class, all attributes and methods of the Class can be known. For any object, can call any of its methods. This kind of information obtained dynamically and the function of calling methods of objects dynamically is called the reflection mechanism of java language. The Java reflection mechanism mainly provides the following functions: to judge the class to which any object belongs at runtime; to construct the object of any class at runtime; judge the member variables and methods of any class at runtime; call the methods of any object at runtime; generate a dynamic proxy. Class c = bytecode 1; each bytecode in the memory is the actual object of a Class; three methods to obtain the bytecode of the Class: Class csl1 = Date. class // bytecode 1; Type 2: Class c = p. getClass (); Class. forName ("java. lang. string "); // obtain the bytecode of this Class. If this bytecode is not found in the memory, use some methods to obtain it. The eight basic types and voids correspond to nine Class objects; calss cls = void. class; [java] package Text; public class ReflectTest {public static void main (String [] args) {String str1 = "abc"; Class cls1 = str1.getClass (); class cls2 = String. class; try {Class cl S3 = Class. forName ("java. lang. string "); System. out. println (cls1 = cls2); System. out. println (cls1 = cls3);} catch (ClassNotFoundException e) {e. printStackTrace ();} System. out. println (cls1.isPrimitive (); System. out. println (int. class. isPrimitive (); System. out. println (int. class = Integer. class); System. out. println (int. class = Integer. TYPE); System. out. println (int []. class. isPrimitive (); System. out. prin Tln (int []. class. isArray (); // use this method for arrays} [java] package Text; public class ReflectTest {public static void main (String [] args) {String str1 = "abc"; Class cls1 = str1.getClass (); Class cls2 = String. class; try {Class cls3 = Class. forName ("java. lang. string "); System. out. println (cls1 = cls2); System. out. println (cls1 = cls3);} catch (ClassNotFoundException e) {e. printStackTrace ();} System. out. println (cls1. IsPrimitive (); System. out. println (int. class. isPrimitive (); System. out. println (int. class = Integer. class); System. out. println (int. class = Integer. TYPE); System. out. println (int []. class. isPrimitive (); System. out. println (int []. class. isArray ()); // array with this method} output [c-sharp] true false true [c-sharp] true false true in short as it is in the source program type, all have their own Class instance objects. Constructor Class represents a Constructor in a Class. Example: (obtain all Constructor constructors [] = Class. forName ("java. lang. string "). getConstructor (); example: (obtain a Constructor constructors = Class. forName ("java. lang. string "). getConstructor (StringBuffer. class); // obtain the Constructor constructors = Class. forName ("java. lang. string "). getConstructor (StringBuffer. class, int. class); // get the constructor of the two parameters. Example: (create an Instance Object) Common method: String str = new String ("abc"); Reflection Method: string str = (String) Constructor. newInstance (new StrngBuffer ("abc"); eg: [c-sharp] Constructor constractor1 = String. class. getConstructor (StringBuffer. class); String str2 = (String) constractor1.newInstance (new StringBuffer ("abc"); // The StringBuffer newInstance method is required in both places to return the Object; System. out. println (str2); [c-sharp] Constructor constractor1 = String. class. getConstructor (StringBuffer. class); String str2 = (String) constractor1.newInstance (new StringBuffer ("abc"); // The StringBuffer newInstance method is required in both places to return the Object; System. out. println (str2); the Class also has a newInstance method that also creates an instance object; the Field Class represents a member variable in a Class; get public: [c-sharp] ReflectPoint rp = new ReflectPoint (3, 5); Field fieldy = rp. getClass (). getField ("y"); // fieldy does not represent a specific value; the surface is of a class and must be used to obtain the corresponding value of an object; System. out. println (fieldy. get (rp); // value on the rp object; [c-sharp] ReflectPoint rp = new ReflectPoint (3, 5); Field fieldy = rp. getClass (). getField ("y"); // fieldy does not represent a specific value; the surface is of a class and must be used to obtain the corresponding value of an object; System. out. println (fieldy. get (rp); // value on the rp object; get private: [c-sharp] ReflectPoint rp = new ReflectPoint (3, 5); Field fieldx = rp. getClass (). getDeclaredField ("x"); // fieldy does not represent a specific value; the surface is class, and it is used to obtain the corresponding value of an object; fieldx. setAccessible (true); // brute force reflection; System. out. println (fieldx. get (rp); // value on the rp object; [c-sharp] ReflectPoint rp = new ReflectPoint (3, 5); Field fieldx = rp. getClass (). getDeclaredField ("x"); // fieldy does not represent a specific value; the surface is class, and it is used to obtain the corresponding value of an object; fieldx. setAccessible (true); // brute force reflection; System. out. println (fieldx. get (rp); // value on the rp object;

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.