There are three locations for annotations:
[2] Custom annotation + reflection use
[3] ElementType: Annotation use range
[4] RetentionPolicy: Specify the location where the annotation survives.
-source \ class 【default】 \ runtime
-Retain in Java files \ Retain in class files [default] \ Can still be obtained during JVM execution
[5] @interface custom annotations-default only exists in the class file; if you want to be reflected in the actual development, you need to use meta annotations.
[6] @Documented: doc documents, notes; write comments into doc documents
[7] @Target: parameters include, ElementType. [TYPE \ FIELD \ CONSTRUCTOR \ METHOOD \ PARAMETER \]
[8] @Retention: parameter, RetentionPlolicy. [RUNTIME]
[9] Add attributes to the annotation: [1] Life abstract method [2] Add default values
ex:
public String value1() default “”;
public int value2();
Regular built-in annotations
[1] @Deprecated: Outdated content
[2] @Override: rewritten method
[3] @SuppressWarnings: cancel the compiler warning
[4] @Functional Interface: functional interface, anonymous interface
[5] @SafeVarargs: Used to ignore generics, so often + final joint application
[6] @Rpeatable: Used many times
-
reflection
[1] Framework development, framework upgrade-learn Huawei customization!
[2] The problem of open source framework: security cannot be guaranteed! Network penetration vulnerability problem!
[3] Hacking-understand the importance of web security!
-----------
[1] Conventional situation: The execution of a class starts from the source file, is compiled, and then added to the class loader, and then the program is executed. --Preloading
[2] Reflection: The program is already being executed. During the execution process, the classes, methods, attributes, etc. are obtained and then organized into the current class loader. ——Dynamic execution
Class
[1] Object is used to describe the class of an object.
[2] Class: Class describing the class.
[3] Get Class:
-Obtain class by class name: Class.ForName ([full class name-package life + class name]); {If you can not locate the class, it will throw: ClassNotFoundException exception}-generally obtained in the configuration file string
-Obtained by object: new Person (). GetClass (); generally used for parameter passing of uncertain type
-Get by class: Person.class. It is generally a clear type of parameter passing
[4] Create an instance
clazz.newInstance ()-the created instance, the default is actually to call the empty parameter structure of the target class {throw exception: noSuchMethodException exception}
[5] Obtain basic information of this type
clazz.getPackage (): full package name + class name
clazz.getSimpleName()
clazz.getSuperclass()
clazz.getInterfaces ()
[6] Obtain class internal information
Field: class attributes
clazz.getField (property name)-can only access visible members
clazz.getDeclaredField (property name)-access to all members {NoSuchFieldException}
field.setAccessible (true); —— Set the access permission to be visible [if accessing a private member] {and no error if not operated}
field.set (object, value)
clazz.getFields ()-Get all visible members
clazz.getDeclaredFields ()-Get all members
field.getName()
field.getType().getSimpleName()
field.getType ()-Get attribute type
==================================
Method method = clazz.getMethod (method name, parameter type)
method.invoke (object, parameter); // Execution method
struts
Strengthen training: cost control!
-Code control; exception handling; bug solving problems;
Annotation, XML role: read configuration information.
Note For large projects, it is not easy to manage.
Exception: The program continues to execute: catch; if it does not go down, throw