@Override Optional<TypeSpec.Builder> write(ClassName generatedTypeName, ProvisionBinding binding) { // We don't want to write out resolved bindings -- we want to write out the generic version. checkState(!binding.unresolved().isPresent()); TypeMirror keyType = binding.contributionType().equals(ContributionType.MAP) ? MapType.from(binding.key().type()).unwrappedValueType(Provider.class) : binding.key().type(); TypeName providedTypeName = TypeName.get(keyType); ParameterizedTypeName parameterizedFactoryName = factoryOf(providedTypeName); Optional<ParameterizedTypeName> factoryOfRawTypeName = Optional.absent(); TypeSpec.Builder factoryBuilder; Optional<MethodSpec.Builder> constructorBuilder = Optional.absent(); ImmutableList<TypeVariableName> typeParameters = bindingTypeElementTypeVariableNames(binding); ImmutableMap<BindingKey, FrameworkField> fields = generateBindingFieldsForDependencies(dependencyRequestMapper, binding); switch (binding.factoryCreationStrategy()) { case ENUM_INSTANCE: factoryBuilder = enumBuilder(generatedTypeName.simpleName()).addEnumConstant("INSTANCE"); // If we have type parameters, then remove the parameters from our providedTypeName, // since we'll be implementing an erased version of it. if (!typeParameters.isEmpty()) { factoryBuilder.addAnnotation(SUPPRESS_WARNINGS_RAWTYPES); // TODO(ronshapiro): instead of reassigning, introduce an optional/second parameter providedTypeName = ((ParameterizedTypeName) providedTypeName).rawType; factoryOfRawTypeName = Optional.of(factoryOf(providedTypeName)); } break; case CLASS_CONSTRUCTOR: factoryBuilder = classBuilder(generatedTypeName.simpleName()) .addTypeVariables(typeParameters) .addModifiers(FINAL); constructorBuilder = Optional.of(constructorBuilder().addModifiers(PUBLIC)); if (binding.bindingKind().equals(PROVISION) && !binding.bindingElement().getModifiers().contains(STATIC)) { addConstructorParameterAndTypeField( TypeName.get(binding.bindingTypeElement().asType()), "module", factoryBuilder, constructorBuilder.get()); } for (FrameworkField bindingField : fields.values()) { addConstructorParameterAndTypeField( bindingField.javapoetFrameworkType(), bindingField.name(), factoryBuilder, constructorBuilder.get()); } break; default: throw new AssertionError(); } factoryBuilder .addModifiers(PUBLIC) .addSuperinterface(factoryOfRawTypeName.or(parameterizedFactoryName)); // If constructing a factory for @Inject or @Provides bindings, we use a static create method // so that generated components can avoid having to refer to the generic types // of the factory. (Otherwise they may have visibility problems referring to the types.) Optional<MethodSpec> createMethod; switch (binding.bindingKind()) { case INJECTION: case PROVISION: // The return type is usually the same as the implementing type, except in the case // of enums with type variables (where we cast). MethodSpec.Builder createMethodBuilder = methodBuilder("create") .addModifiers(PUBLIC, STATIC) .addTypeVariables(typeParameters) .returns(parameterizedFactoryName); List<ParameterSpec> params = constructorBuilder.isPresent() ? constructorBuilder.get().build().parameters : ImmutableList.<ParameterSpec>of(); createMethodBuilder.addParameters(params); switch (binding.factoryCreationStrategy()) { case ENUM_INSTANCE: if (typeParameters.isEmpty()) { createMethodBuilder.addStatement("return INSTANCE"); } else { // We use an unsafe cast here because the types are different. // It's safe because the type is never referenced anywhere. createMethodBuilder.addStatement("return ($T) INSTANCE", TypeNames.FACTORY); createMethodBuilder.addAnnotation(SUPPRESS_WARNINGS_UNCHECKED); } break; case CLASS_CONSTRUCTOR: createMethodBuilder.addStatement( "return new $T($L)", javapoetParameterizedGeneratedTypeNameForBinding(binding), makeParametersCodeBlock(Lists.transform(params, CodeBlocks.PARAMETER_NAME))); break; default: throw new AssertionError(); } createMethod = Optional.of(createMethodBuilder.build()); break; default: createMethod = Optional.absent(); } if (constructorBuilder.isPresent()) { factoryBuilder.addMethod(constructorBuilder.get().build()); } List<CodeBlock> parameters = Lists.newArrayList(); for (DependencyRequest dependency : binding.dependencies()) { parameters.add( frameworkTypeUsageStatement( CodeBlocks.format("$L", fields.get(dependency.bindingKey()).name()), dependency.kind())); } CodeBlock parametersCodeBlock = makeParametersCodeBlock(parameters); MethodSpec.Builder getMethodBuilder = methodBuilder("get") .returns(providedTypeName) .addAnnotation(Override.class) .addModifiers(PUBLIC); if (binding.bindingKind().equals(PROVISION)) { CodeBlock.Builder providesMethodInvocationBuilder = CodeBlock.builder(); if (binding.bindingElement().getModifiers().contains(STATIC)) { providesMethodInvocationBuilder.add("$T", ClassName.get(binding.bindingTypeElement())); } else { providesMethodInvocationBuilder.add("module"); } providesMethodInvocationBuilder.add( ".$L($L)", binding.bindingElement().getSimpleName(), parametersCodeBlock); CodeBlock providesMethodInvocation = providesMethodInvocationBuilder.build(); if (binding.provisionType().equals(SET)) { TypeName paramTypeName = TypeName.get(MoreTypes.asDeclared(keyType).getTypeArguments().get(0)); // TODO(cgruber): only be explicit with the parameter if paramType contains wildcards. getMethodBuilder.addStatement( "return $T.<$T>singleton($L)", Collections.class, paramTypeName, providesMethodInvocation); } else if (binding.nullableType().isPresent() || nullableValidationType.equals(Diagnostic.Kind.WARNING)) { if (binding.nullableType().isPresent()) { getMethodBuilder.addAnnotation((ClassName) TypeName.get(binding.nullableType().get())); } getMethodBuilder.addStatement("return $L", providesMethodInvocation); } else { String failMsg = CANNOT_RETURN_NULL_FROM_NON_NULLABLE_PROVIDES_METHOD; getMethodBuilder .addStatement( "$T provided = $L", getMethodBuilder.build().returnType, providesMethodInvocation) .addCode("if (provided == null) { ") .addStatement("throw new $T($S)", NullPointerException.class, failMsg) .addCode("}") .addStatement("return provided"); } } else if (binding.membersInjectionRequest().isPresent()) { getMethodBuilder.addStatement( "$1T instance = new $1T($2L)", providedTypeName, parametersCodeBlock); getMethodBuilder.addStatement( "$L.injectMembers(instance)", fields.get(binding.membersInjectionRequest().get().bindingKey()).name()); getMethodBuilder.addStatement("return instance"); } else { getMethodBuilder.addStatement("return new $T($L)", providedTypeName, parametersCodeBlock); } factoryBuilder.addMethod(getMethodBuilder.build()); if (createMethod.isPresent()) { factoryBuilder.addMethod(createMethod.get()); } // TODO(gak): write a sensible toString return Optional.of(factoryBuilder); }
private TypeSpec build(ScopeSpec spec) { MethodSpec configureScopeSpec = MethodSpec.methodBuilder("configureScope") .addModifiers(Modifier.PUBLIC) .addAnnotation(Override.class) .addParameter(ClassName.get(MortarScope.Builder.class), "builder") .addParameter(ClassName.get(MortarScope.class), "parentScope") .addCode( CodeBlock.builder() .add( "builder.withService($T.SERVICE_NAME, $T.builder()\n", DAGGERSERVICE_CLS, spec.getDaggerComponentTypeName()) .indent() .add( ".$L(parentScope.<$T>getService($T.SERVICE_NAME))\n", spec.getDaggerComponentBuilderDependencyMethodName(), spec.getDaggerComponentBuilderDependencyTypeName(), DAGGERSERVICE_CLS) .add(".module(new Module())\n") .add(".build());\n") .unindent() .build()) .build(); List<FieldSpec> fieldSpecs = new ArrayList<>(); for (ParameterSpec parameterSpec : spec.getModuleSpec().getInternalParameters()) { fieldSpecs.add(FieldSpec.builder(parameterSpec.type, parameterSpec.name).build()); } MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(ParcelConstructor.class).build()) .addParameters(spec.getModuleSpec().getInternalParameters()); for (ParameterSpec parameterSpec : spec.getModuleSpec().getInternalParameters()) { constructorBuilder.addStatement("this.$L = $L", parameterSpec.name, parameterSpec.name); } TypeSpec.Builder builder = TypeSpec.classBuilder(spec.getClassName().simpleName()) .addModifiers(Modifier.PUBLIC) .addAnnotation( AnnotationSpec.builder(Generated.class) .addMember( "value", "$S", architect.autostack.compiler.AnnotationProcessor.class.getName()) .build()) .addAnnotation(spec.getComponentAnnotationSpec()) .addAnnotation( AnnotationSpec.builder(Parcel.class).addMember("parcelsIndex", "false").build()) .addType(buildModule(spec.getModuleSpec())) .addMethod(constructorBuilder.build()) .addMethod(configureScopeSpec) .addFields(fieldSpecs); if (spec.getScopeAnnotationSpec() != null) { builder.addAnnotation(spec.getScopeAnnotationSpec()); } if (spec.getPathViewTypeName() != null) { builder.addSuperinterface(PATH_CLS); MethodSpec createViewSpec = MethodSpec.methodBuilder("createView") .addModifiers(Modifier.PUBLIC) .returns(spec.getPathViewTypeName()) .addAnnotation(Override.class) .addParameter(CONTEXT_CLS, "context") .addStatement("return new $T(context)", spec.getPathViewTypeName()) .build(); builder.addMethod(createViewSpec); } else { builder.addSuperinterface(STACKABLE_CLS); } return builder.build(); }