• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 library1;
18 
19 import dagger.Binds;
20 import dagger.Module;
21 import dagger.Provides;
22 import javax.inject.Singleton;
23 import library2.MyTransitiveAnnotation;
24 import library2.MyTransitiveType;
25 
26 /**
27  * A class used to test that Dagger won't fail when non-dagger related annotations cannot be
28  * resolved.
29  *
30  * <p>During the compilation of {@code :app}, {@link MyTransitiveAnnotation} will no longer be on
31  * the classpath. In most cases, Dagger shouldn't care that the annotation isn't on the classpath
32  */
33 @MyTransitiveAnnotation
34 @MyAnnotation(MyTransitiveType.VALUE)
35 @MyOtherAnnotation(MyTransitiveType.class)
36 @Module(includes = {MyComponentModule.MyAbstractModule.class})
37 public final class MyComponentModule {
38   // Define bindings for each configuration: Scoped/Unscoped, Qualified/UnQualified, Provides/Binds
39   public static class ScopedQualifiedBindsType {}
40   public static final class ScopedQualifiedProvidesType extends ScopedQualifiedBindsType {}
41   public static class ScopedUnqualifiedBindsType {}
42   public static final class ScopedUnqualifiedProvidesType extends ScopedUnqualifiedBindsType {}
43   public static class UnscopedQualifiedBindsType {}
44   public static final class UnscopedQualifiedProvidesType extends UnscopedQualifiedBindsType {}
45   public static class UnscopedUnqualifiedBindsType {}
46   public static final class UnscopedUnqualifiedProvidesType extends UnscopedUnqualifiedBindsType {}
47 
48   @MyTransitiveAnnotation
49   @MyAnnotation(MyTransitiveType.VALUE)
50   @MyOtherAnnotation(MyTransitiveType.class)
51   @Provides
52   @Singleton
53   @MyQualifier
scopedQualifiedProvidesType( @yQualifier @yTransitiveAnnotation @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) Dep dep)54   ScopedQualifiedProvidesType scopedQualifiedProvidesType(
55       @MyQualifier
56           @MyTransitiveAnnotation
57           @MyAnnotation(MyTransitiveType.VALUE)
58           @MyOtherAnnotation(MyTransitiveType.class)
59           Dep dep) {
60     return new ScopedQualifiedProvidesType();
61   }
62 
63   @MyTransitiveAnnotation
64   @MyAnnotation(MyTransitiveType.VALUE)
65   @MyOtherAnnotation(MyTransitiveType.class)
66   @Provides
67   @Singleton
scopedUnqualifiedProvidesType( @yQualifier @yTransitiveAnnotation @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) Dep dep)68   ScopedUnqualifiedProvidesType scopedUnqualifiedProvidesType(
69       @MyQualifier
70           @MyTransitiveAnnotation
71           @MyAnnotation(MyTransitiveType.VALUE)
72           @MyOtherAnnotation(MyTransitiveType.class)
73           Dep dep) {
74     return new ScopedUnqualifiedProvidesType();
75   }
76 
77   @MyTransitiveAnnotation
78   @MyAnnotation(MyTransitiveType.VALUE)
79   @MyOtherAnnotation(MyTransitiveType.class)
80   @Provides
81   @MyQualifier
unscopedQualifiedProvidesType( @yQualifier @yTransitiveAnnotation @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) Dep dep)82   UnscopedQualifiedProvidesType unscopedQualifiedProvidesType(
83       @MyQualifier
84           @MyTransitiveAnnotation
85           @MyAnnotation(MyTransitiveType.VALUE)
86           @MyOtherAnnotation(MyTransitiveType.class)
87           Dep dep) {
88     return new UnscopedQualifiedProvidesType();
89   }
90 
91   @MyTransitiveAnnotation
92   @MyAnnotation(MyTransitiveType.VALUE)
93   @MyOtherAnnotation(MyTransitiveType.class)
94   @Provides
unscopedUnqualifiedProvidesType( @yQualifier @yTransitiveAnnotation @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) Dep dep)95   UnscopedUnqualifiedProvidesType unscopedUnqualifiedProvidesType(
96       @MyQualifier
97           @MyTransitiveAnnotation
98           @MyAnnotation(MyTransitiveType.VALUE)
99           @MyOtherAnnotation(MyTransitiveType.class)
100           Dep dep) {
101     return new UnscopedUnqualifiedProvidesType();
102   }
103 
104   @MyTransitiveAnnotation
105   @MyAnnotation(MyTransitiveType.VALUE)
106   @MyOtherAnnotation(MyTransitiveType.class)
107   @Module
108   interface MyAbstractModule {
109     // @MyTransitiveAnnotation: Not yet supported
110     @MyAnnotation(MyTransitiveType.VALUE)
111     @MyOtherAnnotation(MyTransitiveType.class)
112     @Binds
113     @Singleton
114     @MyQualifier
scopedQualifiedBindsType( @yQualifier @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) ScopedQualifiedProvidesType scopedQualifiedProvidesType)115     ScopedQualifiedBindsType scopedQualifiedBindsType(
116         // @MyTransitiveAnnotation: Not yet supported
117         @MyQualifier
118             @MyAnnotation(MyTransitiveType.VALUE)
119             @MyOtherAnnotation(MyTransitiveType.class)
120             ScopedQualifiedProvidesType scopedQualifiedProvidesType);
121 
122     // @MyTransitiveAnnotation: Not yet supported
123     @MyAnnotation(MyTransitiveType.VALUE)
124     @MyOtherAnnotation(MyTransitiveType.class)
125     @Binds
126     @Singleton
scopedUnqualifiedBindsType( @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) ScopedUnqualifiedProvidesType scopedUnqualifiedProvidesType)127     ScopedUnqualifiedBindsType scopedUnqualifiedBindsType(
128         // @MyTransitiveAnnotation: Not yet supported
129         @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class)
130             ScopedUnqualifiedProvidesType scopedUnqualifiedProvidesType);
131 
132     // @MyTransitiveAnnotation: Not yet supported
133     @MyAnnotation(MyTransitiveType.VALUE)
134     @MyOtherAnnotation(MyTransitiveType.class)
135     @Binds
136     @MyQualifier
unscopedQualifiedBindsType( @yQualifier @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) UnscopedQualifiedProvidesType unscopedQualifiedProvidesType)137     UnscopedQualifiedBindsType unscopedQualifiedBindsType(
138         // @MyTransitiveAnnotation: Not yet supported
139         @MyQualifier
140             @MyAnnotation(MyTransitiveType.VALUE)
141             @MyOtherAnnotation(MyTransitiveType.class)
142             UnscopedQualifiedProvidesType unscopedQualifiedProvidesType);
143 
144     // @MyTransitiveAnnotation: Not yet supported
145     @MyAnnotation(MyTransitiveType.VALUE)
146     @MyOtherAnnotation(MyTransitiveType.class)
147     @Binds
unscopedUnqualifiedBindsType( @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) UnscopedUnqualifiedProvidesType unscopedUnqualifiedProvidesType)148     UnscopedUnqualifiedBindsType unscopedUnqualifiedBindsType(
149         // @MyTransitiveAnnotation: Not yet supported
150         @MyAnnotation(MyTransitiveType.VALUE) @MyOtherAnnotation(MyTransitiveType.class)
151             UnscopedUnqualifiedProvidesType unscopedUnqualifiedProvidesType);
152   }
153 
154   @MyTransitiveAnnotation
155   @MyAnnotation(MyTransitiveType.VALUE)
156   @MyOtherAnnotation(MyTransitiveType.class)
157   @Provides
158   @MyQualifier
provideQualifiedDep()159   Dep provideQualifiedDep() {
160     return new Dep();
161   }
162 
163   // Provide an unqualified Dep to ensure that if we accidentally drop the qualifier
164   // we'll get a runtime exception.
165   @MyTransitiveAnnotation
166   @MyAnnotation(MyTransitiveType.VALUE)
167   @MyOtherAnnotation(MyTransitiveType.class)
168   @Provides
provideDep()169   Dep provideDep() {
170     throw new UnsupportedOperationException();
171   }
172 
173   // Non-Dagger elements
174 
175   @MyTransitiveAnnotation
176   @MyAnnotation(MyTransitiveType.VALUE)
177   @MyOtherAnnotation(MyTransitiveType.class)
178   private Dep dep;
179 
180   @MyTransitiveAnnotation
181   @MyAnnotation(MyTransitiveType.VALUE)
182   @MyOtherAnnotation(MyTransitiveType.class)
183   private MyTransitiveType nonDaggerField;
184 
185   @MyTransitiveAnnotation
186   @MyAnnotation(MyTransitiveType.VALUE)
187   @MyOtherAnnotation(MyTransitiveType.class)
MyComponentModule( @yTransitiveAnnotation @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) Dep dep)188   public MyComponentModule(
189       @MyTransitiveAnnotation
190           @MyAnnotation(MyTransitiveType.VALUE)
191           @MyOtherAnnotation(MyTransitiveType.class)
192           Dep dep) {
193     this.dep = dep;
194     this.nonDaggerField = new MyTransitiveType();
195   }
196 
197   @MyTransitiveAnnotation
198   @MyAnnotation(MyTransitiveType.VALUE)
199   @MyOtherAnnotation(MyTransitiveType.class)
nonDaggerMethod( @yTransitiveAnnotation @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) MyTransitiveType nonDaggerParameter)200   MyTransitiveType nonDaggerMethod(
201       @MyTransitiveAnnotation
202           @MyAnnotation(MyTransitiveType.VALUE)
203           @MyOtherAnnotation(MyTransitiveType.class)
204           MyTransitiveType nonDaggerParameter) {
205     return nonDaggerParameter;
206   }
207 
208   @MyTransitiveAnnotation
209   @MyAnnotation(MyTransitiveType.VALUE)
210   @MyOtherAnnotation(MyTransitiveType.class)
nonDaggerStaticMethod( @yTransitiveAnnotation @yAnnotationMyTransitiveType.VALUE) @yOtherAnnotationMyTransitiveType.class) MyTransitiveType nonDaggerParameter)211   static MyTransitiveType nonDaggerStaticMethod(
212       @MyTransitiveAnnotation
213           @MyAnnotation(MyTransitiveType.VALUE)
214           @MyOtherAnnotation(MyTransitiveType.class)
215           MyTransitiveType nonDaggerParameter) {
216     return nonDaggerParameter;
217   }
218 }
219