Java Novice Learning Guide [day22] --- Annotation and Reflection

1. Annotation

Definition: Also called metadata, label, comment, it is the data used to describe the data, it is the comment at the code level, it is for the code to see.

Four built-in annotations: Annotations: built-in annotations (comments that have been defined by the JDK)

@Target determines where the annotation is used

1.@Override

Role: to verify whether the current method is a method rewrite

Usage: Method

2.@Deprecated

Function: The mark is outdated, outdated does not mean that it cannot be used, but it is not recommended

Usage: construction methods, fields, local variables, methods, packages, parameters, classes

3.@SuppressWarings

Role: Suppressing warnings is not a fundamental solution to the problem. To put it bluntly, it is to eliminate the yellow warning line on Eclipse to make the code look tidy.

Usage: classes, fields, methods, parameters, construction methods, local variables

Pass value: you can pass value when you use it, the value that SuppressWarings can pass: all

4. @SafeVarargs suppresses heap pollution warning (a separate warning)

Role: Suppress heap pollution warning

Usage: construction method, method

Note: SafeVarargs was added in later versions, and SuppressWarings was used to solve it before

Four meta-annotations: annotations used to define annotations

1、@Target

Function: Used to explain the location of [annotation]

ElementType:

TYPE: Class FIELD: Field

METHOD: Method PARAMETER: parameter-parameter name

CONSTRUCTOR: construction method LOCAL_VARIABLE: local variable

ANNOTATION_TYPE: Annotation PACKAGE: Package

TYPE_PARAMETER: Parameter type TYPE_USE: anywhere

2.@Retention

Role: used to explain the [annotation] life cycle

RetentionPolicy:

SOURCE: source code CLASS: bytecode period RUNTIME: operating period

3.@Documented

Function: Used to explain whether the current comment is displayed after the document is generated

4.@Inherited

Role: The annotation defined by @Inherited is inherited

Variables in annotations: type variable name ()

Pass value of annotation:

General way: name=value, array name={value 1, value 2, value 3,...}

Other common methods: When only one value is needed in the annotation and the name is value, the name can be omitted.

Note: Custom annotations are implemented by a third-party program

2. Reflection (if you want to use privatized members, first ignore the permission check setAccessible(true))

class: ClassAn instance of a class represents the classes and interfaces in the running Java application.

1. Enumeration is a kind of class----->getting method 1, 2, 3

2. Annotation is an interface----->getting method 1, 3

3, each of the array belongs to a class is mapped to a Class object, all of the array element having the same type and dimension of which is shared Classobjects -----> 1,2 acquisition method

4, basic Java types boolean( byte, char, short, int, long, , floatand double) and keywords voidare also represented as Classobjects. ----->Getting method 1. The packaging class corresponding to the basic data type holds the Class instance of the basic data type

Three ways to obtain class objects

1、类名.class
2、对象名.getClass();
3、Class.forName(String str);

Reflection acquisition construction method

		1、通过Class对象创建
			Object newInstance(); 要求:Class对象里面必须要有公共无参数的构造方法
		2、获取构造方法再创建对象
			获取构造方法
			---------------获取公共的构造方法----------------
				Constructor<T> getConstructor(Class<?>... parameterTypes)
    			//参数------parameterTypes - 参数数组 
				Constructor<?>[] getConstructors()
			---------------获取所有的构造方法----------------
				Constructor getDeclaredConstructor(Class<?>... parameterTypes)
  			  //参数--------parameterTypes - 参数数组 
				Constructor[] getDeclaredConstructors()
			创建对象
				T newInstance(Object... o);

Reflection acquisition method

		1、获取Class对象里面的方法
		---------------获取公共的方法(包括继承来的)----------------
			Method getMethod(String name, Class<?>... parameterTypes)
    		// name - 方法名
			// parameterTypes - 参数列表
			Method[] getMethods()
				
		---------------获取所有的方法(不包括继承来的)----------------
			Method getDeclaredMethod(String name, Class<?>... parameterTypes)
    		//name - 方法名
			//parameterTypes - 参数数组 
			Method[] getDeclaredMethods()
			
		2、调用获取到的方法
			Object invoke(Object o,Object... obj);
			o:调用方法的对象
			obj:方法需要的实参

Reflect to get fields

	1、获取Class对象里面的字段
		---------------获取公共的字段----------------
			Field getField(String name) 
    		// name - 字段名
			 Field[] getFields() 
		---------------获取所有的方法(不包括继承来的)----------------
			 Field getDeclaredField(String name)  
    		//name - 字段名
			Field[] getDeclaredFields()  

Guess you like

Origin blog.csdn.net/WLK0423/article/details/109785677
Recommended