use of com.squareup.javapoet.ParameterizedTypeName 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.ParameterizedTypeName 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.ParameterizedTypeName 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.ParameterizedTypeName in project tiger by google.
the class NewDependencyCollector method addDependenciesForRequiredKeys.
/**
* Add dependencies from ctor injected classes needed by requiredKeys recursively to result.
*/
private void addDependenciesForRequiredKeys(SetMultimap<NewBindingKey, NewDependencyInfo> result, Set<NewBindingKey> requiredKeys) {
// Added all the required dependencies from ctor injected classes.
while (!requiredKeys.isEmpty()) {
NewBindingKey key = Iterables.getFirst(requiredKeys, null);
Preconditions.checkNotNull(key);
requiredKeys.remove(key);
if (result.containsKey(key)) {
continue;
}
TypeName typeName = key.getTypeName();
if (Utils.hasBuiltinBinding(typeName)) {
key = Utils.getElementKeyForBuiltinBinding(key);
requiredKeys.add(key);
continue;
}
if (Utils.isMapWithBuiltinValueType(key)) {
NewBindingKey peeledMapKey = Preconditions.checkNotNull(Utils.peelMapWithBuiltinValue(key));
requiredKeys.add(peeledMapKey);
continue;
}
ClassName className;
if (typeName instanceof ClassName) {
className = (ClassName) typeName;
} else {
Preconditions.checkState(typeName instanceof ParameterizedTypeName, "Expect a %s but get %s", ParameterizedTypeName.class, typeName);
ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
for (TypeName parameter : parameterizedTypeName.typeArguments) {
Preconditions.checkState(parameter instanceof ClassName || parameter instanceof ParameterizedTypeName, String.format("Unexpected parameter type %s for type %s.", parameter, typeName));
}
NewDependencyInfo dependencyInfo = Utils.getDependencyInfoByGeneric(result, key);
if (dependencyInfo != null) {
requiredKeys.addAll(dependencyInfo.getDependencies());
continue;
} else {
className = ((ParameterizedTypeName) typeName).rawType;
}
}
TypeElement classTypeElement = env.getElementUtils().getTypeElement(Utils.getClassCanonicalName(className));
Preconditions.checkNotNull(classTypeElement, String.format("Class %s not found.", className));
Collection<NewDependencyInfo> dependencies = collectFromCtorInjectedClass(classTypeElement);
if (dependencies == null) {
messager.printMessage(Kind.ERROR, String.format("Binding not found for %s", key));
continue;
}
NewDependencyInfo 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 {
requiredKeys.addAll(dependency.getDependencies());
}
checkOneDependency(result, dependency);
addDependencyInfo(result, dependency);
}
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class NewDependencyCollector method addDependencyInfo.
/**
* Adds the give {@link NewDependencyInfo} to the map, handling generic type with formal
* parameters. Returns if it changed the given map.
*/
private boolean addDependencyInfo(SetMultimap<NewBindingKey, NewDependencyInfo> existingDependencies, NewDependencyInfo info) {
NewBindingKey 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;
TypeName anyParameter = Preconditions.checkNotNull(Iterables.getFirst(parameterizedTypeName.typeArguments, null), String.format("ParameterizedTypeName of %s has no parameter.", key));
if (anyParameter instanceof TypeVariableName) {
typeName = parameterizedTypeName.rawType;
key = NewBindingKey.get(typeName, key.getQualifier());
}
}
return existingDependencies.put(key, info);
}
Aggregations