use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method generateMapContributors.
protected final void generateMapContributors(BindingKey key, ParameterizedTypeName returnType, MethodSpec.Builder methodSpecBuilder) {
Set<DependencyInfo> dependencyInfos = utils.getDependencyInfo(dependencies, key);
// TODO: remove this hack
if (dependencyInfos == null) {
dependencyInfos = new HashSet<>();
logger.w("no dI for key: " + key);
}
Preconditions.checkNotNull(dependencyInfos, String.format("dependencyInfo not found for key: %s", key));
TypeName mapKeyType = returnType.typeArguments.get(0);
TypeName mapValueType = returnType.typeArguments.get(1);
BindingKey mapValueKey = BindingKey.get(mapValueType);
methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
methodSpecBuilder.addStatement("$T mapValue", mapValueType);
for (DependencyInfo di : dependencyInfos) {
if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
continue;
}
AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
logger.e("unwrapValue = false not supported yet. Consider using set binding.");
return;
}
AnnotationValue mapKey = Utils.getAnnotationValue(elements, mapKeyMirror, "value");
logger.l(Kind.NOTE, "mapKey: %s", mapKey.toString());
methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
if (utils.isMapWithBuiltinValueType(key)) {
methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(mapValueKey, di));
} else {
addNewStatementToMethodSpec(methodSpecBuilder, di, "mapValue");
}
methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
}
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method generateCtor.
private void generateCtor() {
// Ctor
MethodSpec.Builder ctorBuilder = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
// ctorBuilder.addStatement(
// "$T.out.printf($S, $L)", ClassName.get(System.class), "This is tiger: %s\n", "this");
// TODO: maybe remove this.
// Ctor - ancester top level injectors.
// CoreInjectorInfo tmp = coreInjectorInfo;
// while (componentTree.get(tmp) != null) {
// tmp = componentTree.get(tmp);
// ClassName className = ClassName.get(topLevelPackageString,
// getTopLevelInjectorName(tmp, topLevelInjectorPrefix, topLevelInjectorSuffix));
// String sourceCodeName = utils.getSourceCodeName(className);
// injectorBuilder.addField(className, sourceCodeName);
// if (tmp.equals(componentTree.get(coreInjectorInfo))) {
// ctorBuilder.addStatement(
// "this.$L = $L", sourceCodeName, containingInjectorName);
// } else {
// ctorBuilder.addStatement(
// "this.$L = $L.$L", sourceCodeName, containingInjectorName, sourceCodeName);
// }
// }
/**
* Ctor - Component dependencies, @BindsInstance and Passed modules. All sorted together so that
* it is easier to implement {@link #generateGetSubcomponentMethod(ExecutableElement, Builder)}
*/
Set<BindingKey> allParameters = getAllCtorParameters();
for (BindingKey key : utils.sortBindingKeys(allParameters)) {
TypeName typeName = key.getTypeName();
String sourceCodeName = utils.getSourceCodeName(key);
generateFieldIfNeeded(typeName, sourceCodeName);
ctorBuilder.addParameter(typeName, sourceCodeName).addStatement("this.$L = $L", sourceCodeName, sourceCodeName);
}
// TODO: handle ApplicationCompoent in a consistent way.
if (!Sets.newHashSet("DaggerApplicationComponent", "DaggerRootApplicationComponent", "DaggerPlatformApplicationComponentImpl").contains(getInjectorSimpleName())) {
// ctorBuilder.addStatement("$L()", INITIALIZE_METHOD_NAME);
}
injectorBuilder.addMethod(ctorBuilder.build());
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method generateProvisionMethodFromClass.
/**
* Generic is handled.
*/
protected final void generateProvisionMethodFromClass(BindingKey key, String suffix) {
// logger.n("key: " + key + " referencingClass: " +
// referencingClass);
TypeElement cls = utils.getClassFromKey(key);
DeclaredType clsType = (DeclaredType) utils.getTypeFromKey(key);
ExecutableElement ctor = utils.findInjectedCtor(cls);
Preconditions.checkNotNull(ctor, String.format("Did not find ctor for %s", cls));
ExecutableType ctorType = (ExecutableType) types.asMemberOf(clsType, ctor);
List<BindingKey> dependencyKeys = utils.getDependenciesFromMethod(ctorType, ctor);
// TODO: clean this.
// if (key.getTypeName() instanceof ParameterizedTypeName) {
// logger.n("be here :" + key);
// List<BindingKey> specializedKeys = new ArrayList<>();
// Map<TypeVariableName, TypeName> map =
// utils.getMapFromTypeVariableToSpecialized((ParameterizedTypeName) key.getTypeName(),
// (ParameterizedTypeName) TypeName.get(cls.asType()));
// for (BindingKey k : dependencyKeys) {
// specializedKeys.add(utils.specializeIfNeeded(k, map));
// }
// dependencyKeys = specializedKeys;
// }
// logger.n("dependencyKeys: " +
// dependencyKeys);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
TypeName returnTypeName = getAccessibleTypeName(key);
methodSpecBuilder.addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(returnTypeName);
onProvisionMethodStart(methodSpecBuilder, key);
methodSpecBuilder.addStatement("$T result", returnTypeName);
addNewStatementToMethodSpec(methodSpecBuilder, Iterables.getOnlyElement(dependencies.get(key)), "result");
if (shouldInjectAfterCreation() && !utils.isGenericNotSpecialized(cls.asType()) && utils.hasInjectedFieldsOrMethodsRecursively(cls, processingEnv)) {
// logger.n("hasInjected");
generateInjectionMethod(utils.getClassFromKey(key), Utils.getInjectionMethodName(key));
methodSpecBuilder.addStatement("$L(result)", Utils.getInjectionMethodName(key));
}
methodSpecBuilder.addStatement("return result");
onProvisionMethodEnd(methodSpecBuilder, key);
injectorBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method generateProvisionMethodForProviderOrLazy.
/**
* For key like javax.inject.Provider<Foo> and dagger.Lazy<Foo>. Qualifier, if presented, will
* also apply to element binding.
*/
protected final void generateProvisionMethodForProviderOrLazy(BindingKey key, String suffix) {
// logger.n(String.format(
// "generateProvisionMethodForProviderOrLazy: key %s, referencingClass: %s, suffix : %s.", key,
// referencingClass, suffix));
// TypeSpec anonymousTypeSpec = createAnonymousBuiltinTypeForUniqueBinding(key);
BindingKey elementKey = utils.getElementKeyForParameterizedBinding(key);
generateProvisionMethodIfNeeded(elementKey);
TypeName returnTypeName = getAccessibleTypeName(key);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(returnTypeName);
onProvisionMethodStart(methodSpecBuilder, key);
// methodSpecBuilder.addStatement("$T result = $L", key.getTypeName(), anonymousTypeSpec);
methodSpecBuilder.addStatement("$T result = $T.create(()->$L())", returnTypeName, ClassName.get(DoubleCheckLazyProvider.class), getProvisionMethodName(elementKey));
methodSpecBuilder.addStatement("return result");
onProvisionMethodEnd(methodSpecBuilder, key);
injectorBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method generateStringCallingProxyInjectionMethod.
protected final String generateStringCallingProxyInjectionMethod(TypeName typeName, String target) {
typeName = utils.getClassName(typeName);
StringBuilder builder = new StringBuilder();
builder.append(generateGetProxyMethodAndReturnCallingString(utils.getPackageString(utils.getTypeElement(typeName)))).append(".").append(Utils.getInjectionMethodName(BindingKey.get(typeName))).append("(").append(target).append(", ");
// target
TypeName targetTypeName = utils.isPublicRecurively(typeName) ? typeName : objectClassName;
TypeElement c = utils.getTypeElement(typeName);
// fields
for (VariableElement field : utils.getSortedInjectedFields(c, processingEnv)) {
BindingKey key = BindingKey.get(field.asType(), Utils.getQualifier(field));
generateProvisionMethodAndAppendAsParameter(key, builder);
}
// methods
for (ExecutableElement method : utils.getSortedInjectedMethods(c, processingEnv)) {
for (VariableElement injectedMethodParameter : method.getParameters()) {
BindingKey key = BindingKey.get(injectedMethodParameter.asType(), Utils.getQualifier(injectedMethodParameter));
generateProvisionMethodAndAppendAsParameter(key, builder);
}
}
builder.delete(builder.length() - 2, builder.length());
builder.append(")");
return builder.toString();
}
Aggregations