Reflection: The soul of frame design
* Framework: Semi-finished software. * Reflection: Encapsulates the components of a class as other objects. This is the benefit of the reflection mechanism * : 1. You can manipulate these objects while the program is running. 2. Can be decoupled, improve the program scalability. 1.class.forname (" full Class name ") : loads the bytecode file into memory and returns the Class object * Mostly used in configuration files, where the Class name is defined. 2. Class name: obtained from the class name attribute class * used for parameter passing 3. GetClass () : The getClass() method is defined in the Object class. Conclusion: The same bytecode file (*.class) is loaded only once during a program run, and the class object is the same no matter which method is used. * Field[] getFields() : Access to all the members of the public to modify variables * Field getField (String name) for the specified name the members of the public to modify variables * Field [] getDeclaredFields () to obtain all the member variables, * Field getDeclaredField(String name) 2. * Constructor<? >[] constructors () * Constructor<T> constructors () >... ParameterTypes) * Constructor<T> getDeclaredConstructor() >... parameterTypes) * Constructor<? >[] getDeclaredConstructors() 3. * Method getMethod() * Method getMethod(String name, class <? >... ParameterTypes) * Method[] getDeclaredMethods() * Method getDeclaredMethod(String name, class <? >... * String getName() * Field: member variable * Operation: 1. * void set(Object obj, Object value) 2. * get(Object obj) 3. Ignore the security check for the access modifier * setAccessible(true): violent reflection* Constructor * create an Object: * T newInstance(Object... Initargs) * If you use an empty parameter constructor to create an Object, the operation can be simplified: Class Object newInstance * Method: Method Object * Execution Method: * Object Invoke (Object obj, Object... Example: * Requirement: write a "framework" that can create objects of any class and execute any method of the class without changing any code of the class * implementation: 1. Step: 1. Define the full class name of the object that needs to be created and the method that needs to be executed in the configuration file 2. 3. Use reflection to load class files into memory 4. Create an object. 5. Execute methodCopy the code
Comments:
* Concept: describing the program. Note for a computer: Describing a program in words. Definition for programmers: annotations, also known as metadata. A code level description. It is a feature introduced in JDK1.5 and later and is at the same level as classes, interfaces, and enumerations. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc., to explain and annotate these elements. * Concept description: * new feature after JDK1.5 * specifies the use of annotations for programs: @annotation nameCopy the code
* Function categories: (1) Document: generate documentation using comments identified in the code (2) Code analysis: analyze code using comments identified in the code (3) Compile check: Annotations identified in the code allow the compiler to perform basic compilation checks [Override] * Some predefined annotations in the JDK * @Override: Detects whether a method annotated by this annotation is inherited from a parent class (interface) * @deprecated: * @SUPPRESswarnings: SuppressWarnings * General passthrough parameter all @Suppresswarnings ("all") * Custom annotations * Format: Public @interface annotation name {property list; } * essence: Annotation is essentially an interface, the interface inheritance Annotation interface by default * public interface MyAnno extends Java lang. The Annotation. The Annotation {} * attributes: Abstract methods in interfaces * requirements: 1. The return value type of the property has the following values * basic data type * String * enumeration * annotation * array of the above types 2. 1. If the default keyword is used to initialize the attribute when defining the attribute, the attribute value is not assigned when using annotations. 2. If only one attribute needs to be assigned a value and the attribute name is value, the value can be omitted and the value can be directly defined. 3. When an array is assigned, the value is wrapped in {}. If there is only one value in the array, {} may omit * meta annotations: an annotation that describes the annotation * @target: describes where the annotation can be applied * ElementType Values: * TYPE: applies to a class * METHOD: applies to a METHOD * FIELD: * @Retention: Describes the period by which comments are retained * @Retention(retentionPolicy.runtime) : The currently described annotation is retained in the class bytecode file and is read by the JVM to * @Documented: Describes whether the annotation is extracted into the API document * @inherited: describes whether the annotation is Inherited by a subclass * Using (parsing) the annotation in the program: Gets the values of the attributes defined in the annotation 1. Gets the location of an object (Class, Method,Field) defined by an annotation 2. * getAnnotation(Class) public Class ProImpl Implements Pro{public String * getAnnotation(Class) public Class ProImpl Implements Pro{public String className(){ return "cn.itcast.annotation.Demo1"; } public String methodName(){ return "show"; }} 3. Call the abstract method in the annotation to get the value of the configured propertyCopy the code