1*f585d8a3SJacky Wang /* 2*f585d8a3SJacky Wang * Copyright (C) 2023 The Dagger Authors. 3*f585d8a3SJacky Wang * 4*f585d8a3SJacky Wang * Licensed under the Apache License, Version 2.0 (the "License"); 5*f585d8a3SJacky Wang * you may not use this file except in compliance with the License. 6*f585d8a3SJacky Wang * You may obtain a copy of the License at 7*f585d8a3SJacky Wang * 8*f585d8a3SJacky Wang * http://www.apache.org/licenses/LICENSE-2.0 9*f585d8a3SJacky Wang * 10*f585d8a3SJacky Wang * Unless required by applicable law or agreed to in writing, software 11*f585d8a3SJacky Wang * distributed under the License is distributed on an "AS IS" BASIS, 12*f585d8a3SJacky Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*f585d8a3SJacky Wang * See the License for the specific language governing permissions and 14*f585d8a3SJacky Wang * limitations under the License. 15*f585d8a3SJacky Wang */ 16*f585d8a3SJacky Wang 17*f585d8a3SJacky Wang package dagger.functional.binds; 18*f585d8a3SJacky Wang 19*f585d8a3SJacky Wang import static com.google.common.truth.Truth.assertThat; 20*f585d8a3SJacky Wang 21*f585d8a3SJacky Wang import dagger.Binds; 22*f585d8a3SJacky Wang import dagger.Component; 23*f585d8a3SJacky Wang import dagger.Module; 24*f585d8a3SJacky Wang import javax.inject.Inject; 25*f585d8a3SJacky Wang import javax.inject.Provider; 26*f585d8a3SJacky Wang import javax.inject.Singleton; 27*f585d8a3SJacky Wang import org.junit.Test; 28*f585d8a3SJacky Wang import org.junit.runner.RunWith; 29*f585d8a3SJacky Wang import org.junit.runners.JUnit4; 30*f585d8a3SJacky Wang 31*f585d8a3SJacky Wang // This is a regression test for b/267223822 where a scoped @Binds used in a cycle caused problems 32*f585d8a3SJacky Wang // in fastInit mode. 33*f585d8a3SJacky Wang @RunWith(JUnit4.class) 34*f585d8a3SJacky Wang public class RecursiveBindsTest { 35*f585d8a3SJacky Wang 36*f585d8a3SJacky Wang public interface Foo {} 37*f585d8a3SJacky Wang 38*f585d8a3SJacky Wang public static final class FooImpl implements Foo { FooImpl(@uppressWarnings"unused") Provider<Foo> provider)39*f585d8a3SJacky Wang @Inject FooImpl(@SuppressWarnings("unused") Provider<Foo> provider) {} 40*f585d8a3SJacky Wang } 41*f585d8a3SJacky Wang 42*f585d8a3SJacky Wang @Module 43*f585d8a3SJacky Wang public interface FooModule { 44*f585d8a3SJacky Wang // This binding must be scoped to create the cycle. Otherwise without a scope, the generated 45*f585d8a3SJacky Wang // code just doesn't have a field for this @Binds because we can directly use FooImpl's 46*f585d8a3SJacky Wang // provider as they are equivalent. 47*f585d8a3SJacky Wang @Binds 48*f585d8a3SJacky Wang @Singleton bindFoo(FooImpl impl)49*f585d8a3SJacky Wang Foo bindFoo(FooImpl impl); 50*f585d8a3SJacky Wang } 51*f585d8a3SJacky Wang 52*f585d8a3SJacky Wang @Component(modules = FooModule.class) 53*f585d8a3SJacky Wang @Singleton 54*f585d8a3SJacky Wang public interface TestSingletonComponent { 55*f585d8a3SJacky Wang // We get the impl here to create a cycle where the impl factory needs to be delegated. 56*f585d8a3SJacky Wang // That way the scoped binds (which does something like DoubleCheck.provider(implFactory)) is 57*f585d8a3SJacky Wang // the one that would fail if it wasn't delegated properly. getFooImplProvider()58*f585d8a3SJacky Wang Provider<FooImpl> getFooImplProvider(); 59*f585d8a3SJacky Wang } 60*f585d8a3SJacky Wang 61*f585d8a3SJacky Wang @Test test()62*f585d8a3SJacky Wang public void test() { 63*f585d8a3SJacky Wang // Technically the NPE would happen when just initializing the component. 64*f585d8a3SJacky Wang assertThat(DaggerRecursiveBindsTest_TestSingletonComponent.create().getFooImplProvider().get()) 65*f585d8a3SJacky Wang .isNotNull(); 66*f585d8a3SJacky Wang } 67*f585d8a3SJacky Wang } 68