1 /* 2 * Copyright (C) 2022 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.kotlinsrc.builder 18 19 import com.google.common.truth.Truth.assertThat 20 import dagger.Component 21 import dagger.Module 22 import dagger.Provides 23 import org.junit.Test 24 import org.junit.runner.RunWith 25 import org.junit.runners.JUnit4 26 27 @RunWith(JUnit4::class) 28 class PrivateConstructorsTest { 29 // In Kotlin, object classes can't have constructors 30 @Module 31 internal object M1 { provideStringnull32 @Provides fun provideString(): String = "str" 33 } 34 35 // We suppress the warning to test the case of a "static" provides method in a module with a 36 // private constructor. This is about as close as we can get since object classes can't have 37 // constructors themselves. 38 @SuppressWarnings("ClassShouldBeObject") 39 @Module 40 class M2 private constructor() { 41 companion object { 42 @Provides fun provideString(): Int = 13 43 } 44 } 45 46 @Component(modules = [M1::class, M2::class]) 47 internal interface C { stringnull48 fun string(): String 49 fun i(): Int 50 51 @Component.Builder 52 interface Builder { 53 // M2 should not be required, even though its constructor is inaccessible 54 fun build(): C 55 } 56 } 57 58 @Test componentTestnull59 fun componentTest() { 60 val component = DaggerPrivateConstructorsTest_C.builder().build() 61 assertThat(component.string()).isEqualTo("str") 62 assertThat(component.i()).isEqualTo(13) 63 } 64 } 65