blob: b3c869e334de8c29964a9a9ae7dd9eb4d958c876 [file] [log] [blame]
limpbizkit477f9f92008-07-28 07:05:14 +00001/**
2 * Copyright (C) 2008 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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit477f9f92008-07-28 07:05:14 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Binder;
20import com.google.inject.Binding;
limpbizkit5ae41eb2009-06-06 17:51:27 +000021import com.google.inject.Key;
limpbizkit5ae41eb2009-06-06 17:51:27 +000022import com.google.inject.Provider;
limpbizkit76c24b12008-12-25 04:32:41 +000023import com.google.inject.spi.ConstructorBinding;
24import com.google.inject.spi.ConvertedConstantBinding;
25import com.google.inject.spi.ExposedBinding;
limpbizkitb3a8f0b2008-09-05 22:30:40 +000026import com.google.inject.spi.InjectionPoint;
limpbizkit76c24b12008-12-25 04:32:41 +000027import com.google.inject.spi.InstanceBinding;
28import com.google.inject.spi.LinkedKeyBinding;
limpbizkitc3f92842008-12-30 19:43:47 +000029import com.google.inject.spi.PrivateElements;
limpbizkit76c24b12008-12-25 04:32:41 +000030import com.google.inject.spi.ProviderBinding;
31import com.google.inject.spi.ProviderInstanceBinding;
32import com.google.inject.spi.ProviderKeyBinding;
33import com.google.inject.spi.UntargettedBinding;
sberlinb7a02b02011-07-08 00:34:16 +000034
limpbizkit477f9f92008-07-28 07:05:14 +000035import java.util.Set;
36
37/**
38 * Handles {@link Binder#bind} and {@link Binder#bindConstant} elements.
39 *
40 * @author crazybob@google.com (Bob Lee)
41 * @author jessewilson@google.com (Jesse Wilson)
42 */
sberlin07170cc2011-03-11 03:02:39 +000043final class BindingProcessor extends AbstractBindingProcessor {
limpbizkit477f9f92008-07-28 07:05:14 +000044
limpbizkit5fb9d922008-10-14 23:35:56 +000045 private final Initializer initializer;
limpbizkit477f9f92008-07-28 07:05:14 +000046
sberlin07170cc2011-03-11 03:02:39 +000047 BindingProcessor(Errors errors, Initializer initializer, ProcessedBindingData bindingData) {
48 super(errors, bindingData);
limpbizkit5fb9d922008-10-14 23:35:56 +000049 this.initializer = initializer;
limpbizkit477f9f92008-07-28 07:05:14 +000050 }
51
limpbizkit03b81a62009-03-18 05:34:39 +000052 @Override public <T> Boolean visit(Binding<T> command) {
sberlin07170cc2011-03-11 03:02:39 +000053 Class<?> rawType = command.getKey().getTypeLiteral().getRawType();
54 if (Void.class.equals(rawType)) {
limpbizkit759662b2009-04-26 21:00:24 +000055 if (command instanceof ProviderInstanceBinding
Sam Berlin8ad60eb2014-03-10 12:49:46 -040056 && ((ProviderInstanceBinding) command).getUserSuppliedProvider()
57 instanceof ProviderMethod) {
limpbizkit759662b2009-04-26 21:00:24 +000058 errors.voidProviderMethod();
59 } else {
60 errors.missingConstantValues();
61 }
limpbizkitd1fe1302008-08-01 06:40:18 +000062 return true;
63 }
sberlin07170cc2011-03-11 03:02:39 +000064
limpbizkit477f9f92008-07-28 07:05:14 +000065 if (rawType == Provider.class) {
66 errors.bindingToProvider();
67 return true;
68 }
sberlin07170cc2011-03-11 03:02:39 +000069
70 return command.acceptTargetVisitor(new Processor<T, Boolean>((BindingImpl<T>)command) {
Sam Berlind51292d2012-02-26 21:23:19 -050071 @Override
sberlin92073172011-03-11 00:03:01 +000072 public Boolean visit(ConstructorBinding<? extends T> binding) {
sberlin07170cc2011-03-11 03:02:39 +000073 prepareBinding();
limpbizkit44475642009-06-20 17:30:23 +000074 try {
75 ConstructorBindingImpl<T> onInjector = ConstructorBindingImpl.create(injector, key,
Sam Berlinc7567772012-05-31 19:54:04 -040076 binding.getConstructor(), source, scoping, errors, false, false);
limpbizkit44475642009-06-20 17:30:23 +000077 scheduleInitialization(onInjector);
78 putBinding(onInjector);
79 } catch (ErrorsException e) {
80 errors.merge(e.getErrors());
81 putBinding(invalidBinding(injector, key, source));
82 }
sberlin92073172011-03-11 00:03:01 +000083 return true;
limpbizkit44475642009-06-20 17:30:23 +000084 }
limpbizkit76c24b12008-12-25 04:32:41 +000085
Sam Berlind51292d2012-02-26 21:23:19 -050086 @Override
sberlin92073172011-03-11 00:03:01 +000087 public Boolean visit(InstanceBinding<? extends T> binding) {
sberlin07170cc2011-03-11 03:02:39 +000088 prepareBinding();
limpbizkit76c24b12008-12-25 04:32:41 +000089 Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
90 T instance = binding.getInstance();
Christian Edward Gruberbf2b16c2013-05-15 18:39:15 -070091 @SuppressWarnings("unchecked") // safe to cast to binding<T> because
92 // the processor was constructed w/ it
limpbizkitfcbdf992008-11-26 02:37:35 +000093 Initializable<T> ref = initializer.requestInjection(
Christian Edward Gruberbf2b16c2013-05-15 18:39:15 -070094 injector, instance, (Binding<T>) binding, source, injectionPoints);
limpbizkit5fb9d922008-10-14 23:35:56 +000095 ConstantFactory<? extends T> factory = new ConstantFactory<T>(ref);
limpbizkit0cb8b862009-08-10 23:57:56 +000096 InternalFactory<? extends T> scopedFactory
97 = Scoping.scope(key, injector, factory, source, scoping);
limpbizkitb3a8f0b2008-09-05 22:30:40 +000098 putBinding(new InstanceBindingImpl<T>(injector, key, source, scopedFactory, injectionPoints,
99 instance));
sberlin92073172011-03-11 00:03:01 +0000100 return true;
limpbizkit477f9f92008-07-28 07:05:14 +0000101 }
102
Sam Berlind51292d2012-02-26 21:23:19 -0500103 @Override
sberlin92073172011-03-11 00:03:01 +0000104 public Boolean visit(ProviderInstanceBinding<? extends T> binding) {
sberlin07170cc2011-03-11 03:02:39 +0000105 prepareBinding();
Sam Berlin8ad60eb2014-03-10 12:49:46 -0400106 javax.inject.Provider<? extends T> provider = binding.getUserSuppliedProvider();
limpbizkit76c24b12008-12-25 04:32:41 +0000107 Set<InjectionPoint> injectionPoints = binding.getInjectionPoints();
Sam Berlin8ad60eb2014-03-10 12:49:46 -0400108 Initializable<? extends javax.inject.Provider<? extends T>> initializable =
109 initializer.<javax.inject.Provider<? extends T>>requestInjection(
110 injector, provider, null, source, injectionPoints);
Sam Berlind51292d2012-02-26 21:23:19 -0500111 // always visited with Binding<T>
112 @SuppressWarnings("unchecked")
sberlinc94f4502011-05-02 13:30:37 +0000113 InternalFactory<T> factory = new InternalFactoryToInitializableAdapter<T>(
timofeyb5e6c9332015-04-20 12:45:07 -0700114 initializable, source,
Sam Berlind51292d2012-02-26 21:23:19 -0500115 injector.provisionListenerStore.get((ProviderInstanceBinding<T>)binding));
limpbizkit0cb8b862009-08-10 23:57:56 +0000116 InternalFactory<? extends T> scopedFactory
117 = Scoping.scope(key, injector, factory, source, scoping);
limpbizkit76c24b12008-12-25 04:32:41 +0000118 putBinding(new ProviderInstanceBindingImpl<T>(injector, key, source, scopedFactory, scoping,
119 provider, injectionPoints));
sberlin92073172011-03-11 00:03:01 +0000120 return true;
limpbizkit477f9f92008-07-28 07:05:14 +0000121 }
122
Sam Berlind51292d2012-02-26 21:23:19 -0500123 @Override
sberlin92073172011-03-11 00:03:01 +0000124 public Boolean visit(ProviderKeyBinding<? extends T> binding) {
sberlin07170cc2011-03-11 03:02:39 +0000125 prepareBinding();
limpbizkitf2f72252009-09-08 00:35:45 +0000126 Key<? extends javax.inject.Provider<? extends T>> providerKey = binding.getProviderKey();
Sam Berlind51292d2012-02-26 21:23:19 -0500127 // always visited with Binding<T>
128 @SuppressWarnings("unchecked")
sberlinc94f4502011-05-02 13:30:37 +0000129 BoundProviderFactory<T> boundProviderFactory = new BoundProviderFactory<T>(
timofeyb5e6c9332015-04-20 12:45:07 -0700130 injector, providerKey, source,
Sam Berlin8ad60eb2014-03-10 12:49:46 -0400131 injector.provisionListenerStore.get((ProviderKeyBinding<T>) binding));
sberlin07170cc2011-03-11 03:02:39 +0000132 bindingData.addCreationListener(boundProviderFactory);
limpbizkit5ae41eb2009-06-06 17:51:27 +0000133 InternalFactory<? extends T> scopedFactory = Scoping.scope(
limpbizkit0cb8b862009-08-10 23:57:56 +0000134 key, injector, (InternalFactory<? extends T>) boundProviderFactory, source, scoping);
limpbizkit477f9f92008-07-28 07:05:14 +0000135 putBinding(new LinkedProviderBindingImpl<T>(
limpbizkit76c24b12008-12-25 04:32:41 +0000136 injector, key, source, scopedFactory, scoping, providerKey));
sberlin92073172011-03-11 00:03:01 +0000137 return true;
limpbizkit477f9f92008-07-28 07:05:14 +0000138 }
139
Sam Berlind51292d2012-02-26 21:23:19 -0500140 @Override
sberlin92073172011-03-11 00:03:01 +0000141 public Boolean visit(LinkedKeyBinding<? extends T> binding) {
sberlin07170cc2011-03-11 03:02:39 +0000142 prepareBinding();
limpbizkit76c24b12008-12-25 04:32:41 +0000143 Key<? extends T> linkedKey = binding.getLinkedKey();
144 if (key.equals(linkedKey)) {
limpbizkit477f9f92008-07-28 07:05:14 +0000145 errors.recursiveBinding();
146 }
147
limpbizkit76c24b12008-12-25 04:32:41 +0000148 FactoryProxy<T> factory = new FactoryProxy<T>(injector, key, linkedKey, source);
sberlin07170cc2011-03-11 03:02:39 +0000149 bindingData.addCreationListener(factory);
limpbizkit0cb8b862009-08-10 23:57:56 +0000150 InternalFactory<? extends T> scopedFactory
151 = Scoping.scope(key, injector, factory, source, scoping);
limpbizkit76c24b12008-12-25 04:32:41 +0000152 putBinding(
153 new LinkedBindingImpl<T>(injector, key, source, scopedFactory, scoping, linkedKey));
sberlin92073172011-03-11 00:03:01 +0000154 return true;
limpbizkit477f9f92008-07-28 07:05:14 +0000155 }
156
Sam Berlind51292d2012-02-26 21:23:19 -0500157 @Override
sberlin07170cc2011-03-11 03:02:39 +0000158 public Boolean visit(UntargettedBinding<? extends T> untargetted) {
159 return false;
limpbizkit477f9f92008-07-28 07:05:14 +0000160 }
161
Sam Berlind51292d2012-02-26 21:23:19 -0500162 @Override
sberlin92073172011-03-11 00:03:01 +0000163 public Boolean visit(ExposedBinding<? extends T> binding) {
limpbizkitaa07ab02009-05-15 07:10:43 +0000164 throw new IllegalArgumentException("Cannot apply a non-module element");
limpbizkitfcbdf992008-11-26 02:37:35 +0000165 }
Sam Berlind51292d2012-02-26 21:23:19 -0500166
167 @Override
sberlin92073172011-03-11 00:03:01 +0000168 public Boolean visit(ConvertedConstantBinding<? extends T> binding) {
limpbizkit477f9f92008-07-28 07:05:14 +0000169 throw new IllegalArgumentException("Cannot apply a non-module element");
170 }
Sam Berlind51292d2012-02-26 21:23:19 -0500171
172 @Override
sberlin92073172011-03-11 00:03:01 +0000173 public Boolean visit(ProviderBinding<? extends T> binding) {
limpbizkit477f9f92008-07-28 07:05:14 +0000174 throw new IllegalArgumentException("Cannot apply a non-module element");
175 }
sberlin07170cc2011-03-11 03:02:39 +0000176
177 @Override
178 protected Boolean visitOther(Binding<? extends T> binding) {
179 throw new IllegalStateException("BindingProcessor should override all visitations");
limpbizkit477f9f92008-07-28 07:05:14 +0000180 }
181 });
limpbizkit477f9f92008-07-28 07:05:14 +0000182 }
183
limpbizkitaa07ab02009-05-15 07:10:43 +0000184 @Override public Boolean visit(PrivateElements privateElements) {
185 for (Key<?> key : privateElements.getExposedKeys()) {
186 bindExposed(privateElements, key);
187 }
188 return false; // leave the private elements for the PrivateElementsProcessor to handle
189 }
190
191 private <T> void bindExposed(PrivateElements privateElements, Key<T> key) {
192 ExposedKeyFactory<T> exposedKeyFactory = new ExposedKeyFactory<T>(key, privateElements);
sberlin07170cc2011-03-11 03:02:39 +0000193 bindingData.addCreationListener(exposedKeyFactory);
limpbizkitaa07ab02009-05-15 07:10:43 +0000194 putBinding(new ExposedBindingImpl<T>(
195 injector, privateElements.getExposedSource(key), key, exposedKeyFactory, privateElements));
196 }
limpbizkit477f9f92008-07-28 07:05:14 +0000197}