A, annotations,

1. What are annotations

Annotations: Also known as Java annotations, annotations alone are used as annotations. Annotations only make sense if they are used in conjunction with techniques such as reflection and staking.

2. What are meta-annotations

Meta-annotation: the simple understanding is the annotation of the annotation. (Annotations added to annotations are called meta-annotations.)

@Retention(RetentionPolicy.RUNTIME) // For what time, in code? Compile time? The run-time?
@Target({ElementType.TYPE})//test the purpose of the annotation. (Where to use)
public @interface test {
}
Copy the code

Where @Retention and @Target are meta-annotations.

3. Introduction of common meta-annotations

@Retention: Indicates the current annotation and how long it will be retained.
1.SOURCE: The compiler will lose modified annotations after running, and compile-time annotations will not exist. 2.CLASS: Compile-time annotations exist, but run-time annotations are gone. 3.RUNTIME: Annotations exist at any time.Copy the code
@target: Where do annotations work
1.TYPE: class, interface, enumeration 2.FIELD: FIELD declaration, including enumeration constants 3. 4.PARAMETER: on the PARAMETER. 4. () 5. 7.ANNOTATION_TYPE: annotated. 8. PACKAGE: a PACKAGE. TYPE_PARAMETER: type parameter declaration. 1.8 10.TYPE_USE: Type use.Copy the code

4. Default annotations

Annotations in Java implement the Annotation interface by default.

package java.lang.annotation;
public interface Annotation {  
    boolean equals(Object obj);
    int hashCode(a);  
    String toString(a);
    Class<? extends Annotation> annotationType();
}
Copy the code

2. Usage scenarios of annotations

1. Source-level examples Lint checks the @intdef annotation

This annotation is used to replace enumerations. Since each enumeration argument to an enumeration generates an object that takes up too much space, use @intdef to replace it. This annotation is only used for lint checking. Reported errors are only reminders and do not affect program compilation and execution.

@retention (SOURCE) @target ({ANNOTATION_TYPE})// annotate public @interface IntDef { int[] value() default {}; boolean flag() default false; boolean open() default false; }Copy the code

2. Source-level annotations, APT(Annotation Processor Tools) annotation processor

APT technology is derived from Javac command, when coding bytecode, add plug-in, the plug-in can generate automatically generated Java classes. (APT is not packaged into APK, only compiled.)

3. Notes from the editorial period. Bytecode staking technology (Bytecode enhancement Technology)

Bytecode staking: Simply write code in. Class. (You can skip the syntax check, classes generated in APT can not be used, only bytecode to skip the syntax check, add code to the bytecode)

4. Compile-time annotations. AOP aspect programming. (Annotations and bytecode staking techniques)

With annotations, add code with annotations, don’t add code without annotations. (All codes that need to be logged in must be added to the operation of logging in, and those that do not need to be added will not be added.)

5. Runtime annotations. Reflection technology

Interview question 1: Basic types of final modifiers, can they be modified by reflection?

Answer: Non-basic types of final modifiers can be modified. Basic types are determined at compile time because compile-time optimizations cause variables of final types to be changed directly to constant values. So you can’t modify the result of reflection. If you want to modify fianl, you need to initialize fianl in the constructor.

Interview question 2: Why is the reflection slow? (The effect is actually small)

1) The invoke method requires boxing and unboxing operations: primitive data types that are first boxing and then converted into an array of Objects. The final use of the need for unpacking operations.

2) Reflection needs to retrieve classes and methods by name: traverse the class to find them.

3) Check the method: Check the permission of the method and check the parameters of the method, including parameters and arguments.

4) The compiler cannot optimize for dynamically dropped code, such as inline: reflection involves dynamically resolved types, affects the judgment of introversion and cannot be JIT.

Dynamic proxy

1. How to use dynamic proxy

public static void main(String[] args){
        jiatianlong hao = new jiatianlong();
        // Return a new object. And a new class generated by dynamic proxies.
        student student = (com.lzl.proces.proxy.student) Proxy.newProxyInstance(
                hao.getClass().getClassLoader(),// Class loader
                new Class[]{student.class},// Array of interfaces
                new ProxyInvokeHandler(hao)// The agent handler (interpreted as a callback).
        );
        student.play();
    }
Copy the code
public class ProxyInvokeHandler implements InvocationHandler {
    private Object realObject;
    public ProxyInvokeHandler(Object realObject){
        this.realObject = realObject;
    }

    / * * * *@paramO Proxy object *@paramMethod Calls the method *@paramObjects method parameter *@return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

        returnmethod.invoke(realObject,objects); }}Copy the code

2. Principle of dynamic proxy

Bytecode generation: generates a bytecode (class object) for you. This bytecode is a class that inherits the proxy and implements the corresponding interface.