use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class DependencyCollector method fixDependencies.
// For gmm, replace the component dependency method bindings with ctor injector classes if
// possible.
private void fixDependencies(SetMultimap<BindingKey, DependencyInfo> result) {
Set<BindingKey> keys = new HashSet<>();
for (BindingKey key : result.keySet()) {
Set<DependencyInfo> dIs = result.get(key);
DependencyInfo dI = Preconditions.checkNotNull(Iterables.getFirst(dIs, null));
if (dI.getDependencySourceType().equals(DependencySourceType.COMPONENT_DEPENDENCIES_METHOD)) {
Preconditions.checkState(dI.getType().equals(UNIQUE), "expected type for dI: " + dI);
keys.add(key);
}
}
for (BindingKey key : keys) {
DeclaredType declaredType = (DeclaredType) utils.getTypeFromKey(key);
TypeElement typeElement;
if (key.getTypeName() instanceof ParameterizedTypeName) {
typeElement = utils.getTypeElementForClassName(((ParameterizedTypeName) key.getTypeName()).rawType);
} else {
typeElement = utils.getTypeElement(key);
}
Collection<DependencyInfo> newDis = collectFromCtorInjectedClass(typeElement, declaredType);
if (newDis == null) {
continue;
}
Set<DependencyInfo> dIs = result.get(key);
DependencyInfo newDi = Iterables.getOnlyElement(newDis);
logger.n(TAG + ".fixDependencies: old dI " + result.get(key));
dIs.clear();
dIs.add(newDi);
logger.n(TAG + ".fixDependencies: new dI " + result.get(key));
}
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class DependencyCollector method addDependencyInfo.
/**
* Adds the give {@link DependencyInfo} to the map, handling generic type with formal
* parameters. Returns if it changed the given map.
*/
private boolean addDependencyInfo(SetMultimap<BindingKey, DependencyInfo> existingDependencies, DependencyInfo info) {
checkOneDependency(existingDependencies, info);
BindingKey key = info.getDependant();
TypeName typeName = key.getTypeName();
// For generic type with type variable, only keep raw type.
if (typeName instanceof ParameterizedTypeName) {
ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
for (TypeName anyParameter : parameterizedTypeName.typeArguments) {
if (anyParameter instanceof TypeVariableName) {
logger.e("unexpected type with TypeVariable: " + typeName);
// typeName = parameterizedTypeName.rawType;
// key = BindingKey.get(typeName, key.getQualifier());
}
}
}
/**
* TODO: remove this. This hack elimite duplicate UNIQUE deps. It depends on the order deps are
* collected, module->componentDependencies->bindsInstance. Check the warning from {@link
* #checkOneDependency(SetMultimap, DependencyInfo)}.
*/
if (info.getType().equals(UNIQUE) && existingDependencies.containsKey(key)) {
DependencyInfo old = Iterables.getOnlyElement(existingDependencies.get(key));
if (shouldKeep(info, old)) {
logger.w(TAG + ".addDependencyInfo: old info is stronger then the new one. Old: " + old + " new: " + info);
return false;
} else {
existingDependencies.get(key).clear();
}
}
boolean result = existingDependencies.put(key, info);
Preconditions.checkState(result, "failed to add dependency: " + info);
return true;
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class DependencyCollector method completeDependenciesAndRequiredKeys.
private void completeDependenciesAndRequiredKeys(SetMultimap<BindingKey, DependencyInfo> result, Set<BindingKey> requiredKeys, Set<BindingKey> unresolved) {
Collection<DependencyInfo> dependencies;
DependencyInfo dependency;
// Added all the required dependencies from ctor injected classes.
while (!requiredKeys.isEmpty()) {
BindingKey key = Iterables.getFirst(requiredKeys, null);
Preconditions.checkNotNull(key);
requiredKeys.remove(key);
TypeName typeName = key.getTypeName();
DeclaredType type = (DeclaredType) utils.getTypeFromTypeName(typeName);
TypeElement classTypeElement = (TypeElement) type.asElement();
Preconditions.checkNotNull(classTypeElement, String.format("Class %s not found.", type));
// logger.n(TAG + ".addDependenciesForRequiredKeys: typeName " + typeName);
if (result.containsKey(key)) {
continue;
} else if (key.getTypeName().isPrimitive()) {
BindingKey boxed = BindingKey.get(key.getTypeName().box(), key.getQualifier());
if (!result.containsKey(boxed)) {
logger.n(TAG + ".addDependenciesForRequiredKeys: binding not found for key " + key);
}
continue;
} else if (key.getTypeName().isBoxedPrimitive()) {
BindingKey unboxed = BindingKey.get(key.getTypeName().unbox(), key.getQualifier());
if (!result.containsKey(unboxed)) {
logger.n(TAG + ".addDependenciesForRequiredKeys: binding not found for key " + key);
}
continue;
} else if (utils.isOptional(key)) {
if (utils.getDependencyInfo(result, key) == null) {
logger.n(TAG + " key " + key + "'s dependencies not found");
}
continue;
} else if (utils.isProviderOrLazy(typeName)) {
key = utils.getElementKeyForParameterizedBinding(key);
requiredKeys.add(key);
continue;
} else if (utils.isMapWithBuiltinValueType(key)) {
BindingKey peeledMapKey = Preconditions.checkNotNull(utils.peelMapWithBuiltinValue(key));
requiredKeys.add(peeledMapKey);
continue;
} else if (utils.isMap(key)) {
if (getMapContributorKeys(result.keySet(), key).isEmpty()) {
// Not found, must be from parent.
unresolved.add(key);
logger.n("Binding not found for : " + key);
}
continue;
} else if (utils.isDaggerMembersInjector(typeName)) {
BindingKey childKey = utils.getElementKeyForParameterizedBinding(key);
Set<BindingKey> keys = collectRequiredKeysFromInjectedClass(utils.getTypeElement(childKey));
DeclaredType childType = (DeclaredType) utils.getTypeFromTypeName(childKey.getTypeName());
TypeElement childClassTypeElement = (TypeElement) childType.asElement();
// child, to make generation code happy.
// dependency =
// new DependencyInfo(
// DependencySourceType.DAGGER_MEMBERS_INJECTOR,
// childKey,
// Sets.newHashSet(keys),
// childClassTypeElement,
// ProvisionType.UNIQUE);
// addDependencyInfo(result, dependency);
// itself
dependency = new DependencyInfo(DependencySourceType.DAGGER_MEMBERS_INJECTOR, key, Sets.newHashSet(keys), childClassTypeElement, ProvisionType.UNIQUE);
} else // }
if (utils.isEitherComponentBuilder(classTypeElement)) {
dependency = new DependencyInfo(DependencySourceType.EITHER_COMPONENT_BUILDER, key, new HashSet<>(), classTypeElement, ProvisionType.UNIQUE);
} else if (utils.isEitherComponent(classTypeElement)) {
dependency = new DependencyInfo(DependencySourceType.EITHER_COMPONENT, key, new HashSet<>(), classTypeElement, ProvisionType.UNIQUE);
} else {
dependencies = collectFromCtorInjectedClass(classTypeElement, type);
if (dependencies == null) {
unresolved.add(key);
continue;
}
dependency = Iterables.getOnlyElement(dependencies);
}
// if (typeName instanceof ParameterizedTypeName) {
// Map<TypeVariableName, TypeName> parameterMap =
// utils.getMapFromTypeVariableToSpecialized(
// (ParameterizedTypeName) typeName,
// (ParameterizedTypeName) dependency.getDependant().getTypeName());
// requiredKeys.addAll(utils.specializeIfNeeded(dependency.getDependencies(),
// parameterMap));
// } else {
// logger.n(
// TAG + ".addDependenciesForRequiredKeys new keys: " + dependency.getDependencies());
requiredKeys.addAll(dependency.getDependencies());
// }
checkOneDependency(result, dependency);
addDependencyInfo(result, dependency);
}
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class GeneralInjectorGenerator method generateProvisionMethodForSet.
/**
* TODO: support set of builtin types.
*/
protected final void generateProvisionMethodForSet(BindingKey key, String suffix) {
// logger.n("" + key +
// " PackagedInjector: "
// + getInjectorFor(key, referencingClass) + " SpecBuilder: " + componentSpecBuilder);
ParameterizedTypeName type = (ParameterizedTypeName) key.getTypeName();
Preconditions.checkArgument(type.rawType.equals(ClassName.get(Set.class)));
TypeName elementType = Iterables.getOnlyElement(type.typeArguments);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(type);
onProvisionMethodStart(methodSpecBuilder, key);
methodSpecBuilder.addStatement("$T result = new $T<>()", type, HashSet.class);
generateSetContributors(key, methodSpecBuilder);
methodSpecBuilder.addStatement("return result");
onProvisionMethodEnd(methodSpecBuilder, key);
injectorBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class GeneralInjectorGenerator method generateProvisionMethodForMap.
protected final void generateProvisionMethodForMap(final BindingKey key, String suffix) {
// logger.n("" + key +
// " PackagedInjector: "
// + getInjectorFor(key, referencingClass) + " SpecBuilder: " + componentSpecBuilder);
ParameterizedTypeName returnType = (ParameterizedTypeName) key.getTypeName();
Preconditions.checkArgument(returnType.rawType.equals(ClassName.get(Map.class)));
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix).addModifiers(suffix.isEmpty() ? Modifier.PUBLIC : Modifier.PRIVATE).returns(returnType);
onProvisionMethodStart(methodSpecBuilder, key);
methodSpecBuilder.addStatement("$T result = new $T<>()", returnType, HashMap.class);
generateMapContributors(key, returnType, methodSpecBuilder);
methodSpecBuilder.addStatement("return result");
onProvisionMethodEnd(methodSpecBuilder, key);
injectorBuilder.addMethod(methodSpecBuilder.build());
}
Aggregations