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 androidx.room.compiler.processing.XElement; 26 import androidx.room.compiler.processing.XFiler; 27 import androidx.room.compiler.processing.XProcessingEnv; 28 import com.google.common.collect.ImmutableList; 29 import com.squareup.javapoet.FieldSpec; 30 import com.squareup.javapoet.TypeSpec; 31 import dagger.internal.codegen.base.SourceFileGenerator; 32 import dagger.internal.codegen.binding.ContributionBinding; 33 import dagger.internal.codegen.binding.MapKeys; 34 import dagger.internal.codegen.javapoet.TypeNames; 35 import dagger.internal.codegen.model.DaggerAnnotation; 36 import javax.inject.Inject; 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 XProcessingEnv processingEnv; 45 46 @Inject InaccessibleMapKeyProxyGenerator(XProcessingEnv processingEnv, XFiler filer)47 InaccessibleMapKeyProxyGenerator(XProcessingEnv processingEnv, XFiler filer) { 48 super(filer, processingEnv); 49 this.processingEnv = processingEnv; 50 } 51 52 @Override originatingElement(ContributionBinding binding)53 public XElement originatingElement(ContributionBinding binding) { 54 // a map key is only ever present on bindings that have a binding element 55 return binding.bindingElement().get(); 56 } 57 58 @Override topLevelTypes(ContributionBinding binding)59 public ImmutableList<TypeSpec.Builder> topLevelTypes(ContributionBinding binding) { 60 return MapKeys.mapKeyFactoryMethod(binding, processingEnv) 61 .map( 62 method -> { 63 TypeSpec.Builder builder = 64 classBuilder(MapKeys.mapKeyProxyClassName(binding)) 65 .addModifiers(PUBLIC, FINAL) 66 .addMethod(constructorBuilder().addModifiers(PRIVATE).build()) 67 .addMethod(method); 68 // In proguard, we need to keep the classes referenced by @LazyClassKey, we do that by 69 // generating a field referencing the type, and then applying @KeepFieldType to the 70 // field. Here, we generate the field in the proxy class. For classes that are 71 // accessible from the dagger component, we generate fields in LazyClassKeyProvider. 72 // Note: the generated field should not be initialized to avoid class loading. 73 binding 74 .mapKey() 75 .map(DaggerAnnotation::xprocessing) 76 .filter( 77 mapKey -> 78 mapKey.getTypeElement().getClassName().equals(TypeNames.LAZY_CLASS_KEY)) 79 .map( 80 mapKey -> 81 FieldSpec.builder(mapKey.getAsType("value").getTypeName(), "className") 82 .addAnnotation(TypeNames.KEEP_FIELD_TYPE) 83 .build()) 84 .ifPresent(builder::addField); 85 return builder; 86 }) 87 .map(ImmutableList::of) 88 .orElse(ImmutableList.of()); 89 } 90 } 91