use of dagger.Lazy in project tiger by google.
the class NewInjectorGenerator method createAnonymousBuiltinType.
/**
* Generate for either unique a binding or a contributor to a multi-binding. NewDependencyInfo is
* null for unique one and non-null or multi-binding one. ReferencingClass is the opposite, for
* multi-binding, it is the module in dependency. Scope is null for unique binding.
*/
private TypeSpec createAnonymousBuiltinType(ClassName leafInjectorClassName, NewBindingKey key, @Nullable TypeElement scope, @Nullable TypeElement referencingClass, @Nullable NewDependencyInfo newDependencyInfo) {
Preconditions.checkArgument(key.getTypeName() instanceof ParameterizedTypeName);
boolean isMultiBinding = newDependencyInfo != null;
if (isMultiBinding) {
Preconditions.checkArgument(referencingClass == null);
Preconditions.checkNotNull(scope);
} else {
Preconditions.checkNotNull(referencingClass);
}
TypeName rawTypeName = ((ParameterizedTypeName) key.getTypeName()).rawType;
Preconditions.checkArgument(Utils.hasBuiltinBinding(key), String.format("Built-in binding expected(Provider or Lazy), but get %s", key));
boolean isLazy = rawTypeName.equals(ClassName.get(Lazy.class));
NewBindingKey elementKey = Utils.getElementKeyForBuiltinBinding(key);
Preconditions.checkNotNull(elementKey);
if (!isMultiBinding) {
generateProvisionMethodIfNeeded(elementKey, referencingClass);
}
MethodSpec.Builder builderForGet = MethodSpec.methodBuilder("get").returns(elementKey.getTypeName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
builderForGet.addStatement("$T var = null", elementKey.getTypeName());
if (isLazy) {
builderForGet.beginControlFlow("if (var == null)");
}
if (!isMultiBinding) {
builderForGet.addStatement("var = $N()", getProvisionMethodName(elementKey));
} else {
addNewStatementToMethodSpec(scope, newDependencyInfo, leafInjectorClassName, builderForGet, "var");
}
if (isLazy) {
builderForGet.endControlFlow();
}
builderForGet.addStatement("return var");
TypeSpec result = TypeSpec.anonymousClassBuilder("").addSuperinterface(key.getTypeName()).addMethod(builderForGet.build()).build();
return result;
}
Aggregations