View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.reflect.internal.types;
21  
22  import org.grouplens.grapht.reflect.InjectionPoint;
23  import org.grouplens.grapht.reflect.internal.ConstructorParameterInjectionPoint;
24  import org.grouplens.grapht.reflect.internal.SetterInjectionPoint;
25  
26  import javax.inject.Inject;
27  
28  public class TypeC {
29      public static final InjectionPoint CONSTRUCTOR;
30      public static final InjectionPoint INTERFACE_A;
31      public static final InjectionPoint TYPE_A;
32      public static final InjectionPoint INTERFACE_B;
33      public static final InjectionPoint TYPE_B;
34      
35      static {
36          try {
37              CONSTRUCTOR = new ConstructorParameterInjectionPoint(TypeC.class.getConstructor(int.class), 0);
38              INTERFACE_A = new SetterInjectionPoint(TypeC.class.getMethod("setRoleA", InterfaceA.class), 0);
39              TYPE_A = new SetterInjectionPoint(TypeC.class.getMethod("setTypeA", TypeA.class), 0);
40              INTERFACE_B = new SetterInjectionPoint(TypeC.class.getMethod("setRoleD", InterfaceB.class), 0);
41              TYPE_B = new SetterInjectionPoint(TypeC.class.getMethod("setTypeB", TypeB.class), 0);
42          } catch(Exception e) {
43              throw new RuntimeException(e);
44          }
45      }
46      
47      private final int value;
48      private InterfaceA a1;
49      private TypeA a2;
50      private InterfaceB b1;
51      private TypeB b2;
52      
53      @Inject
54      public TypeC(@ParameterA int intValue) {
55          // ParameterA has a default value of 5
56          value = intValue;
57      }
58      
59      @Inject
60      public void setRoleA(@RoleA InterfaceA a) {
61          // RoleA has no default type, InterfaceA is implemented by TypeA,
62          // which is then provided by a ProviderA
63          a1 = a;
64      }
65      
66      @Inject
67      public void setTypeA(TypeA a) {
68          // TypeA is provided by ProviderA, which creates instances of TypeB
69          a2 = a;
70      }
71      
72      @Inject
73      public void setRoleD(@RoleD InterfaceB b) {
74          // RoleD defaults to TypeB
75          b1 = b;
76      }
77      
78      @Inject
79      public void setTypeB(TypeB b) {
80          // No default desire, but TypeB is satisfiable on its own
81          b2 = b;
82      }
83      
84      public TypeB getTypeB() {
85          return b2;
86      }
87      
88      public InterfaceB getInterfaceB() {
89          return b1;
90      }
91      
92      public int getIntValue() {
93          return value;
94      }
95  
96      public InterfaceA getInterfaceA() {
97          return a1;
98      }
99      
100     public TypeA getTypeA() {
101         return a2;
102     }
103 }