1 /* 2 * Copyright (C) 2023 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.membersinject 18 19 import com.google.common.truth.Truth.assertThat 20 import dagger.BindsInstance 21 import dagger.Component 22 import dagger.assisted.Assisted 23 import dagger.assisted.AssistedFactory 24 import dagger.assisted.AssistedInject 25 import javax.inject.Inject 26 import org.junit.Test 27 import org.junit.runner.RunWith 28 import org.junit.runners.JUnit4 29 30 // Regression test for https://github.com/google/dagger/issues/3995. 31 @RunWith(JUnit4::class) 32 internal class MembersInjectionWithJavaKeywordNamesTest { 33 @Component 34 interface MyComponent { myClassnull35 fun myClass(): MyClass 36 37 @Component.Builder 38 interface Builder { 39 @BindsInstance fun addInteger(int: Int): Builder 40 @BindsInstance fun addString(string: String): Builder 41 @BindsInstance fun addLong(long: Long): Builder 42 fun build(): MyComponent 43 } 44 } 45 46 @Suppress("BadInject") 47 class MyClass @Inject constructor(val int: Int) { 48 @Inject @JvmField var string: String = "" 49 50 var long: Long? = null 51 injectMethodnull52 @Inject fun injectMethod(long: Long) { 53 this.long = long 54 } 55 } 56 57 @Test testParametersWithJavaKeywordNamesnull58 fun testParametersWithJavaKeywordNames() { 59 val int = 1 60 val long = 2L 61 val string = "string" 62 val myClass = 63 DaggerMembersInjectionWithJavaKeywordNamesTest_MyComponent.builder() 64 .addInteger(int) 65 .addString(string) 66 .addLong(long) 67 .build() 68 .myClass() 69 assertThat(myClass.int).isEqualTo(int) 70 assertThat(myClass.long).isEqualTo(long) 71 assertThat(myClass.string).isEqualTo(string) 72 } 73 } 74