Home | History | Annotate | Download | only in cycle
      1 /*
      2  * Copyright (C) 2015 Google, Inc.
      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 package test.cycle;
     17 
     18 import dagger.Component;
     19 import dagger.Lazy;
     20 import dagger.Module;
     21 import dagger.Provides;
     22 import dagger.Subcomponent;
     23 import dagger.mapkeys.StringKey;
     24 import java.util.Map;
     25 import javax.inject.Inject;
     26 import javax.inject.Provider;
     27 
     28 import static dagger.Provides.Type.MAP;
     29 
     30 /**
     31  * Cycle classes used for testing cyclic dependencies.
     32  * A <- (E <- D <- B <- C <- Provider<A>, Lazy<A>), (B <- C <- Provider<A>, Lazy<A>)
     33  * S <- Provider<S>, Lazy<S>
     34  *
     35  * @author Tony Bentancur
     36  * @since 2.0
     37  */
     38 
     39 final class Cycles {
     40   private Cycles() {}
     41 
     42   static class A {
     43     public final B b;
     44     public final E e;
     45 
     46     @Inject
     47     A(E e, B b) {
     48       this.e = e;
     49       this.b = b;
     50     }
     51   }
     52 
     53   static class B {
     54     public final C c;
     55 
     56     @Inject
     57     B(C c) {
     58       this.c = c;
     59     }
     60   }
     61 
     62   static class C {
     63     public final Provider<A> aProvider;
     64     @Inject public Lazy<A> aLazy;
     65 
     66     @Inject
     67     C(Provider<A> aProvider) {
     68       this.aProvider = aProvider;
     69     }
     70   }
     71 
     72   static class D {
     73     public final B b;
     74 
     75     @Inject
     76     D(B b) {
     77       this.b = b;
     78     }
     79   }
     80 
     81   static class E {
     82     public final D d;
     83 
     84     @Inject
     85     E(D d) {
     86       this.d = d;
     87     }
     88   }
     89 
     90   static class S {
     91     public final Provider<S> sProvider;
     92     @Inject public Lazy<S> sLazy;
     93 
     94     @Inject
     95     S(Provider<S> sProvider) {
     96       this.sProvider = sProvider;
     97     }
     98   }
     99 
    100   static class X {
    101     public final Y y;
    102 
    103     @Inject
    104     X(Y y) {
    105       this.y = y;
    106     }
    107   }
    108 
    109   static class Y {
    110     public final Map<String, Provider<X>> mapOfProvidersOfX;
    111     public final Map<String, Provider<Y>> mapOfProvidersOfY;
    112 
    113     @Inject
    114     Y(Map<String, Provider<X>> mapOfProvidersOfX, Map<String, Provider<Y>> mapOfProvidersOfY) {
    115       this.mapOfProvidersOfX = mapOfProvidersOfX;
    116       this.mapOfProvidersOfY = mapOfProvidersOfY;
    117     }
    118   }
    119 
    120   @Module
    121   static class CycleMapModule {
    122     @Provides(type = MAP)
    123     @StringKey("X")
    124     static X x(X x) {
    125       return x;
    126     }
    127 
    128     @Provides(type = MAP)
    129     @StringKey("Y")
    130     static Y y(Y y) {
    131       return y;
    132     }
    133   }
    134 
    135   @SuppressWarnings("dependency-cycle")
    136   @Component(modules = CycleMapModule.class)
    137   interface CycleMapComponent {
    138     Y y();
    139   }
    140 
    141   @SuppressWarnings("dependency-cycle")
    142   @Component
    143   interface CycleComponent {
    144     A a();
    145 
    146     C c();
    147 
    148     ChildCycleComponent child();
    149   }
    150 
    151   @SuppressWarnings("dependency-cycle")
    152   @Component
    153   interface SelfCycleComponent {
    154     S s();
    155   }
    156 
    157   @Subcomponent
    158   interface ChildCycleComponent {
    159     @SuppressWarnings("dependency-cycle")
    160     A a();
    161   }
    162 }
    163