1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.objectfactory.guice;
35
36 import java.lang.reflect.InvocationTargetException;
37 import java.util.Set;
38 import javax.inject.Inject;
39 import javax.inject.Provider;
40
41 import com.google.common.collect.ImmutableSet;
42 import com.google.common.collect.Sets;
43 import com.google.inject.Injector;
44 import com.google.inject.Key;
45 import com.google.inject.spi.Dependency;
46 import com.google.inject.spi.InjectionPoint;
47 import com.google.inject.spi.ProviderWithDependencies;
48
49 import info.magnolia.objectfactory.ComponentFactory;
50 import info.magnolia.objectfactory.ComponentFactoryUtil;
51 import info.magnolia.objectfactory.ComponentProvider;
52
53
54
55
56
57
58 public class GuiceUtils {
59
60 public static <T> Provider<T> providerForInstance(final T instance) {
61 return new Provider<T>() {
62 @Override
63 public T get() {
64 return instance;
65 }
66
67 @Override
68 public String toString() {
69 return "Provider for: " + instance.toString();
70 }
71 };
72 }
73
74 public static <T> Provider<T> providerForComponentFactory(final Class<? extends ComponentFactory<T>> componentFactoryClass) {
75 return new Provider<T>() {
76
77 @Inject
78 private ComponentProvider componentProvider;
79
80 @Override
81 public T get() {
82 try {
83 ComponentFactory<T> componentFactory = ComponentFactoryUtil.createFactory(componentFactoryClass, componentProvider);
84 ((GuiceComponentProvider) componentProvider).injectMembers(componentFactory);
85 T instance = componentFactory.newInstance();
86 return instance;
87 } catch (InstantiationException e) {
88 throw new IllegalStateException("Failed to create ComponentFactory [" + componentFactoryClass + "]", e);
89 } catch (IllegalAccessException e) {
90 throw new IllegalStateException("Failed to create ComponentFactory [" + componentFactoryClass + "]", e);
91 } catch (InvocationTargetException e) {
92 throw new IllegalStateException("Failed to create ComponentFactory [" + componentFactoryClass + "]", e);
93 }
94 }
95
96 @Override
97 public String toString() {
98 return "ComponentFactory: " + componentFactoryClass.toString();
99 }
100 };
101 }
102
103 public static <T> Provider<T> providerForComponentFactory(final ComponentFactory<T> componentFactory) {
104
105
106
107
108 Set<InjectionPoint> injectionPoints =
109 InjectionPoint.forInstanceMethodsAndFields(componentFactory.getClass());
110 if(injectionPoints.isEmpty()) {
111 return new com.google.inject.Provider<T>() {
112 @Override
113 public T get() {
114 return componentFactory.newInstance();
115 }
116
117 @Override
118 public String toString() {
119 return "ComponentFactory: " + componentFactory.toString();
120 }
121 };
122 } else {
123 Set<Dependency<?>> mutableDeps = Sets.newHashSet();
124 for(InjectionPoint ip : injectionPoints) {
125 mutableDeps.addAll(ip.getDependencies());
126 }
127 final Set<Dependency<?>> dependencies = ImmutableSet.copyOf(mutableDeps);
128 return new ProviderWithDependencies<T>() {
129 @SuppressWarnings("unused")
130 @com.google.inject.Inject
131 void initialize(Injector injector) {
132 injector.injectMembers(componentFactory);
133 }
134
135 @Override
136 public Set<Dependency<?>> getDependencies() {
137 return dependencies;
138 }
139
140 @Override
141 public T get() {
142 return componentFactory.newInstance();
143 }
144
145 @Override
146 public String toString() {
147 return "ComponentFactory: " + componentFactory.toString();
148 }
149 };
150 }
151 }
152
153 public static boolean hasExplicitBindingFor(Injector injector, Class<?> type) {
154 Key<?> key = Key.get(type);
155 Injector target = injector;
156 do {
157 if (target.getBindings().containsKey(key)) {
158 return true;
159 }
160 target = target.getParent();
161 } while (target != null);
162 return false;
163 }
164 }