Java_day21_ Reflections and annotations

Source: Internet
Author: User

Reflections and annotations

I. Reflections

Reflection (Reflection) is one of the characteristics of a Java programming language that allows a running Java program to obtain its own information and to manipulate the internal properties of a class or object. The type of the object in the program is determined at compile time, and the Java reflection mechanism can dynamically create the object and invoke its properties, so that the type of the object is unknown at compile time. So we can create objects directly from the reflection mechanism, even if the type of the object is unknown at compile time.
The core of reflection is that the JVM dynamically loads a class or invokes a method/access property at run time, and it does not need to know who is running the object in advance (write code or compile time).

Reference Blog

The Java Reflection Framework provides the following features: reference

    • 1. Determine the class to which any object belongs at run time;
    • 2. Constructs an object of any class at run time;
    • 3. At run time, judge the member variables and methods that any one class has (through reflection can even call the private method);
    • 4. Methods for invoking any object at run time

  (i), the method of getting the constructor -Constructor

    1. onstructor(Class[] params) -- 获得使用特殊的参数类型的公共构造函数
    2. Constructor[] getConstructors() -- 获得类的所有公共构造函数
    3. Constructor getDeclaredConstructor(Class[] params) -- 获得使用特定参数类型的构造函数(与接入级别无关)
    4. Constructor[] getDeclaredConstructors() -- 获得类的所有构造函数(与接入级别无关)

  (b). ways to get field information- Field

    1. Field getField(String name) -- 获得命名的公共字段
    2. Field[] getFields() -- 获得类的所有公共字段
    3. Field getDeclaredField(String name) -- 获得类声明的命名的字段
    4. Field[] getDeclaredFields() -- 获得类声明的所有字段

  (iii). received Methods of getting information about methods -method

    1. Method getMethod(String name, Class[] params) -- 使用特定的参数类型,获得命名的公共方法
    2. Method[] getMethods() -- 获得类的所有公共方法
    3. Method getDeclaredMethod(String name, Class[] params) -- 使用特写的参数类型,获得类声明的命名的方法
    4. Method[] getDeclaredMethods() -- 获得类声明的所有方法

  (iv). Three ways to get the class type

1. Get the object based on the class

person person = new person ();

Class clazz = Person.getclass ();

2. Access by class

Class clazz = Person.class;

3. Get through the class name

Class clazz = Class.forName ("Com.lanou3g.code0510.reflection.Person")

(v) The code that invokes the method through reflection:

person person = new person ();//create object of person class

Person.setname ("Zhang San");//Set property value

Person.setage (19);//Set property value

Class Clazz = person.class;//Create Class object

Method Showmethod = Clazz.getdeclaredmethod ("show");//Get this method by means name

Showmethod.setaccessible (TRUE);//Allow this class of methods to be accessed

Showmethod.invoke (person);//Call this method

      

Two. Annotations

It provides a new way of structuring and having type checking capabilities, which allows programmers to add metadata to code without causing the code to be cluttered and difficult to read.

1. Basic format: Public @interface annotation name {definition body}

2. Four meta-annotations, annotations for retouching annotations

1). @Target: Indicates where the annotation can be used.
The values (ElementType) include:
CONSTRUCTOR: Used to describe the constructor
Field: Used to describe the domain
Local_variable: Used to describe local variables
Method: Used to describe methods
Package: Used to describe packages
PARAMETER: Used to describe parameters
Type: Used to describe classes, interfaces (including annotation types), or enum declarations

2). @Retention: Indicates the level at which the annotation information needs to be saved.

The values (retentionpolicy) include:
Source: Valid in the original file (that is, the source file is reserved)
Class: Valid in class file (that is, class reserved)
Runtime: Valid at run time (that is, run-time retention), so you can read the annotation information through the reflection mechanism.

3). @Documented: Indicates that this annotation is included in the Javadoc.

4). @Inherited: Indicates that subclasses are allowed to inherit annotations from the parent class.

Reference Blog

Java_day21_ Reflections and annotations

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.