• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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;
18 
19 import static com.squareup.javapoet.MethodSpec.constructorBuilder;
20 import static com.squareup.javapoet.TypeSpec.classBuilder;
21 import static javax.lang.model.element.Modifier.FINAL;
22 import static javax.lang.model.element.Modifier.PRIVATE;
23 import static javax.lang.model.element.Modifier.PUBLIC;
24 
25 import com.squareup.javapoet.ClassName;
26 import com.squareup.javapoet.TypeSpec;
27 import dagger.internal.codegen.langmodel.DaggerElements;
28 import dagger.internal.codegen.langmodel.DaggerTypes;
29 import java.util.Optional;
30 import javax.annotation.processing.Filer;
31 import javax.inject.Inject;
32 import javax.lang.model.SourceVersion;
33 import javax.lang.model.element.Element;
34 
35 /**
36  * Generates a class that exposes a non-{@code public} {@link
37  * ContributionBinding#mapKeyAnnotation()} @MapKey} annotation.
38  */
39 final class InaccessibleMapKeyProxyGenerator extends SourceFileGenerator<ContributionBinding> {
40   private final DaggerTypes types;
41   private final DaggerElements elements;
42 
43   @Inject
InaccessibleMapKeyProxyGenerator( Filer filer, DaggerTypes types, DaggerElements elements, SourceVersion sourceVersion)44   InaccessibleMapKeyProxyGenerator(
45       Filer filer, DaggerTypes types, DaggerElements elements, SourceVersion sourceVersion) {
46     super(filer, elements, sourceVersion);
47     this.types = types;
48     this.elements = elements;
49   }
50 
51   @Override
nameGeneratedType(ContributionBinding binding)52   ClassName nameGeneratedType(ContributionBinding binding) {
53     return MapKeys.mapKeyProxyClassName(binding);
54   }
55 
56   @Override
originatingElement(ContributionBinding binding)57   Element originatingElement(ContributionBinding binding) {
58     // a map key is only ever present on bindings that have a binding element
59     return binding.bindingElement().get();
60   }
61 
62   @Override
write(ClassName generatedName, ContributionBinding binding)63   Optional<TypeSpec.Builder> write(ClassName generatedName, ContributionBinding binding) {
64     return MapKeys.mapKeyFactoryMethod(binding, types, elements)
65         .map(
66             method ->
67                 classBuilder(generatedName)
68                     .addModifiers(PUBLIC, FINAL)
69                     .addMethod(constructorBuilder().addModifiers(PRIVATE).build())
70                     .addMethod(method));
71   }
72 }
73