[Javase entry series] Chapter 2 reflection

Source: Internet
Author: User
Tags access properties
Chapter 4 reflection-v512 workshop Editor: Xuan Yu
Reflection API Java. Lang. Reflect. Array Get Class Object
Java. Lang. Reflect. Field Java. Lang. Reflect. Modifier My blog
Java. Lang. Reflect. Method Java. Lang. Reflect. Constructor V512 Studio

Reflection mechanism (reflection)
Overview: Java programs can dynamically load, parse, and use types that are not fixed during compilation. This mechanism is called introspection or reflection.
Reflection allows the program to parse and obtain the new type, and then manipulate it. This is different from the programming method in the past.
In the past, the data to be used was clearly defined in the source code, including variables, methods, and their data types before they can be accessed or called.
Function: You can operate on this type or its instances, including access properties, call methods, and create new objects. However, the method is very different from the previous usage.
It is also a data type that can be determined only during loading and running. You can also parse the class structure and obtain its internal information.
Note: Sometimes a data type and its method name can be determined in the program. If you do not use the reflection API, you cannot create an object of this type or call its method.
In fact, in Web programming or later use of various JavaBean, as well as Dao data design patterns, as well as some Java classes when manipulating data in hibernate, etc.
In fact, reflection mechanisms are involved in their underlying layers. For example, in the source code of this tutorial, testreflection05 is a practical example.
Supplement: Reflection mechanisms are rarely or almost never used in actual development. Only when using design patterns and frameworks such as JavaBean, struts, and Dao
As a programmer, you should know that the underlying layer will automatically load new data types based on the reflection mechanism, parse the type structure of this class, and access and manipulate the members in it.
In addition, programming through reflection is less efficient than non-reflection. Therefore, unless necessary, it is generally not recommended to use the reflection mechanism in development.

 

 

 

Reflection API
Class: Java. Lang. Class class. An instance of the class is used to indicate the Java data type at runtime.
Includes classes, interfaces, arrays, enumerations, annotations, and basic data types. It also includes a special Java data type, namely, void
When a class is loaded, the Java Virtual Machine automatically creates the corresponding class object.
API package: Java. Lang. Reflect package. The following are commonly used, which are the main components of Java reflection APIs.
Java. Lang. Reflect. Field ------ describes the attributes of members in the Java class, also known as fields or member variables.
Java. Lang. Reflect. Method ----- describes methods in the Java class, including static methods and common member methods.
Java. Lang. Reflect. constructor --- description Constructor
Java. Lang. Reflect. modifier ----- description identifier
Java. Lang. Reflect. array ------ dynamically create and access Java arrays. Is a dedicated array operation tool.
(Object-Oriented Programming allows users to define new data types and describe the information to be studied or processed .)
In fact, each class object can be considered to correspond to a Java data type, including the reference type and basic data type.
Continued 1: multiple packages may be introduced in a Java source file. Sometimes, for convenience, they are directly written in the form of import java. util. *.
In this case, you can open the source file in eclipse and right-click the file-→ Source-→ organize imports to display the specific class names in the used package.

 

 

 

Get Class Object
Reference: There are three methods for referencing data types:
1) Call the static method class. forname ()
Class. forname ("p1.person ");
Class. forname ("oracle. JDBC. Driver. oracledriver ");
2) Call the getclass () method defined in the object class
Person P = new person ();
Class C1 = P. getclass ();
Class C2 = "helloworld". getclass ();
3) use a. Class expression
Class C1 = string. Class;
Class C2 = p1.person. Class;
Class C3 = oracle. JDBC. Driver. oracledriver. Class;
Note: The package level cannot be omitted in the first method, because in the eyes of the compiler, it is only a string and it does not check whether the package and class actually exist.
The package hierarchy can be omitted in the third method, because the corresponding class name will be identified during the compilation phase, and it will find the corresponding class in the introduced package
In fact, the class expression is neither a method nor an attribute, but a Convention expression.
Basic: There are two methods for basic data types and void:
1) Use a. Class expression
Class C1 = int. Class; --- this is just an agreed tag, not to say that the int type encapsulates the class attribute.
Class C2 = double. Class;
Class C3 = void. Class;
2) Call The. Type attribute of the corresponding encapsulation class
Class C1 = integer. type; --- the returned Class Object corresponds to the int type rather than the integer type. The two are actually different.
Class C2 = double. type;
Class C3 = void. type;

 

 

 

Java. Lang. Reflect. Field
Obtain the Field object: Provided by the class
Public field [] getdeclaredfields () // return all attributes declared in the current class hierarchy. No matter what the access control permission is, all will be returned, but does not include the attributes declared in the parent class hierarchy.
Public field getdeclaredfield (string name) // return the specific attribute declared in the current class hierarchy. Regardless of the type of access control permission, the returned value is a singular field object.
Public field [] getfields () // return all public attributes declared in all levels of the current class (including the current class level and its parent class level)
Public field getfield (string name) // return a specific attribute in the current class, which must be public. It can also be in the parent class
Main field member Methods
Public object get (Object OBJ) // get the attribute value of the specified object
Public void set (Object OBJ, object Value) // set the object property value to the specified value
Public XXX getxxx (Object OBJ) // get the attribute value of an object
Public void setxxx (Object OBJ, xxx X) // set the attribute of an object to the specified value.
Public String getname () // returns the current property name
Public int getmodifiers () // returns the modifier of the current attribute. The return value is the pattern value of the modifier.
Public <t extends annotation> T getannotation (class <t> annotationclass) // returns the annotation before the current domain or this attribute. If not, null is returned.
Public String tostring () // is overwritten. Returns the complete format of the current property in string format. Including its modifier, type name, initial value, annotation, and other information
Public Boolean equals (Object OBJ) // is overwritten. Compare whether the current field object is equivalent to another field object. As long as the type and name are the same, it is considered equivalent.

 

 

 

Java. Lang. Reflect. Method
Get method object: Provided by the class
Public method [] getdeclaredmethods () // gets all the methods declared at the current class level, excluding the parent class, and does not restrict access control permissions
Public method getdeclaredmethod (string name, class <?>... Parametertypes) // return a specific method declared in the current class hierarchy
// Name is the method name, and the form parameters use the generic mechanism and variable parameters.
Public method [] getmethods () // access the public method defined by all layers of the current class (including the parent class)
Public method getmethod (string name, class <?>... Parametertypes) // This is the access-specific method.
Method main member Method
Public object invoke (Object OBJ, object... ARGs)
Public String getname () // return method name
Public class <?> [] Getparametertypes () // return parameter list
Public int getmodifiers () // return Modifier
Public class <?> Getreturntype () // return the type of the returned value of the current method
Public class <?> [] Getexceptiontypes () // returns the current method declaration and throws an exception type
Public <t extends annotation> T getannotation (class <t> annotationclass) // obtain possible annotations of the current method
Public String tostring () and Public Boolean equals (Object OBJ) // The tostring () and equals () methods are rewritten.

 

 

 

Java. Lang. Reflect. Modifier
Method provided by class: Public int getmodifiers () // gets the modifier object
Modifier class method: public static Boolean ispublic (INT mod) // whether the parsing mode value contains a modifier
Public static Boolean isprivate (INT mod) // because the mode value may also appear in combination
Public static Boolean isprotected (INT mod)
Public static Boolean isabstract (INT mod)
Public static Boolean isfinal (INT mod)
Public static Boolean isstatic (INT mod)
......
Public static string tostring (INT mod) // convert the specified mode value to the corresponding string

 

 

 

Java. Lang. Reflect. Constructor
Get the constructor object: Provided by the class
Public constructor <?> [] Getdeclaredconstructors ()
Public constructor <t> getdeclaredconstructor (class <?>... Parametertypes)
Public constructor <?> [] Getconstructors ()
Public constructor <t> getconstructor (class <?>... Parametertypes)
Constructor main member Method
Public t newinstance (object... initargs) // create a new object
Public String getname ()
Public class <?> [] Getparametertypes ()
Public int getmodifiers ()
Public class <?> [] Getexceptiontypes ()
Public <t extends annotation> T getannotation (class <t> annotationclass)
Public String tostring ()
Public Boolean equals (Object OBJ)

 

 

 

Java. Lang. Reflect. Array
Function: dynamically create and access Java arrays.
Method: public static int getlength (Object array) // return the length of the specified array object
Public static object get (Object array, int index) // return the value of the element at the specified subscript in the specified array. The returned value is of the object type.
Public static XXX getxxx (Object array, int index) // returns the value of the element at the specified subscript in the specified array. The element value is automatically parsed into a basic data type.
Public static void set (Object array, int index, object Value) // you can specify the value of the element at the subscript in the specified array as the specified value.
Public static void setxxx (Object array, int index, xxx Z) // you can specify the value of the element at the subscript in the specified array as the value of the specified basic data type.
Public static object newinstance (class <?> Componenttype, int length) // create a new one-dimensional array
Public static object newinstance (class <?> Componenttype, Int... dimensions) // create a new multi-dimensional array
Method provided by the class: public class <?> Getcomponenttype () // if the current Class Object corresponds to an array, this method returns the Data Type of the elements in the array.

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.