• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static dagger.internal.codegen.model.BindingKind.MULTIBOUND_MAP;
20 import static dagger.internal.codegen.model.BindingKind.MULTIBOUND_SET;
21 
22 import dagger.assisted.Assisted;
23 import dagger.assisted.AssistedFactory;
24 import dagger.assisted.AssistedInject;
25 import dagger.internal.codegen.binding.ContributionBinding;
26 
27 /** An object that returns static factory to satisfy framework instance request. */
28 final class StaticFactoryInstanceSupplier implements FrameworkInstanceSupplier {
29   private final FrameworkInstanceSupplier frameworkInstanceSupplier;
30 
31   @AssistedInject
StaticFactoryInstanceSupplier( @ssisted ContributionBinding binding, FrameworkInstanceBindingRepresentation.Factory frameworkInstanceBindingRepresentationFactory)32   StaticFactoryInstanceSupplier(
33       @Assisted ContributionBinding binding,
34       FrameworkInstanceBindingRepresentation.Factory
35           frameworkInstanceBindingRepresentationFactory) {
36     this.frameworkInstanceSupplier = () -> staticFactoryCreation(binding);
37   }
38 
39   @Override
memberSelect()40   public MemberSelect memberSelect() {
41     return frameworkInstanceSupplier.memberSelect();
42   }
43 
44   // TODO(wanyingd): no-op members injector is currently handled in
45   // `MembersInjectorProviderCreationExpression`, we should inline the logic here so we won't create
46   // an extra field for it.
staticFactoryCreation(ContributionBinding binding)47   private MemberSelect staticFactoryCreation(ContributionBinding binding) {
48     switch (binding.kind()) {
49       case MULTIBOUND_MAP:
50         return StaticMemberSelects.emptyMapFactory(binding);
51       case MULTIBOUND_SET:
52         return StaticMemberSelects.emptySetFactory(binding);
53       case PROVISION:
54       case INJECTION:
55         return StaticMemberSelects.factoryCreateNoArgumentMethod(binding);
56       default:
57         throw new AssertionError(String.format("Invalid binding kind: %s", binding.kind()));
58     }
59   }
60 
61   @AssistedFactory
62   static interface Factory {
create(ContributionBinding binding)63     StaticFactoryInstanceSupplier create(ContributionBinding binding);
64   }
65 }
66