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