Enhanced Java basics-reflection, annotation, and java basic reflection Annotation
- Enhanced infrastructure
- 1. Generic
- Binary reflection
- Three annotations
Basic enhancement I. Review generic
-
Basic concepts of generics:
-
To
ArrayList<E>For example:
-
①
ArrayList<E>E In is called the type parameter variable
-
ArrayList<Integer>The Integer in is called the actual type parameter.
-
② Whole
ArrayList<E>Generic Type
-
Whole
ArrayList<Integer>ParameterizedType)
-
Define a generic method:
:
// <T>: Declaration of the generic definition, before the return value. Public <T> T findOne (Class <T> clazz) {}// for static generic methods, you must define them before using public static <T> void findTwo () {}// defines multiple generic public static <K, V> V find (K k );
-
Define a class with generics:
// Generic definition at the class level. You can directly use the "instance Method" in the class. Public class Demo <T> {}
Ii. Generic reflection
// Obtain the parent Class with generic information, for example, BaseDao <User>, which is called ParameterizedType and Type is the Class interface Type = XXX. class. getGenericsSuperclass (); ParameterizedType ptype = type; Clazz clazz = ptype. getActualTypeArguments () [0];
Iii. Annotations
-
Introduction
-
① Since JDK5.0, Java has added support for MetaData (MetaData), that is, Annotation (Annotation ).
-
② Annotation is actually a special mark in the code, which is used to replace the configuration file. In the traditional method, the configuration is used to tell the class how to run. With Annotations, developers can use annotations to tell the class how to run.
-
③ Annotated in Java Technology
TypicalApplication: You can get the annotation in the class through the reflection technology to determine how to run the class.
-
Basic annotations
-
① SupperssWarnings: Alarm
-
Custom Annotation
-
① Annotation Definition
public @interface MyAnnotation {}
-
2. annotation attributes: Neither method nor field.
Annotation attributes can use the following types: primitive (basic type), String, Class, Annotation, enum, or a one-dimensional array of the above types.
If you assign values to attributes in the annotation, you can specify only one value or multiple values.
Public @ interface MyAnnotation_1 {// annotation attribute String name (); // int age () default 18 ;}
-
③ Use annotations and attributes:
public class UseAnnotation { @MyAnnotation_1(name = "ABC", age = 18) public void test1(){}}
-
Annotation reflection
-
Involved APIs: java. lang. reflect. AnnotatedElement
-
Class, Constructor, Field, Method, and Packages all implement this interface.
// Obtain the specified annotation type that you own. aType is an annotation type bytecode object. Annotation getAnnotataion (Class <T> aType); // returns all Annotation [] getAnnotations (); // determines whether you have specified Annotation type boolean isAnnotationPresent (Class <? Extends Annotation> aType );
-
Class Three States
-
①SOURCE:. Java File
②CLASS:. Class File
③RUNTIME: Class in the memory. The class loader loads the. class file into the memory.
-
Lifecycle of Annotation
-
① Annotated
DefaultStored
RangeYes: bytecode status. RUNTIME does not exist.
-
Use meta annotation to change the survival range of the Annotation
-
① Annotations can only be used for annotation, that isMeta Annotation.
@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation_1 {}
-
Use meta annotation to change the place of Annotation
-
① Put it on the class: @ Target (ElementType. TYPE)
-
② Put it on the METHOD: @ Target (ElementType. METHOD)
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface MyAnnotation_1 {}