use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGeneratorHubClone method createInjectorBuilder.
protected final Builder createInjectorBuilder() {
String injectorSimpleName = getInjectorSimpleName();
logger.n("generated component " + injectorSimpleName);
Builder result = TypeSpec.classBuilder(injectorSimpleName).addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", GENERATOR_NAME).build());
for (TypeName typeName : getSuperInterfaces()) {
result.addSuperinterface(typeName);
}
return result;
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class CoreInjectorGenerator method generateMapTypeProvisionMethodForPackage.
// it is a contributor to map binding.
/**
* TODO: revisit the logic to handle scoped multi bindings.
*/
private void generateMapTypeProvisionMethodForPackage(final BindingKey key, Set<DependencyInfo> dependencyInfos, String suffix) {
messager.printMessage(Kind.NOTE, TAG + ".generateMapTypeProvisionMethodForPackage: key " + key + " di " + dependencyInfos);
Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
TypeElement scope = scopeCalculator.calculate(key);
DependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
TypeName returnType = key.getTypeName();
ClassName injectorClassName = getPackagedInjectorNameForDependencyInfo(key, dependencyInfo);
TypeSpec.Builder packagedInjectorSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(utils.getProvisionMethodName(dependencies, 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);
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())) {
errors.add(String.format("unwrapValue = false not supported yet. Consider using set binding."));
return;
}
AnnotationValue mapKey = utils.getAnnotationValue(elements, mapKeyMirror, "value");
messager.printMessage(Kind.NOTE, TAG + ".generateMapTypeProvisionMethodForPackage: mapKey " + mapKey);
methodSpecBuilder.addStatement("mapKey = ($T) $L", mapKeyType, mapKey);
if (utils.isMapWithBuiltinValueType(key)) {
methodSpecBuilder.addStatement("mapValue = $L", createAnonymousBuiltinTypeForMultiBinding(injectorClassName, mapValueKey, scope, di));
} else {
addNewStatementToMethodSpec(scope, di, injectorClassName, methodSpecBuilder, "mapValue");
}
methodSpecBuilder.addStatement("result.put(mapKey, mapValue)");
}
methodSpecBuilder.addStatement("return result");
packagedInjectorSpecBuilder.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.TypeName in project tiger by google.
the class CoreInjectorGenerator method generateSetTypeProvisionMethodForPackage.
// If dependencyInfo is not null, then it is a contributor to set binding.
private void generateSetTypeProvisionMethodForPackage(BindingKey key, Set<DependencyInfo> dependencyInfos, String suffix) {
messager.printMessage(Kind.NOTE, TAG + ".generateSetTypeProvisionMethodForPackage: key " + key + " dI: " + dependencyInfos);
Preconditions.checkArgument(!dependencyInfos.isEmpty(), String.format("Empty dependencyInfo for key: %s", key));
DependencyInfo dependencyInfo = Iterables.getFirst(dependencyInfos, null);
TypeName returnType = key.getTypeName();
ClassName injectorClassName = getPackagedInjectorNameForDependencyInfo(key, dependencyInfo);
TypeSpec.Builder componentSpecBuilder = getInjectorTypeSpecBuilder(injectorClassName);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(utils.getProvisionMethodName(dependencies, key) + suffix);
methodSpecBuilder.addModifiers(Modifier.PUBLIC).returns(returnType);
methodSpecBuilder.addStatement("$T result = new $T<>()", returnType, HashSet.class);
for (DependencyInfo di : dependencyInfos) {
if (utils.isMultibindsMethod(di.getProvisionMethodElement())) {
continue;
}
methodSpecBuilder.beginControlFlow("");
Preconditions.checkNotNull(di.getProvisionMethodElement());
TypeName contributorType = TypeName.get(di.getProvisionMethodElement().getReturnType());
methodSpecBuilder.addStatement("$T contributor", contributorType);
addNewStatementToMethodSpec(key, di, injectorClassName, methodSpecBuilder, "contributor");
if (di.getType().equals(SET)) {
methodSpecBuilder.addStatement("result.add(contributor)");
} else {
Preconditions.checkState(di.getType().equals(SET_VALUES));
methodSpecBuilder.addStatement("result.addAll(contributor)");
}
methodSpecBuilder.endControlFlow();
}
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.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method createInjectorBuilder.
protected final Builder createInjectorBuilder() {
String injectorSimpleName = getInjectorSimpleName();
logger.n("generated component " + injectorSimpleName);
Builder result = TypeSpec.classBuilder(injectorSimpleName).addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(Generated.class).addMember("value", "$S", GENERATOR_NAME).build());
for (TypeName typeName : getSuperInterfaces()) {
result.addSuperinterface(typeName);
}
return result;
}
use of com.squareup.javapoet.TypeName in project tiger by google.
the class GeneralInjectorGenerator4 method createAnonymousBuiltinType.
/**
* Generate for either unique a binding or a contributor to a multi-binding. DependencyInfo is
* null for unique one and non-null or multi-binding one. ReferencingClass is the opposite. For
* multi-binding, referencing class is the module in dependency. Scope is null for unique binding.
*/
protected final TypeSpec createAnonymousBuiltinType(BindingKey key, @Nullable DependencyInfo dependencyInfo) {
Preconditions.checkArgument(key.getTypeName() instanceof ParameterizedTypeName);
boolean isMultiBinding = dependencyInfo != null;
TypeName rawTypeName = ((ParameterizedTypeName) key.getTypeName()).rawType;
Preconditions.checkArgument(utils.isProviderOrLazy(key), String.format("Built-in binding expected(Provider or Lazy), but get %s", key));
boolean isLazy = rawTypeName.equals(ClassName.get(Lazy.class));
BindingKey elementKey = utils.getElementKeyForParameterizedBinding(key);
Preconditions.checkNotNull(elementKey);
if (!isMultiBinding) {
String provisionMethodName = getProvisionMethodName(elementKey);
// utils.generateDebugInfoMethod(injectorBuilder,
// "before" + provisionMethodName + generatedBindings.contains(provisionMethodName));
generateProvisionMethodIfNeeded(elementKey);
}
// TODO: multi-threading.
MethodSpec.Builder builderForGet = MethodSpec.methodBuilder("get").returns(elementKey.getTypeName()).addAnnotation(Override.class).addModifiers(Modifier.SYNCHRONIZED).addModifiers(Modifier.PUBLIC);
if (isLazy) {
builderForGet.beginControlFlow("if (var == null)");
}
if (!isMultiBinding) {
Set<DependencyInfo> dIs = Utils.getDependencyInfosHandlingBox(dependencies, elementKey);
if (dIs != null) {
elementKey = Preconditions.checkNotNull(Iterables.getFirst(dIs, null), "key: " + elementKey + " dI: " + dIs).getDependant();
}
builderForGet.addStatement("var = $N()", getProvisionMethodName(elementKey));
} else {
/**
* TODO: revisit the logic here, current, for Provide, Lazy and Optional, the key != {@link
* DependencyInfo#getDependant()}.
*/
addNewStatementToMethodSpec(builderForGet, dependencyInfo, "var");
}
if (isLazy) {
builderForGet.endControlFlow();
}
builderForGet.addStatement("return var");
return TypeSpec.anonymousClassBuilder("").addSuperinterface(key.getTypeName()).addField(elementKey.getTypeName(), "var", Modifier.PRIVATE).addMethod(builderForGet.build()).build();
}
Aggregations