Java Dependency Injection Library Framework Dagger source Analysis (i)

Source: Internet
Author: User
Tags key string

1, Generatedadapters

The corresponding comments above say:

A single point to API used in common by adapters and Adapter generators

Widely used by producers of adapters and adapter

Through the code, it can be noted that this is a final class and is not allowed to be overloaded.

His constructor is an empty constructor function.

Also with the definition of the following constants:

private static final String SEPARATOR = "$$";
public static final String Inject_adapter_suffix = SEPARATOR + "Injectadapter";
public static final String Module_adapter_suffix = SEPARATOR + "Moduleadapter";
public static final String Static_injection_suffix = SEPARATOR + "Staticinjection";


In terms of variable names, the approximate meaning is the definition of the delimiter constant string symbol, the suffix of the injected adapter, the suffix of the model adapter, and the suffix of the static injection.

2, Reflectiveatinjectbinding

The corresponding comments above say:

Injects the {@code @Inject}-annotated fields and constructors of a class
Using Reflection.

Code that injects annotation-related member variables into a class file by reflection and the constructor's code

His member variables contain the following sections:

Private final field[] fields;
Private final ClassLoader loader;
Private final constructor<t> Constructor;
Private final class<?> supertype;
Private final string[] keys;
Private final binding<?>[] fieldbindings;
Private final binding<?>[] parameterbindings;
Private binding<? Super T> supertypebinding;

The corresponding description is the following:

1. Array of field in reflection

2. Class Loader

3, reflection in the constructor of the object, support generics

4, class type, support generics

5. Key string array

6. Member variable in reflection field bound array

7. Array of parameter bindings

8. Arrays of super type bindings


In the constructor,

The corresponding description is this:

@param keys keys for the fields, constructor parameters and supertype in
That order. These is precomputed to minimize reflection when {@code
Attach} is called multiple times.
@param constructor The injectable constructor, or null if this binding
Supports injection only.
@param supertype The injectable supertype, or null if the supertype is a

The approximate meaning is described as follows:

The first set of parameters key, for member variables field, constructor parameters, and superclass in a specific order of key, these will be

It is calculated in advance so that the reflection is minimized when the additional code is called repeatedly.


The second parameter, constructor, can be injected into the constructor, and if the binding is simply to support the injection of the member, then constructor is a null pointer.


In the attach function, we see an object that contains a parameter linker,

From the code of the body of the function, the main thing to do is to use the linker object to request a link binding for the type of the member variable, constructor, superclass, respectively.


In the Get function, we can see that we are using constructor to create an object of the specified generic type.


In the Injectmembers function, the main loop field array binds the object that needs to be injected with the specified Index object in the field array-bound object.


The main functions of the CREATE function are understood through related annotations.

Lookup the injectable fields and their corresponding keys.

Find the member variables that can be injected and the keys associated with them.


Look up @Inject-annotated constructors. If there ' s no @Inject-annotated
constructor, use a default public constructor if the class have other
Injections. Otherwise treat the class as non-injectable.

Find the annotated constructor, if there is no already annotated constructor, and this class has other annotations, use a constructor of the default public type.

Otherwise, this class is not to be annotated.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Dependency Injection Library Framework Dagger source Analysis (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.