1 /* 2 * Copyright (C) 2021 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.internal.codegen.writing; 18 19 import dagger.internal.codegen.binding.ComponentRequirement; 20 import dagger.internal.codegen.binding.ContributionBinding; 21 import dagger.internal.codegen.binding.ProvisionBinding; 22 import dagger.spi.model.RequestKind; 23 import javax.inject.Inject; 24 25 /** 26 * A factory for creating a binding expression for an unscoped instance. 27 * 28 * <p>Note that these binding expressions are for getting "direct" instances -- i.e. instances that 29 * are created via constructors or modules (e.g. {@code new Foo()} or {@code 30 * FooModule.provideFoo()}) as opposed to an instance created from calling a getter on a framework 31 * type (e.g. {@code fooProvider.get()}). See {@link FrameworkInstanceRequestRepresentation} for 32 * binding expressions that are created from framework types. 33 */ 34 final class UnscopedDirectInstanceRequestRepresentationFactory { 35 private final AssistedFactoryRequestRepresentation.Factory 36 assistedFactoryRequestRepresentationFactory; 37 private final ComponentInstanceRequestRepresentation.Factory 38 componentInstanceRequestRepresentationFactory; 39 private final ComponentProvisionRequestRepresentation.Factory 40 componentProvisionRequestRepresentationFactory; 41 private final ComponentRequirementRequestRepresentation.Factory 42 componentRequirementRequestRepresentationFactory; 43 private final DelegateRequestRepresentation.Factory delegateRequestRepresentationFactory; 44 private final MapRequestRepresentation.Factory mapRequestRepresentationFactory; 45 private final OptionalRequestRepresentation.Factory optionalRequestRepresentationFactory; 46 private final SetRequestRepresentation.Factory setRequestRepresentationFactory; 47 private final SimpleMethodRequestRepresentation.Factory simpleMethodRequestRepresentationFactory; 48 private final SubcomponentCreatorRequestRepresentation.Factory 49 subcomponentCreatorRequestRepresentationFactory; 50 51 @Inject UnscopedDirectInstanceRequestRepresentationFactory( ComponentImplementation componentImplementation, AssistedFactoryRequestRepresentation.Factory assistedFactoryRequestRepresentationFactory, ComponentInstanceRequestRepresentation.Factory componentInstanceRequestRepresentationFactory, ComponentProvisionRequestRepresentation.Factory componentProvisionRequestRepresentationFactory, ComponentRequirementRequestRepresentation.Factory componentRequirementRequestRepresentationFactory, DelegateRequestRepresentation.Factory delegateRequestRepresentationFactory, MapRequestRepresentation.Factory mapRequestRepresentationFactory, OptionalRequestRepresentation.Factory optionalRequestRepresentationFactory, SetRequestRepresentation.Factory setRequestRepresentationFactory, SimpleMethodRequestRepresentation.Factory simpleMethodRequestRepresentationFactory, SubcomponentCreatorRequestRepresentation.Factory subcomponentCreatorRequestRepresentationFactory)52 UnscopedDirectInstanceRequestRepresentationFactory( 53 ComponentImplementation componentImplementation, 54 AssistedFactoryRequestRepresentation.Factory assistedFactoryRequestRepresentationFactory, 55 ComponentInstanceRequestRepresentation.Factory componentInstanceRequestRepresentationFactory, 56 ComponentProvisionRequestRepresentation.Factory 57 componentProvisionRequestRepresentationFactory, 58 ComponentRequirementRequestRepresentation.Factory 59 componentRequirementRequestRepresentationFactory, 60 DelegateRequestRepresentation.Factory delegateRequestRepresentationFactory, 61 MapRequestRepresentation.Factory mapRequestRepresentationFactory, 62 OptionalRequestRepresentation.Factory optionalRequestRepresentationFactory, 63 SetRequestRepresentation.Factory setRequestRepresentationFactory, 64 SimpleMethodRequestRepresentation.Factory simpleMethodRequestRepresentationFactory, 65 SubcomponentCreatorRequestRepresentation.Factory 66 subcomponentCreatorRequestRepresentationFactory) { 67 this.assistedFactoryRequestRepresentationFactory = assistedFactoryRequestRepresentationFactory; 68 this.componentInstanceRequestRepresentationFactory = 69 componentInstanceRequestRepresentationFactory; 70 this.componentProvisionRequestRepresentationFactory = 71 componentProvisionRequestRepresentationFactory; 72 this.componentRequirementRequestRepresentationFactory = 73 componentRequirementRequestRepresentationFactory; 74 this.delegateRequestRepresentationFactory = delegateRequestRepresentationFactory; 75 this.mapRequestRepresentationFactory = mapRequestRepresentationFactory; 76 this.optionalRequestRepresentationFactory = optionalRequestRepresentationFactory; 77 this.setRequestRepresentationFactory = setRequestRepresentationFactory; 78 this.simpleMethodRequestRepresentationFactory = simpleMethodRequestRepresentationFactory; 79 this.subcomponentCreatorRequestRepresentationFactory = 80 subcomponentCreatorRequestRepresentationFactory; 81 } 82 83 /** Returns a direct, unscoped binding expression for a {@link RequestKind#INSTANCE} request. */ create(ContributionBinding binding)84 RequestRepresentation create(ContributionBinding binding) { 85 switch (binding.kind()) { 86 case DELEGATE: 87 return delegateRequestRepresentationFactory.create(binding, RequestKind.INSTANCE); 88 89 case COMPONENT: 90 return componentInstanceRequestRepresentationFactory.create(binding); 91 92 case COMPONENT_DEPENDENCY: 93 return componentRequirementRequestRepresentationFactory.create( 94 binding, ComponentRequirement.forDependency(binding.key().type().xprocessing())); 95 96 case COMPONENT_PROVISION: 97 return componentProvisionRequestRepresentationFactory.create((ProvisionBinding) binding); 98 99 case SUBCOMPONENT_CREATOR: 100 return subcomponentCreatorRequestRepresentationFactory.create(binding); 101 102 case MULTIBOUND_SET: 103 return setRequestRepresentationFactory.create((ProvisionBinding) binding); 104 105 case MULTIBOUND_MAP: 106 return mapRequestRepresentationFactory.create((ProvisionBinding) binding); 107 108 case OPTIONAL: 109 return optionalRequestRepresentationFactory.create((ProvisionBinding) binding); 110 111 case BOUND_INSTANCE: 112 return componentRequirementRequestRepresentationFactory.create( 113 binding, ComponentRequirement.forBoundInstance(binding)); 114 115 case ASSISTED_FACTORY: 116 return assistedFactoryRequestRepresentationFactory.create((ProvisionBinding) binding); 117 118 case INJECTION: 119 case PROVISION: 120 return simpleMethodRequestRepresentationFactory.create((ProvisionBinding) binding); 121 122 case ASSISTED_INJECTION: 123 case MEMBERS_INJECTOR: 124 case MEMBERS_INJECTION: 125 case COMPONENT_PRODUCTION: 126 case PRODUCTION: 127 // Fall through 128 } 129 throw new AssertionError("Unexpected binding kind: " + binding.kind()); 130 } 131 } 132