use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class NewDependencyCollector method collectionToMultimap.
/**
* NOTE: the key of the returned map is of the raw type if the related
* {@link NewDependencyInfo#getDependant()} is for a generic class.
*/
public static SetMultimap<NewBindingKey, NewDependencyInfo> collectionToMultimap(Collection<NewDependencyInfo> dependencies) {
SetMultimap<NewBindingKey, NewDependencyInfo> result = HashMultimap.create();
for (NewDependencyInfo info : dependencies) {
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());
}
}
result.put(key, info);
}
return result;
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class NewInjectorGenerator method generateMapTypeProvisionMethodForPackage.
// If dependencyInfo is not null, then it is a contributor to map biding.
private void generateMapTypeProvisionMethodForPackage(final NewBindingKey key, Set<NewDependencyInfo> dependencyInfos, String suffix) {
Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
NewDependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
TypeName returnType = key.getTypeName();
ClassName injectorClassName = getPackagedInjectorForNewDependencyInfo(key, dependencyInfo);
TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getProvisionMethodName(key) + suffix);
methodSpecBuilder.addModifiers(Modifier.PUBLIC).returns(returnType);
methodSpecBuilder.addStatement("$T result = new $T<>()", returnType, HashMap.class);
TypeName mapKeyType = ((ParameterizedTypeName) returnType).typeArguments.get(0);
TypeName mapValueType = ((ParameterizedTypeName) returnType).typeArguments.get(1);
NewBindingKey mapValueKey = NewBindingKey.get(mapValueType);
methodSpecBuilder.addStatement("$T mapKey", mapKeyType);
methodSpecBuilder.addStatement("$T mapValue", mapValueType);
for (NewDependencyInfo di : dependencyInfos) {
AnnotationMirror mapKeyMirror = Utils.getAnnotationMirrorWithMetaAnnotation(di.getProvisionMethodElement(), MapKey.class);
AnnotationValue unwrapValueAnnotationValue = Utils.getAnnotationValue(mapKeyMirror, "unwrapValue");
if (unwrapValueAnnotationValue != null && !((boolean) unwrapValueAnnotationValue.getValue())) {
errors.add(String.format("unwrapValue = false not supported yet. Consider using set binding."));
return;
}
Object mapKey = Utils.getAnnotationValue(mapKeyMirror, "value").getValue();
methodSpecBuilder.addStatement("mapKey = ($T) $S", mapKeyType, mapKey);
if (Utils.isMapWithBuiltinValueType(key)) {
TypeElement scope = scopeCalculator.calculate(key);
methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(injectorClassName, mapValueKey, scope, di));
} else {
addNewStatementToMethodSpec(mapValueKey, di, injectorClassName, methodSpecBuilder, "mapValue");
}
methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
}
methodSpecBuilder.addStatement("return result");
componentSpecBuilder.addMethod(methodSpecBuilder.build());
// messager.printMessage(Kind.NOTE, String.format(
// "generateSetTypeProvisionMethodFromModule: \n key: %s, \n injector: %s, \n method: %s.",
// key, injectorClassName, methodSpecBuilder.build()));
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class NewInjectorGenerator method getClassFromKey.
// Handles generic case.
private TypeElement getClassFromKey(NewBindingKey key) {
Preconditions.checkArgument(Iterables.getOnlyElement(Utils.getDependencyInfo(dependencies, key)).getProvisionMethodElement() == null, String.format("Key: %s", key));
ClassName className;
TypeName typeName = key.getTypeName();
if (typeName instanceof ClassName) {
className = (ClassName) typeName;
} else {
Preconditions.checkState(typeName instanceof ParameterizedTypeName, String.format("typeName: %s", typeName));
className = ((ParameterizedTypeName) typeName).rawType;
}
String classNameString = Utils.getClassCanonicalName(className);
TypeElement result = processingEnv.getElementUtils().getTypeElement(classNameString);
Preconditions.checkNotNull(result, String.format("Did not find TypeElement for %s", classNameString));
return result;
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class NewInjectorGenerator method generateProvisionMethodForSet.
private void generateProvisionMethodForSet(NewBindingKey key, TypeElement referencingClass, String suffix) {
TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(getInjectorFor(key, referencingClass));
// messager.printMessage(Kind.NOTE, "generateProvisionMethodForSet: " + 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);
methodSpecBuilder.addStatement("$T result = new $T<>()", type, HashSet.class);
methodSpecBuilder.addStatement("$T setVar", type);
methodSpecBuilder.addStatement("$T elementVar", elementType);
SetMultimap<PackageElement, NewDependencyInfo> packageToDependencyInfoMap = HashMultimap.create();
for (NewDependencyInfo dependencyInfo : dependencies.get(key)) {
packageToDependencyInfoMap.put(Utils.getPackage(dependencyInfo.getSourceClassElement()), dependencyInfo);
}
for (PackageElement pkg : packageToDependencyInfoMap.keySet()) {
// messager.printMessage(Kind.NOTE, String.format("generateProvisionMethodForSet for %s from"
// +
// " %s", key, packageToDependencyInfoMap.get(pkg)));
generateSetTypeProvisionMethodForPackage(key, packageToDependencyInfoMap.get(pkg), suffix);
NewDependencyInfo dependencyInfo = Iterables.getFirst(packageToDependencyInfoMap.get(pkg), null);
Preconditions.checkNotNull(dependencyInfo, String.format("no dependencyInfo for set key %s in module %s", key, pkg));
ClassName packagedInjectorClassName = getPackagedInjectorForNewDependencyInfo(key, dependencyInfo);
methodSpecBuilder.addStatement("setVar = $L.$N().$N()", TOP_LEVEL_INJECTOR_FIELD, Utils.getGetMethodName(packagedInjectorClassName), getProvisionMethodName(key));
methodSpecBuilder.addStatement("result.addAll($L)", "setVar");
}
methodSpecBuilder.addStatement("return result");
componentSpecBuilder.addMethod(methodSpecBuilder.build());
}
use of com.squareup.javapoet.ParameterizedTypeName in project tiger by google.
the class Utils method getClassName.
public ClassName getClassName(TypeName typeName) {
ClassName className;
if (typeName instanceof ClassName) {
className = (ClassName) typeName;
} else {
Preconditions.checkState(typeName instanceof ParameterizedTypeName, String.format("typeName: %s", typeName));
className = ((ParameterizedTypeName) typeName).rawType;
}
return className;
}
Aggregations