diff --git a/java/dagger/internal/codegen/binding/MapKeys.java b/java/dagger/internal/codegen/binding/MapKeys.java index 0537c4cb653..b05e0a6a12b 100644 --- a/java/dagger/internal/codegen/binding/MapKeys.java +++ b/java/dagger/internal/codegen/binding/MapKeys.java @@ -52,8 +52,6 @@ /** Methods for extracting {@link MapKey} annotations and key code blocks from binding elements. */ public final class MapKeys { - public static final String LAZY_CLASS_KEY_NAME_FIELD = "lazyClassKeyName"; - public static final String KEEP_FIELD_TYPE_FIELD = "keepFieldType"; /** * If {@code bindingElement} is annotated with a {@link MapKey} annotation, returns it. @@ -219,15 +217,5 @@ public static boolean useLazyClassKey(Binding binding, BindingGraph graph) { return false; } - public static CodeBlock getLazyClassMapKeyExpression(ContributionBinding contributionBinding) { - ClassName proxyClassName = - lazyClassKeyProxyClassName(XElements.asMethod(contributionBinding.bindingElement().get())); - return CodeBlock.of("$T.$N", proxyClassName, LAZY_CLASS_KEY_NAME_FIELD); - } - - public static ClassName lazyClassKeyProxyClassName(XMethodElement methodElement) { - return elementBasedClassName(methodElement, "_LazyMapKey"); - } - private MapKeys() {} } diff --git a/java/dagger/internal/codegen/processingstep/LazyClassKeyProcessingStep.java b/java/dagger/internal/codegen/processingstep/LazyClassKeyProcessingStep.java index fa57dc3e944..54c406a7981 100644 --- a/java/dagger/internal/codegen/processingstep/LazyClassKeyProcessingStep.java +++ b/java/dagger/internal/codegen/processingstep/LazyClassKeyProcessingStep.java @@ -16,12 +16,10 @@ package dagger.internal.codegen.processingstep; -import static androidx.room.compiler.processing.XElementKt.isTypeElement; import static java.nio.charset.StandardCharsets.UTF_8; import androidx.room.compiler.processing.XElement; import androidx.room.compiler.processing.XFiler; -import androidx.room.compiler.processing.XMethodElement; import androidx.room.compiler.processing.XProcessingEnv; import androidx.room.compiler.processing.XTypeElement; import com.google.common.base.Joiner; @@ -31,7 +29,6 @@ import com.google.common.collect.SetMultimap; import com.squareup.javapoet.ClassName; import dagger.internal.codegen.javapoet.TypeNames; -import dagger.internal.codegen.writing.LazyMapKeyProxyGenerator; import dagger.internal.codegen.xprocessing.XElements; import java.io.BufferedWriter; import java.io.IOException; @@ -47,12 +44,9 @@ final class LazyClassKeyProcessingStep extends TypeCheckingProcessingStep { private static final String PROGUARD_KEEP_RULE = "-keep,allowobfuscation,allowshrinking class "; private final SetMultimap processedElements = LinkedHashMultimap.create(); - private final LazyMapKeyProxyGenerator lazyMapKeyProxyGenerator; @Inject - LazyClassKeyProcessingStep(LazyMapKeyProxyGenerator lazyMapKeyProxyGenerator) { - this.lazyMapKeyProxyGenerator = lazyMapKeyProxyGenerator; - } + LazyClassKeyProcessingStep() {} @Override public ImmutableSet annotationClassNames() { @@ -67,28 +61,8 @@ protected void process(XElement element, ImmutableSet annotations) { .getAsType("value") .getTypeElement() .getClassName(); - // No need to fail, since we want to support customized usage of class key annotations. - // https://github.com/google/dagger/pull/2831 - if (!isMapBinding(element) || !isModuleOrProducerModule(element.getEnclosingElement())) { - return; - } XTypeElement moduleElement = XElements.asTypeElement(element.getEnclosingElement()); processedElements.put(moduleElement.getClassName(), lazyClassKey); - XMethodElement method = XElements.asMethod(element); - lazyMapKeyProxyGenerator.generate(method); - } - - private static boolean isMapBinding(XElement element) { - return element.hasAnnotation(TypeNames.INTO_MAP) - && (element.hasAnnotation(TypeNames.BINDS) - || element.hasAnnotation(TypeNames.PROVIDES) - || element.hasAnnotation(TypeNames.PRODUCES)); - } - - private static boolean isModuleOrProducerModule(XElement element) { - return isTypeElement(element) - && (element.hasAnnotation(TypeNames.MODULE) - || element.hasAnnotation(TypeNames.PRODUCER_MODULE)); } @Override diff --git a/java/dagger/internal/codegen/writing/LazyClassKeyProviders.java b/java/dagger/internal/codegen/writing/LazyClassKeyProviders.java new file mode 100644 index 00000000000..b1a216b8e1b --- /dev/null +++ b/java/dagger/internal/codegen/writing/LazyClassKeyProviders.java @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2024 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.internal.codegen.writing; + +import static dagger.internal.codegen.base.MapKeyAccessibility.isMapKeyAccessibleFrom; +import static javax.lang.model.element.Modifier.FINAL; +import static javax.lang.model.element.Modifier.PRIVATE; +import static javax.lang.model.element.Modifier.STATIC; + +import androidx.room.compiler.processing.XAnnotation; +import com.google.common.base.Preconditions; +import com.squareup.javapoet.ClassName; +import com.squareup.javapoet.CodeBlock; +import com.squareup.javapoet.FieldSpec; +import com.squareup.javapoet.TypeSpec; +import dagger.internal.codegen.base.UniqueNameSet; +import dagger.internal.codegen.javapoet.TypeNames; +import dagger.internal.codegen.model.Key; +import java.util.LinkedHashMap; +import java.util.Map; +import javax.inject.Inject; + +/** + * Keeps track of all providers for DaggerMap keys. + * + *

Generated class looks like below: + * + *

{@code
+ *  @IdentifierNameString
+ *  static final class  LazyClassKeyProvider {
+ *    static final String com_google_foo_Bar = "com.google.foo.Bar";
+ *    @KeepFieldType static final com.google.foo.Bar com_google_foo_Bar2;
+ * }
+ * }
+ */ +public final class LazyClassKeyProviders { + @PerGeneratedFile + static final class LazyClassKeyProviderCache { + // Map key to its corresponding the field reference expression from LazyClassKeyProvider. + final Map mapKeyToProvider = new LinkedHashMap<>(); + private final Map entries = new LinkedHashMap<>(); + private final Map keepClassNamesFields = new LinkedHashMap<>(); + private final UniqueNameSet uniqueFieldNames = new UniqueNameSet(); + private ClassName mapKeyProviderType; + + @Inject + LazyClassKeyProviderCache() {} + + private CodeBlock getMapKeyExpression(Key key) { + Preconditions.checkArgument( + key.multibindingContributionIdentifier().isPresent() + && key.multibindingContributionIdentifier() + .get() + .bindingMethod() + .xprocessing() + .hasAnnotation(TypeNames.LAZY_CLASS_KEY)); + XAnnotation lazyClassKeyAnnotation = getLazyClassKeyAnnotation(key); + ClassName lazyClassKey = getLazyClassKey(lazyClassKeyAnnotation); + if (mapKeyToProvider.containsKey(lazyClassKey)) { + return mapKeyToProvider.get(lazyClassKey); + } + addField(lazyClassKey, lazyClassKeyAnnotation, mapKeyProviderType.packageName()); + mapKeyToProvider.put( + lazyClassKey, CodeBlock.of("$T.$N", mapKeyProviderType, entries.get(lazyClassKey))); + return mapKeyToProvider.get(lazyClassKey); + } + + private ClassName getLazyClassKey(XAnnotation lazyClassKeyAnnotation) { + return lazyClassKeyAnnotation.getAsType("value").getTypeElement().getClassName(); + } + + private XAnnotation getLazyClassKeyAnnotation(Key key) { + return key.multibindingContributionIdentifier() + .get() + .bindingMethod() + .xprocessing() + .getAnnotation(TypeNames.LAZY_CLASS_KEY); + } + + private void addField( + ClassName lazyClassKey, XAnnotation lazyClassKeyAnnotation, String accessingPackage) { + entries.put( + lazyClassKey, + FieldSpec.builder( + TypeNames.STRING, + uniqueFieldNames.getUniqueName(lazyClassKey.canonicalName().replace('.', '_'))) + // TODO(b/217435141): Leave the field as non-final. We will apply + // @IdentifierNameString on the field, which doesn't work well with static final + // fields. + .addModifiers(STATIC) + .initializer("$S", lazyClassKey.reflectionName()) + .build()); + // To be able to apply -includedescriptorclasses rule to keep the class names referenced by + // LazyClassKey, we need to generate fields that uses those classes as type in + // LazyClassKeyProvider. For types that are not accessible from the generated component, we + // generate fields in the proxy class. + // Note: the generated field should not be initialized to avoid class loading. + if (isMapKeyAccessibleFrom(lazyClassKeyAnnotation, accessingPackage)) { + keepClassNamesFields.put( + lazyClassKey, + FieldSpec.builder( + lazyClassKey, + uniqueFieldNames.getUniqueName(lazyClassKey.canonicalName().replace('.', '_'))) + .addAnnotation(TypeNames.KEEP_FIELD_TYPE) + .build()); + } + } + + private TypeSpec build() { + return TypeSpec.classBuilder(mapKeyProviderType) + .addAnnotation(TypeNames.IDENTIFIER_NAME_STRING) + .addModifiers(PRIVATE, STATIC, FINAL) + .addFields(entries.values()) + .addFields(keepClassNamesFields.values()) + .build(); + } + } + + public static final String MAP_KEY_PROVIDER_NAME = "LazyClassKeyProvider"; + private final GeneratedImplementation topLevelImplementation; + private final LazyClassKeyProviderCache cache; + + @Inject + LazyClassKeyProviders( + @TopLevel GeneratedImplementation topLevelImplementation, LazyClassKeyProviderCache cache) { + this.topLevelImplementation = topLevelImplementation; + this.cache = cache; + } + + /** Returns a reference to a field in LazyClassKeyProvider that corresponds to this binding. */ + CodeBlock getMapKeyExpression(Key key) { + // This is for avoid generating empty LazyClassKeyProvider. + if (cache.entries.isEmpty()) { + String name = topLevelImplementation.getUniqueClassName(MAP_KEY_PROVIDER_NAME); + cache.mapKeyProviderType = topLevelImplementation.name().nestedClass(name); + topLevelImplementation.addTypeSupplier(this::build); + } + return cache.getMapKeyExpression(key); + } + + private TypeSpec build() { + return cache.build(); + } +} diff --git a/java/dagger/internal/codegen/writing/LazyMapKeyProxyGenerator.java b/java/dagger/internal/codegen/writing/LazyMapKeyProxyGenerator.java deleted file mode 100644 index 623e13e1439..00000000000 --- a/java/dagger/internal/codegen/writing/LazyMapKeyProxyGenerator.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (C) 2024 The Dagger Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package dagger.internal.codegen.writing; - -import static com.squareup.javapoet.TypeSpec.classBuilder; -import static dagger.internal.codegen.binding.MapKeys.KEEP_FIELD_TYPE_FIELD; -import static dagger.internal.codegen.binding.MapKeys.LAZY_CLASS_KEY_NAME_FIELD; -import static dagger.internal.codegen.binding.MapKeys.lazyClassKeyProxyClassName; -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.CAST; -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.DEPRECATION; -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.KOTLIN_INTERNAL; -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.RAWTYPES; -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.UNCHECKED; -import static javax.lang.model.element.Modifier.FINAL; -import static javax.lang.model.element.Modifier.PUBLIC; -import static javax.lang.model.element.Modifier.STATIC; - -import androidx.room.compiler.processing.XFiler; -import androidx.room.compiler.processing.XMethodElement; -import androidx.room.compiler.processing.XProcessingEnv; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.squareup.javapoet.AnnotationSpec; -import com.squareup.javapoet.ClassName; -import com.squareup.javapoet.FieldSpec; -import com.squareup.javapoet.JavaFile; -import com.squareup.javapoet.TypeSpec; -import dagger.internal.DaggerGenerated; -import dagger.internal.codegen.javapoet.AnnotationSpecs; -import dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression; -import dagger.internal.codegen.javapoet.TypeNames; -import java.util.Optional; -import javax.inject.Inject; - -/** - * Generate a class containing fields that works with proguard rules to support @LazyClassKey - * usages. - */ -public final class LazyMapKeyProxyGenerator { - private static final String GENERATED_COMMENTS = "https://dagger.dev"; - private final XProcessingEnv processingEnv; - private final XFiler filer; - - @Inject - LazyMapKeyProxyGenerator(XProcessingEnv processingEnv, XFiler filer) { - this.processingEnv = processingEnv; - this.filer = filer; - } - - public void generate(XMethodElement element) { - ClassName lazyClassKeyProxyClassName = lazyClassKeyProxyClassName(element); - TypeSpec.Builder typeSpecBuilder = - classBuilder(lazyClassKeyProxyClassName) - .addModifiers(PUBLIC, FINAL) - .addAnnotation(TypeNames.IDENTIFIER_NAME_STRING) - .addAnnotation(DaggerGenerated.class) - .addFields(lazyClassKeyFields(element)); - Optional generatedAnnotation = - Optional.ofNullable(processingEnv.findGeneratedAnnotation()) - .map( - annotation -> - AnnotationSpec.builder(annotation.getClassName()) - .addMember("value", "$S", "dagger.internal.codegen.LazyClassKeyProcessor") - .addMember("comments", "$S", GENERATED_COMMENTS) - .build()); - generatedAnnotation.ifPresent(typeSpecBuilder::addAnnotation); - typeSpecBuilder.addAnnotation( - AnnotationSpecs.suppressWarnings( - ImmutableSet.builder() - .add(UNCHECKED, RAWTYPES, KOTLIN_INTERNAL, CAST, DEPRECATION) - .build())); - - filer.write( - JavaFile.builder(lazyClassKeyProxyClassName.packageName(), typeSpecBuilder.build()).build(), - XFiler.Mode.Isolating); - } - - private static ImmutableList lazyClassKeyFields(XMethodElement element) { - ClassName lazyClassMapKeyClassName = - element - .getAnnotation(TypeNames.LAZY_CLASS_KEY) - .getAsType("value") - .getTypeElement() - .getClassName(); - // Generate a string referencing the map key class name, and dagger will apply - // identifierrnamestring rule to it to make sure it is correctly obfuscated. - FieldSpec lazyClassKeyField = - FieldSpec.builder(TypeNames.STRING, LAZY_CLASS_KEY_NAME_FIELD) - // TODO(b/217435141): Leave the field as non-final. We will apply - // @IdentifierNameString on the field, which doesn't work well with static final - // fields. - .addModifiers(STATIC, PUBLIC) - .initializer("$S", lazyClassMapKeyClassName.reflectionName()) - .build(); - // In proguard, we need to keep the classes referenced by @LazyClassKey, we do that by - // generating a field referencing the type, and then applying @KeepFieldType to the - // field. Here, we generate the field in the proxy class. For classes that are - // accessible from the dagger component, we generate fields in LazyClassKeyProvider. - // Note: the generated field should not be initialized to avoid class loading. - FieldSpec keepFieldTypeField = - FieldSpec.builder(lazyClassMapKeyClassName, KEEP_FIELD_TYPE_FIELD) - .addModifiers(STATIC) - .addAnnotation(TypeNames.KEEP_FIELD_TYPE) - .build(); - return ImmutableList.of(keepFieldTypeField, lazyClassKeyField); - } -} diff --git a/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java b/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java index 892eba963aa..1e186b5cd24 100644 --- a/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java +++ b/java/dagger/internal/codegen/writing/MapFactoryCreationExpression.java @@ -17,7 +17,6 @@ package dagger.internal.codegen.writing; import static com.google.common.base.Preconditions.checkNotNull; -import static dagger.internal.codegen.binding.MapKeys.getLazyClassMapKeyExpression; import static dagger.internal.codegen.binding.MapKeys.getMapKeyExpression; import static dagger.internal.codegen.binding.SourceFiles.mapFactoryClassName; @@ -44,10 +43,12 @@ final class MapFactoryCreationExpression extends MultibindingFactoryCreationExpr private final BindingGraph graph; private final MultiboundMapBinding binding; private final boolean useLazyClassKey; + private final LazyClassKeyProviders lazyClassKeyProviders; @AssistedInject MapFactoryCreationExpression( @Assisted MultiboundMapBinding binding, + LazyClassKeyProviders lazyClassKeyProviders, XProcessingEnv processingEnv, ComponentImplementation componentImplementation, ComponentRequestRepresentations componentRequestRepresentations, @@ -58,6 +59,7 @@ final class MapFactoryCreationExpression extends MultibindingFactoryCreationExpr this.componentImplementation = componentImplementation; this.graph = graph; this.useLazyClassKey = MapKeys.useLazyClassKey(binding, graph); + this.lazyClassKeyProviders = lazyClassKeyProviders; } @Override @@ -81,7 +83,7 @@ public CodeBlock creationExpression() { builder.add( ".put($L, $L)", useLazyClassKey - ? getLazyClassMapKeyExpression(graph.contributionBinding(dependency.key())) + ? lazyClassKeyProviders.getMapKeyExpression(dependency.key()) : getMapKeyExpression( contributionBinding, componentImplementation.name(), processingEnv), multibindingDependencyExpression(dependency)); diff --git a/java/dagger/internal/codegen/writing/MapRequestRepresentation.java b/java/dagger/internal/codegen/writing/MapRequestRepresentation.java index a870f669f0a..ae11ea8c279 100644 --- a/java/dagger/internal/codegen/writing/MapRequestRepresentation.java +++ b/java/dagger/internal/codegen/writing/MapRequestRepresentation.java @@ -19,7 +19,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Iterables.getOnlyElement; import static dagger.internal.codegen.binding.BindingRequest.bindingRequest; -import static dagger.internal.codegen.binding.MapKeys.getLazyClassMapKeyExpression; import static dagger.internal.codegen.binding.MapKeys.getMapKeyExpression; import static dagger.internal.codegen.javapoet.CodeBlocks.toParametersCodeBlock; import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom; @@ -57,10 +56,12 @@ final class MapRequestRepresentation extends RequestRepresentation { private final ImmutableMap dependencies; private final ComponentRequestRepresentations componentRequestRepresentations; private final boolean useLazyClassKey; + private final LazyClassKeyProviders lazyClassKeyProviders; @AssistedInject MapRequestRepresentation( @Assisted MultiboundMapBinding binding, + LazyClassKeyProviders lazyClassKeyProviders, XProcessingEnv processingEnv, BindingGraph graph, ComponentImplementation componentImplementation, @@ -73,6 +74,7 @@ final class MapRequestRepresentation extends RequestRepresentation { this.dependencies = Maps.toMap(binding.dependencies(), dep -> graph.contributionBinding(dep.key())); this.useLazyClassKey = MapKeys.useLazyClassKey(binding, graph); + this.lazyClassKeyProviders = lazyClassKeyProviders; } @Override @@ -151,7 +153,7 @@ private CodeBlock keyAndValueExpression(DependencyRequest dependency, ClassName return CodeBlock.of( "$L, $L", useLazyClassKey - ? getLazyClassMapKeyExpression(dependencies.get(dependency)) + ? lazyClassKeyProviders.getMapKeyExpression(dependency.key()) : getMapKeyExpression(dependencies.get(dependency), requestingClass, processingEnv), componentRequestRepresentations .getDependencyExpression(bindingRequest(dependency), requestingClass) diff --git a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_DEFAULT_MODE_test.DaggerTestComponent b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_DEFAULT_MODE_test.DaggerTestComponent index a3faffc55c7..2d8e6fec35b 100644 --- a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_DEFAULT_MODE_test.DaggerTestComponent +++ b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_DEFAULT_MODE_test.DaggerTestComponent @@ -2,6 +2,8 @@ package test; import com.google.common.collect.ImmutableMap; import dagger.internal.DaggerGenerated; +import dagger.internal.IdentifierNameString; +import dagger.internal.KeepFieldType; import dagger.internal.LazyClassKeyMap; import java.util.Map; import javax.annotation.processing.Generated; @@ -50,8 +52,21 @@ final class DaggerTestComponent { @Override public Map, Integer> classKey() { - return LazyClassKeyMap.of(ImmutableMap.of(MapKeyBindingsModule_ClassKey_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey(), MapKeyBindingsModule_ClassKey2_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey2())); + return LazyClassKeyMap.of(ImmutableMap.of(LazyClassKeyProvider.test_Foo_Bar, MapKeyBindingsModule.classKey(), LazyClassKeyProvider.test_Foo_Bar3, MapKeyBindingsModule.classKey2())); } } + + @IdentifierNameString + private static final class LazyClassKeyProvider { + static String test_Foo_Bar = "test.Foo_Bar"; + + static String test_Foo_Bar3 = "test.Foo$Bar"; + + @KeepFieldType + Foo_Bar test_Foo_Bar2; + + @KeepFieldType + Foo.Bar test_Foo_Bar4; + } } diff --git a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_FAST_INIT_MODE_test.DaggerTestComponent b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_FAST_INIT_MODE_test.DaggerTestComponent index a3faffc55c7..2d8e6fec35b 100644 --- a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_FAST_INIT_MODE_test.DaggerTestComponent +++ b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_lazyClassKeySimilarQualifiedName_doesNotConflict_FAST_INIT_MODE_test.DaggerTestComponent @@ -2,6 +2,8 @@ package test; import com.google.common.collect.ImmutableMap; import dagger.internal.DaggerGenerated; +import dagger.internal.IdentifierNameString; +import dagger.internal.KeepFieldType; import dagger.internal.LazyClassKeyMap; import java.util.Map; import javax.annotation.processing.Generated; @@ -50,8 +52,21 @@ final class DaggerTestComponent { @Override public Map, Integer> classKey() { - return LazyClassKeyMap.of(ImmutableMap.of(MapKeyBindingsModule_ClassKey_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey(), MapKeyBindingsModule_ClassKey2_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey2())); + return LazyClassKeyMap.of(ImmutableMap.of(LazyClassKeyProvider.test_Foo_Bar, MapKeyBindingsModule.classKey(), LazyClassKeyProvider.test_Foo_Bar3, MapKeyBindingsModule.classKey2())); } } + + @IdentifierNameString + private static final class LazyClassKeyProvider { + static String test_Foo_Bar = "test.Foo_Bar"; + + static String test_Foo_Bar3 = "test.Foo$Bar"; + + @KeepFieldType + Foo_Bar test_Foo_Bar2; + + @KeepFieldType + Foo.Bar test_Foo_Bar4; + } } diff --git a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_DEFAULT_MODE_test.DaggerTestComponent b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_DEFAULT_MODE_test.DaggerTestComponent index 77e0a95c7ce..a1b4bfe4a1f 100644 --- a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_DEFAULT_MODE_test.DaggerTestComponent +++ b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_DEFAULT_MODE_test.DaggerTestComponent @@ -2,6 +2,7 @@ package test; import com.google.common.collect.ImmutableMap; import dagger.internal.DaggerGenerated; +import dagger.internal.IdentifierNameString; import dagger.internal.LazyClassKeyMap; import dagger.internal.MapFactory; import dagger.internal.Provider; @@ -10,7 +11,6 @@ import javax.annotation.processing.Generated; import mapkeys.MapKeys; import mapkeys.MapModule; import mapkeys.MapModule_ClassKeyFactory; -import mapkeys.MapModule_ClassKey_LazyMapKey; import mapkeys.MapModule_ComplexKeyWithInaccessibleAnnotationValueFactory; import mapkeys.MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey; import mapkeys.MapModule_ComplexKeyWithInaccessibleArrayValueFactory; @@ -67,13 +67,13 @@ final class DaggerTestComponent { @SuppressWarnings("unchecked") private void initialize() { - this.mapOfClassOfAndIntegerProvider = LazyClassKeyMap.MapFactory.of(MapFactory.builder(1).put(MapModule_ClassKey_LazyMapKey.lazyClassKeyName, MapModule_ClassKeyFactory.create()).build()); + this.mapOfClassOfAndIntegerProvider = LazyClassKeyMap.MapFactory.of(MapFactory.builder(1).put(LazyClassKeyProvider.mapkeys_MapKeys_Inaccessible, MapModule_ClassKeyFactory.create()).build()); this.mapOfComplexKeyAndIntegerProvider = MapFactory.builder(3).put(MapModule_ComplexKeyWithInaccessibleValueMapKey.create(), MapModule_ComplexKeyWithInaccessibleValueFactory.create()).put(MapModule_ComplexKeyWithInaccessibleArrayValueMapKey.create(), MapModule_ComplexKeyWithInaccessibleArrayValueFactory.create()).put(MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey.create(), MapModule_ComplexKeyWithInaccessibleAnnotationValueFactory.create()).build(); } @Override public Map, Integer> classKey() { - return LazyClassKeyMap.of(ImmutableMap.of(MapModule_ClassKey_LazyMapKey.lazyClassKeyName, MapModule.classKey())); + return LazyClassKeyMap.of(ImmutableMap.of(LazyClassKeyProvider.mapkeys_MapKeys_Inaccessible, MapModule.classKey())); } @Override @@ -91,5 +91,10 @@ final class DaggerTestComponent { return mapOfComplexKeyAndIntegerProvider; } } + + @IdentifierNameString + private static final class LazyClassKeyProvider { + static String mapkeys_MapKeys_Inaccessible = "mapkeys.MapKeys$Inaccessible"; + } } diff --git a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_FAST_INIT_MODE_test.DaggerTestComponent b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_FAST_INIT_MODE_test.DaggerTestComponent index 71da9b92d62..b3c32b90939 100644 --- a/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_FAST_INIT_MODE_test.DaggerTestComponent +++ b/javatests/dagger/internal/codegen/goldens/LazyClassKeyMapBindingComponentProcessorTest_mapBindingsWithInaccessibleKeys_FAST_INIT_MODE_test.DaggerTestComponent @@ -2,13 +2,13 @@ package test; import com.google.common.collect.ImmutableMap; import dagger.internal.DaggerGenerated; +import dagger.internal.IdentifierNameString; import dagger.internal.LazyClassKeyMap; import dagger.internal.Provider; import java.util.Map; import javax.annotation.processing.Generated; import mapkeys.MapKeys; import mapkeys.MapModule; -import mapkeys.MapModule_ClassKey_LazyMapKey; import mapkeys.MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey; import mapkeys.MapModule_ComplexKeyWithInaccessibleArrayValueMapKey; import mapkeys.MapModule_ComplexKeyWithInaccessibleValueMapKey; @@ -101,7 +101,7 @@ final class DaggerTestComponent { public T get() { switch (id) { case 0: // java.util.Map,java.lang.Integer> - return (T) LazyClassKeyMap.of(ImmutableMap.of(MapModule_ClassKey_LazyMapKey.lazyClassKeyName, MapModule.classKey())); + return (T) LazyClassKeyMap.of(ImmutableMap.of(LazyClassKeyProvider.mapkeys_MapKeys_Inaccessible, MapModule.classKey())); case 1: // java.util.Map return (T) ImmutableMap.of(MapModule_ComplexKeyWithInaccessibleValueMapKey.create(), MapModule.complexKeyWithInaccessibleValue(), MapModule_ComplexKeyWithInaccessibleArrayValueMapKey.create(), MapModule.complexKeyWithInaccessibleArrayValue(), MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey.create(), MapModule.complexKeyWithInaccessibleAnnotationValue()); @@ -111,5 +111,10 @@ final class DaggerTestComponent { } } } + + @IdentifierNameString + private static final class LazyClassKeyProvider { + static String mapkeys_MapKeys_Inaccessible = "mapkeys.MapKeys$Inaccessible"; + } }