Simple understanding of the Java reflection mechanism

Source: Internet
Author: User
Tags modifier stringbuffer

Java reflection mechanism: in the running state, for any class, you can know all the properties and methods of this class, for any one object can be called to any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language. This mechanism allows the program to obtain the internal information of any known name class, including its modifiers (such as public, static, etc.), superclass (such as Object), through the reflection APIs at runtime. The interfaces of implementation (for example, cloneable) also includes all the information of fields and methods, and can change the fields content or evoke methods at run time.

Features provided by the Java Reflection mechanism:

1. Determine the class to which any object belongs at run time

2. Constructing an object of any class at run time

3. At run time, the member variables and methods that any one class has

4. Methods to invoke any object at run time

5. Creating a new class object at run time

6. When using the reflection function of Java, it is essential to get the class object of the classes first, and then get the other objects through the class object.

Java gets the way class object (reflects is a custom class):

// by way of Class.forName. It's like JDBC is used to this! Class reflects_1 = Class.forName ("reflects");             // Each class in Java has its own class attribute Class reflects_2 = reflects. class ;             //  new= Reflects.getclass ();

Gets the fields of the class:

A property of a class can be obtained through a reflection mechanism, and then the property value corresponding to an instance of the class is changed. The JAVA Class<t> class provides several ways to get the properties of a class.

Public Field GetField (String name) Returns a Field object that reflects the specified public member field of the class or interface represented by this class object
Public field[] GetFields () Returns an array containing some field objects that reflect all accessible public fields of the class or interface represented by this class object
Public Field Getdeclaredfield (String name) Returns a Field object that reflects the specified declared field of the class or interface represented by this class object
Public field[] Getdeclaredfields ()

Returns an array of field objects that reflect all the fields declared by the class or interface represented by this class object

class<?> C = class.forname ("Java.lang.Integer");//get all the properties? field[] fs =C.getdeclaredfields (); //defines a variable-length string used to store propertiesStringBuffer SB =NewStringBuffer (); //each of the properties inside for(Field field:fs) {sb.append ("\ t");//SpaceSb.append (Modifier.tostring (Field.getmodifiers ()) + "");//gets the modifier for the property, such as Public,static, and so onSb.append (Field.gettype (). Getsimplename () + "");//the name of the type of the propertySb.append (Field.getname () + "; \ n");//the name of the property} System.out.println (SB);

To get the Methon of a class:

A method of a class is obtained through a reflection mechanism, and then the method that corresponds to an instance of the class is called, and the,class<t> class provides several ways to get the class.

Public Method GetMethod(String name, Class<?> .... parametertypes)

Returns a method object that reflects the specified public member methods of the class or interface represented by this class object

Public method[] getmethods()

Returns an array that contains some method objects that reflect the public member method of the class or interface represented by this class object, including those declared by it or by those classes or interfaces that inherit from the superclass and the hyper-interface.

Public Method Getdeclaredmethod(String name, Class<?> .... parametertypes)

Returns a method object that reflects the specified declared method of the class or interface represented by this class object

Public Method [] getdeclaredmethods()

Returns an array of method objects that reflect all methods of the class or interface declaration represented by this class object, including public, protected, default (package) access, and private methods, but not inherited methods

class<?> C = class.forname ("Java.lang.Integer");             = c.getdeclaredmethods ();            
New StringBuffer (); for (Method m:mts) { sb.append ("\ t"); Sb.append (Modifier.tostring (M.getmodifiers ())+ "" ); Sb.append (M.getreturntype (). Getsimplename ()+ ""); Sb.append (M.getname ()+ "\ n"); }

Simple understanding of the Java reflection mechanism

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.