xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/kotlin/CompanionModuleClasses.kt (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.kotlin
18 
19 import dagger.Binds
20 import dagger.Component
21 import dagger.Module
22 import dagger.Provides
23 import javax.inject.Inject
24 import javax.inject.Named
25 
26 @Component(
27   modules = [
28     TestKotlinModuleWithCompanion::class,
29     TestKotlinModuleWithNamedCompanion::class,
30     TestKotlinAbstractModuleWithCompanion::class,
31     TestKotlinWorkaroundModuleWithCompanion::class,
32     TestKotlinModuleWithPrivateCompanion::class
33   ]
34 )
35 interface TestKotlinComponentWithCompanionModule {
getDataAnull36   fun getDataA(): TestDataA
37   fun getDataB(): TestDataB
38   fun getBoolean(): Boolean
39   fun getStringType(): String
40   @Named("Cat")
41   fun getCatNamedStringType(): String
42   @Named("Dog")
43   fun getDogNamedStringType(): String
44 
45   fun getInterface(): TestInterface
46   fun getLong(): Long
47   fun getDouble(): Double
48   fun getInteger(): Int
49 }
50 
51 @Module
52 class TestKotlinModuleWithCompanion {
53   @Provides
54   fun provideDataA() = TestDataA("test")
55 
56   companion object {
57     @Provides
58     fun provideDataB() = TestDataB("test")
59 
60     @Provides
61     fun provideBoolean(): Boolean = true
62   }
63 }
64 
65 @Module
66 class TestKotlinModuleWithNamedCompanion {
67 
68   @Provides
69   @Named("Cat")
provideNamedStringnull70   fun provideNamedString() = "Cat"
71 
72   companion object Foo {
73     @Provides
74     fun provideStringType(): String = ""
75   }
76 }
77 
78 @Module
79 abstract class TestKotlinAbstractModuleWithCompanion {
80 
81   @Binds
bindInterfacenull82   abstract fun bindInterface(injectable: TestInjectable): TestInterface
83 
84   companion object {
85     @Provides
86     fun provideLong() = 4L
87   }
88 }
89 
90 @Module
91 class TestKotlinWorkaroundModuleWithCompanion {
92 
93   @Provides
provideDoublenull94   fun provideDouble() = 1.0
95 
96   @Module
97   companion object {
98     @Provides
99     @JvmStatic
100     fun provideInteger() = 2
101   }
102 }
103 
104 @Module
105 class TestKotlinModuleWithPrivateCompanion {
106 
107   @Provides
108   @Named("Dog")
getNamedStringTypenull109   fun getNamedStringType() = "Dog"
110 
111   private companion object {
112     fun randomFunction() = ""
113   }
114 }
115 
116 data class TestDataA(val data: String)
117 data class TestDataB(val data: String)
118 
119 interface TestInterface
120 class TestInjectable @Inject constructor() : TestInterface
121 
122