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.functional.names; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import dagger.Component; 22 import javax.inject.Inject; 23 import javax.inject.Provider; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 /** This is a regression test for https://github.com/google/dagger/issues/3069. */ 29 @RunWith(JUnit4.class) 30 public final class ComponentFactoryNameConflictsTest { 31 32 // A class name "Create" so the private method name in fastInit mode conflicts with the create() 33 // factory method. 34 public static final class Create { 35 // Take a dependency so we don't just inline this directly. Create(Provider<CreateUsage> provider)36 @Inject Create(Provider<CreateUsage> provider) {} 37 } 38 39 // Use another class to make sure the create class is used but not a direct component entry point. 40 public static final class CreateUsage { CreateUsage(Create create)41 @Inject CreateUsage(Create create) {} 42 } 43 44 @Component 45 interface CreateComponent { getCreateUsage()46 CreateUsage getCreateUsage(); 47 } 48 49 @Test testCreate()50 public void testCreate() { 51 CreateComponent testComponent = 52 DaggerComponentFactoryNameConflictsTest_CreateComponent.create(); 53 CreateUsage createUsage = testComponent.getCreateUsage(); 54 assertThat(createUsage).isNotNull(); 55 } 56 57 // A class name "Builder" so the private method name in fastInit mode conflicts with the builder() 58 // factory method. 59 public static final class Builder { 60 // Take a dependency so we don't just inline this directly. Builder(Provider<BuilderUsage> provider)61 @Inject Builder(Provider<BuilderUsage> provider) {} 62 } 63 64 public static final class BuilderUsage { BuilderUsage(Builder create)65 @Inject BuilderUsage(Builder create) {} 66 } 67 68 @Component 69 interface BuilderComponent { getBuilderUsage()70 BuilderUsage getBuilderUsage(); 71 72 @Component.Builder 73 interface OtherBuilder { build()74 BuilderComponent build(); 75 } 76 } 77 78 // Technically this test passes without claiming the name "builder" when we add the method (even 79 // though we do anyway for safety) because KeyVariableNamer actually hardcodes a list of common 80 // names to avoid which includes "builder". 81 @Test testBuilder()82 public void testBuilder() { 83 BuilderComponent testComponent = 84 DaggerComponentFactoryNameConflictsTest_BuilderComponent.builder().build(); 85 BuilderUsage builderUsage = testComponent.getBuilderUsage(); 86 assertThat(builderUsage).isNotNull(); 87 } 88 } 89