Note the Annotation

Annotation: Add @xxx to a method, class, field to comment or qualify (input/output rules) or to inject code, help write duplicate code, etc. Annotation alone is meaningless, but combined with reflection, peg, etc., the possibilities are endless.

Annotation statement

All annotations implement the Annotation interface by default.

Annotation declaration: An @ symbol precedes an Interface to indicate annotation.

Yuan notes

Meta annotations: The annotations used for annotations are important in two ways: @target ({elementtype.type}) and @retention (retentionPolicy.source).

1. Target: Declare where this annotation is used. Methods? The class? Parameters? Variable? There are multiple rows of {elementtype. TYPE, elementtype. FIELD} “, “separated.

Annotations are retained on the SOURCE code: Classes that have access to annotations and annotation declarations at compile time include information about all members of the class, and are generally used to generate additional auxiliary classes. Examples: APT technology, IDE syntax checking

CLASS: Annotations remain in bytecode: After a CLASS is compiled, the CLASS data is modified for the purpose of modifying the code logic. Annotations can be used to distinguish whether changes are needed or to determine whether changes are made to a different logic. Examples: bytecode enhancement (writing code on.class files), AspectJ, hot fix Roubust

RUNTIME: Annotations are retained at RUNTIME. Annotations and their elements are dynamically retrieved during program execution using reflection techniques to make different logical decisions. Example: Reflection

reflection

Reflection is the ability to know all the properties and methods of any class in the running state; For any object, you can call any of its methods and properties; And can change its properties.

The Java reflection mechanism provides the following functions:

  • Constructs an object of any class at run time
  • Gets or modifies the member variables and methods that any class has at run time
  • Call methods (properties) of any object at run time

Class

Reflection begins with Class, a Class that encapsulates information about the Class of the current object. A class has class names, attributes, methods, constructors, and so on

Get the Class object:

Obtain the name of the class from the class name Class 2 Obtain the name of the object from the object getClass() 3 Get class.forname (Class name) classloader.loadClass (Class name)

Create an instance

There are two main ways to generate objects through reflection. 1. Use newInstance() to create an instance of the corresponding Class.

2. Get the Constructor object from the Class object and then call the Constructor object’s newInstance() method to create an instance. (Execute newInstance() to the constructor.)

Get constructor

Public T newInstance() public T newInstance(Object… initargs)

Member variable (field)

To obtain method information

Reflection gets the generic real type

When we reflect on a generic class, we need the real data types in the generic to perform operations such as JSON deserialization. This needs to be done through the Type system. The Type interface contains an implementation Class and four implementation interfaces: TypeVariable Generic Type variables. You can generalize upper and lower limits and other information; ParameterizedType Specifies a generic Type, which can be used to obtain the signature Type of the generic Type in the metadata. GenericArrayType This interface implements Type when the Type to be described is an array of generic classes, such as List[],Map[]. WildcardType Wildcard generic to obtain upper and lower limits of information

TypeVariable

ParameterizedType

GenericArrayType

WildcardType

Gson deserialization

Gson converts String to Json:

FromJson () takes one of two arguments, class or type:

Why not use class, because generics do type erasure at compile time. The Object concrete type becomes Object, the Action Bean in BeseBean<> treats it as Object and will not return the Object.

Typetolen.gettype () can be used to fetch the specific type

New TypeTolen<> () +{

{} indicates that this creates an anonymous inner class, without {} it is an object. If it is an Object, then the generic T is erased and becomes Object. But a + {} instead declares an anonymous inner class. T becomes a class, erasing a class file, and T points to that file. (approximate name) xxx.BaseBean $1.class. This $1.class is an anonymous inner class of BaseBean. Then there is no T generic inside the anonymous inner class, which is a concrete ParameteBean that will not be erased

The agent

A proxy can create a new class proxy pattern at run time that implements a given set of interfaces and provides a proxy object to an object that controls references to the original object. Generally speaking, the agency model is a common intermediary in our life. Objective :(1) to access the target object indirectly by introducing proxy object, so as to prevent unnecessary complexity brought to the system by direct access to the target object; (2) Access control through proxy objects;



The proxy mode has three roles:

Abstract role: Refers to the public methods provided by the proxy role and the real role, usually as an interface

Real roles: You need to implement an abstract role interface that defines the business logic that real roles implement for invocation by proxy roles. That’s where the real business logic is.

Proxy role: Needs to implement the abstract role interface, is a proxy for the real role, through the real role’s business logic methods to implement the abstract method, and can attach its own operations. Put unified process control into the agent role!

Static proxy Write dead proxy.

A dynamic proxy

Dynamic proxies are essentially the same as static proxies, most importantly the Proxy class is created with proxy.newProxyInstance ().

Example:

PutExtra (“name1″,” the value of AActivty “)

1. Automatic findViewById code screenshot

2. Automatic injection getIntent().getStringExtra() code screenshot

3. Automatically inject setOnclickLinser to achieve butterknife effect

Improvement, the above writing method is very stupid!!

The Intent passes a value through the Bundle. Put is actually putting the name and value into the Bundle

Bundle.putstring () is stored in ArrayMap<String, Object> mMap

So we get getStringExtra, getStringExtra,getIntExtra(String Name, int defaultValue) from the Bundle

The Bundle is an ArrayMap<String, Object> a Map of keys and values stored in it

Optimized code

From the above analysis, it is found that there is no need to use the various types of values in getIntent, and then a bunch of if to determine the various types, and then extract. You can just get it out of the Bundle using the get key

The test code



Source code address:

All the code above are study notes, not for commercial use gitee.com/winding2015…