1 /*
2  * Copyright (C) 2017 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.membersinject;
18 
19 import dagger.Component;
20 import dagger.Module;
21 import dagger.Provides;
22 import javax.inject.Inject;
23 
24 /**
25  * This exhibits a regression case, that albeit weird, is valid according to the JSR 330 spec. JSR
26  * 330 specifies a rough ordering by which members should be injected, and it is possible to rely on
27  * such ordering. When members injecting {@link Subtype}, field injection is guaranteed to be
28  * performed on {@link Base} first. The binding for {@code @FirstToString} in {@link
29  * OrderingModule#provideToString()} relies on this ordering, and thus uses the value in {@link
30  * Base#first} to satisfy the binding.
31  */
32 class MembersInjectionOrdering {
33   static class Base {
34     @Inject First first;
35   }
36 
37   static class Subtype extends Base {
38     @Inject String firstToString;
39   }
40 
41   @Module
42   static class OrderingModule {
43     private final Subtype subtype;
44 
OrderingModule(Subtype subtype)45     OrderingModule(Subtype subtype) {
46       this.subtype = subtype;
47     }
48 
49     @Provides
provideToString()50     String provideToString() {
51       return subtype.first.toString();
52     }
53   }
54 
55   @Component(modules = OrderingModule.class)
56   interface TestComponent {
inject(Subtype subtype)57     void inject(Subtype subtype);
58   }
59 
60   static class First {
61     @Inject
First()62     First() {}
63   }
64 }
65