1/**
2 * Copyright (C) 2011 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
17package com.google.inject.internal;
18
19import com.google.inject.Binding;
20import com.google.inject.spi.UntargettedBinding;
21
22/**
23 * Processes just UntargettedBindings.
24 *
25 * @author sameb@google.com (Sam Berlin)
26 */
27class UntargettedBindingProcessor extends AbstractBindingProcessor {
28
29  UntargettedBindingProcessor(Errors errors, ProcessedBindingData bindingData) {
30    super(errors, bindingData);
31  }
32
33  @Override
34  public <T> Boolean visit(Binding<T> binding) {
35    return binding.acceptTargetVisitor(new Processor<T, Boolean>((BindingImpl<T>)binding) {
36      public Boolean visit(UntargettedBinding<? extends T> untargetted) {
37        prepareBinding();
38
39        // Error: Missing implementation.
40        // Example: bind(Date.class).annotatedWith(Red.class);
41        // We can't assume abstract types aren't injectable. They may have an
42        // @ImplementedBy annotation or something.
43        if (key.getAnnotationType() != null) {
44          errors.missingImplementation(key);
45          putBinding(invalidBinding(injector, key, source));
46          return true;
47        }
48
49        // This cast is safe after the preceeding check.
50        try {
51          BindingImpl<T> binding = injector.createUninitializedBinding(
52              key, scoping, source, errors, false);
53          scheduleInitialization(binding);
54          putBinding(binding);
55        } catch (ErrorsException e) {
56          errors.merge(e.getErrors());
57          putBinding(invalidBinding(injector, key, source));
58        }
59
60        return true;
61      }
62
63      @Override
64      protected Boolean visitOther(Binding<? extends T> binding) {
65        return false;
66      }
67    });
68  }
69}
70