use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method generateScopedProvisionMethod.
protected final void generateScopedProvisionMethod(Builder componentSpecBuilder, BindingKey key) {
TypeName returnTypeName = getAccessibleTypeName(key);
MethodSpec.Builder builder = MethodSpec.methodBuilder(getProvisionMethodName(key)).returns(returnTypeName).addModifiers(Modifier.PUBLIC);
builder.addStatement("$T result = $N", returnTypeName.box(), getFieldName(key)).beginControlFlow("if (result == null)").beginControlFlow("synchronized($L)", getLockFieldName(key)).addStatement("result = $N", getFieldName(key)).beginControlFlow("if (result == null)").addStatement("result = $L = $L()", getFieldName(key), getProvisionMethodName(key) + UNSCOPED_SUFFIX).endControlFlow().endControlFlow().endControlFlow().addStatement("return result");
componentSpecBuilder.addMethod(builder.build());
initializeBuilder.addStatement("$L = $L()", getFieldName(key), getProvisionMethodName(key));
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class PackagedHubInterfaceGenerator method generateAbstractInjectionMethod.
private void generateAbstractInjectionMethod(TypeElement cls) {
if (!classesWithInjectionMethodGenerated.add(cls)) {
return;
}
// TODO: this can only handle generic where type variables do not involve DI. A more general
// solution
// is needed.
TypeName typeName = TypeName.get(cls.asType());
if (typeName instanceof ParameterizedTypeName) {
typeName = ((ParameterizedTypeName) typeName).rawType;
}
MethodSpec.Builder builder = MethodSpec.methodBuilder("inject").addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC).addParameter(typeName, "v");
interfaceBuilder.addMethod(builder.build());
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class HubInjectorGenerator4 method createStringProvidingKey.
private String createStringProvidingKey(BindingKey key) {
// logger.n("key: %s", key);
generateProvisionMethodIfNeeded(key);
StringBuilder builder = new StringBuilder();
TypeName typeName = key.getTypeName();
Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
DependencyInfo dependencyInfo = dependencyInfos == null ? null : Preconditions.checkNotNull(Iterables.getFirst(dependencyInfos, null), "DI not found for key: " + key);
if (dependencyInfo != null && dependencyInfo.getDependencySourceType().equals(DependencySourceType.CTOR_INJECTED_CLASS) && typeName instanceof ParameterizedTypeName && !utils.isProviderOrLazy(key) && !utils.isOptional(key)) {
generateGenericInjectorIfNeeded(dependencyInfo);
// generic ctor injected class
builder = new StringBuilder("new ").append(utils.getGenericInjectorName(key)).append("().generate(");
for (BindingKey k : utils.sortBindingKeys(dependencyInfo.getDependencies())) {
builder.append(createStringProvidingKey(k)).append(", ");
}
utils.trimTrailing(builder, ", ");
builder.append(")");
} else {
// types from hub or this
builder.append(getProvisionMethodName(key)).append("()");
}
// logger.n("result: %s", builder.toString());
return builder.toString();
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class HubInjectorGenerator4 method generateGenericInjectorIfNeeded.
private void generateGenericInjectorIfNeeded(DependencyInfo dependencyInfo) {
BindingKey dependant = dependencyInfo.getDependant();
if (!generatedGenericInjectors.add(utils.getGenericInjectorName(dependant))) {
return;
}
Builder genericInjectorBuilder = TypeSpec.classBuilder(utils.getGenericInjectorSimpleName(dependant)).addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", GENERATOR_NAME).build()).addModifiers(Modifier.PUBLIC);
TypeName returnType = dependant.getTypeName();
MethodSpec.Builder generateBuilder = MethodSpec.methodBuilder("generate").addModifiers(Modifier.PUBLIC).returns(returnType);
for (BindingKey key : utils.sortBindingKeys(dependencyInfo.getDependencies())) {
generateBuilder.addParameter(key.getTypeName(), utils.getSourceCodeName(key));
}
DeclaredType type = (DeclaredType) utils.getTypeFromTypeName(returnType);
TypeElement classElement = (TypeElement) type.asElement();
ExecutableElement ctor = Preconditions.checkNotNull(utils.findInjectedCtor(classElement), "injector ctor not found for dI: " + dependencyInfo);
TypeMirror ctorTypeMirror = types.asMemberOf(type, ctor);
List<BindingKey> ctorDependencies = utils.getCtorDependencies(dependencies, dependant);
StringBuilder statementBuilder = new StringBuilder("$T result = new $T(");
for (BindingKey key : ctorDependencies) {
statementBuilder.append(utils.getSourceCodeName(key)).append(", ");
}
utils.trimTrailing(statementBuilder, ", ");
statementBuilder.append(")");
generateBuilder.addStatement(statementBuilder.toString(), returnType, returnType);
for (Element element : classElement.getEnclosedElements()) {
if (element.getKind().equals(ElementKind.FIELD) && utils.isInjected(element)) {
generateBuilder.addStatement("result.$N = $L", element.getSimpleName(), utils.getSourceCodeName(utils.getKeyForField(type, element)));
}
if (element.getKind().equals(ElementKind.METHOD) && utils.isInjected(element)) {
statementBuilder = new StringBuilder("result.$L(");
for (BindingKey key : utils.getDependenciesFromMethod((ExecutableType) types.asMemberOf(type, element), (ExecutableElement) element)) {
statementBuilder.append(utils.getSourceCodeName(key)).append(", ");
}
utils.trimTrailing(statementBuilder, ", ");
statementBuilder.append(")");
generateBuilder.addStatement(statementBuilder.toString(), element.getSimpleName());
}
}
generateBuilder.addStatement("return result");
genericInjectorBuilder.addMethod(generateBuilder.build());
JavaFile javaFile = JavaFile.builder(utils.getPackageString(utils.getClassFromKey(dependant)), genericInjectorBuilder.build()).build();
try {
javaFile.writeTo(processingEnv.getFiler());
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class HubInjectorGenerator4 method generateGetPackagedInjectorMethod.
/**
* generate a getXXX() that returns xxx field, which is a {@link DoubleCheckLazyProvider}.
*/
protected void generateGetPackagedInjectorMethod(String packageString) {
generateGetPackagedInjectorMethodInternal(packageString);
ClassName packagedInjectorClassName = getPackagedInjectorClassName(packageString);
String packagedInjectorSourceName = Utils.getSourceCodeName(packagedInjectorClassName);
TypeName fieldTypeName = ParameterizedTypeName.get(ClassName.get(DoubleCheckLazyProvider.class), packagedInjectorClassName);
generateFieldIfNeeded(fieldTypeName, packagedInjectorSourceName, "$T.create(()->$L())", ClassName.get(DoubleCheckLazyProvider.class), getInternalPackagedInjectorGetterName(packagedInjectorClassName));
injectorBuilder.addMethod(MethodSpec.methodBuilder(utils.getGetMethodName(packagedInjectorClassName)).addModifiers(Modifier.PRIVATE).returns(packagedInjectorClassName).addStatement("return $L.get()", packagedInjectorSourceName).build());
}
Aggregations