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);
}
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 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 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 Utils method getSourceCodeName.
/**
* Returns "com_Foo" for com.Foo, "com_Foo_com_Bar_com_Baz" for Foo<Bar, Baz>.
* upper_bounds_UpperBound_Foo for "? extends Foo" and
* lower_bounds_LowerBound_Foo for "? super Foo". Assuming raw types are not
* used, there will be not conflicts.
*/
public static String getSourceCodeName(TypeName typeName) {
Preconditions.checkNotNull(typeName);
if (typeName.isPrimitive()) {
return typeName.toString();
} else if (typeName instanceof ClassName) {
return getClassCanonicalName((ClassName) typeName).replace(".", "_");
} else if (typeName instanceof ParameterizedTypeName) {
ParameterizedTypeName p = (ParameterizedTypeName) typeName;
StringBuilder builder = new StringBuilder(getSourceCodeName(p.rawType));
for (TypeName t : p.typeArguments) {
builder.append("_").append(getSourceCodeName(t));
}
return builder.toString();
} else if (typeName instanceof WildcardTypeName) {
WildcardTypeName w = (WildcardTypeName) typeName;
if (w.upperBounds.size() > 0) {
return "upper_bounds_" + getSourceCodeName(w.upperBounds.get(0));
} else {
Preconditions.checkState(w.lowerBounds.size() > 0);
return "lower_bounds_" + getSourceCodeName(w.lowerBounds.get(0));
}
} else if (typeName instanceof ArrayTypeName) {
ArrayTypeName arrayTypeName = (ArrayTypeName) typeName;
return "ArrayOf" + getSourceCodeName(arrayTypeName.componentType);
}
throw new RuntimeException("Unexpected type: " + typeName);
}
Aggregations