blob: 36fb2725c0eb58269f150a9816d0525cc277a75f [file] [log] [blame]
crazyboblee712705c2007-09-07 03:20:30 +00001/*
2 * Copyright (C) 2007 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;
18
crazybobleed71c19e2007-09-08 01:55:10 +000019import com.google.inject.InjectorImpl.SingleParameterInjector;
limpbizkit51411872008-05-13 22:15:29 +000020import com.google.inject.internal.ResolveFailedException;
limpbizkit3b1cd582008-04-28 00:06:01 +000021import com.google.inject.internal.ToStringBuilder;
22import com.google.inject.spi.BindingVisitor;
23import com.google.inject.spi.ClassBinding;
24import com.google.inject.spi.Dependency;
25
crazybobleed71c19e2007-09-08 01:55:10 +000026import java.util.Collection;
crazyboblee712705c2007-09-07 03:20:30 +000027
28/**
29 *
30 *
31 */
32class ClassBindingImpl<T> extends BindingImpl<T>
33 implements ClassBinding<T> {
34
limpbizkit3b1cd582008-04-28 00:06:01 +000035 private final InjectorImpl.LateBoundConstructor<T> lateBoundConstructor;
36
crazyboblee712705c2007-09-07 03:20:30 +000037 ClassBindingImpl(InjectorImpl injector, Key<T> key, Object source,
limpbizkit3b1cd582008-04-28 00:06:01 +000038 InternalFactory<? extends T> internalFactory, Scope scope,
39 InjectorImpl.LateBoundConstructor<T> lateBoundConstructor) {
crazyboblee712705c2007-09-07 03:20:30 +000040 super(injector, key, source, internalFactory, scope);
limpbizkit3b1cd582008-04-28 00:06:01 +000041 this.lateBoundConstructor = lateBoundConstructor;
crazyboblee712705c2007-09-07 03:20:30 +000042 }
43
limpbizkit51411872008-05-13 22:15:29 +000044 @Override void initialize(InjectorImpl injector) throws ResolveFailedException {
45 lateBoundConstructor.bind(injector, getBoundClass());
46 }
47
crazyboblee712705c2007-09-07 03:20:30 +000048 public void accept(BindingVisitor<? super T> visitor) {
49 visitor.visit(this);
50 }
51
52 @SuppressWarnings("unchecked")
53 public Class<T> getBoundClass() {
54 // T should always be the class itself.
55 return (Class<T>) key.getRawType();
56 }
57
crazybobleed71c19e2007-09-08 01:55:10 +000058 public Collection<Dependency<?>> getDependencies() {
limpbizkit3b1cd582008-04-28 00:06:01 +000059 if (lateBoundConstructor == null) {
60 throw new AssertionError();
61 }
62
crazybobleed71c19e2007-09-08 01:55:10 +000063 Class<T> boundClass = getBoundClass();
64 Collection<Dependency<?>> injectors
65 = injector.getModifiableFieldAndMethodDependenciesFor(boundClass);
limpbizkit3b1cd582008-04-28 00:06:01 +000066 ConstructorInjector<T> constructor = lateBoundConstructor.constructorInjector;
crazybobleed71c19e2007-09-08 01:55:10 +000067 if (constructor.parameterInjectors != null) {
68 for (SingleParameterInjector<?> parameterInjector
69 : constructor.parameterInjectors) {
limpbizkitfcf2b8c2007-10-21 18:23:43 +000070 injectors.add(parameterInjector.injectionPoint);
crazybobleed71c19e2007-09-08 01:55:10 +000071 }
72 }
73 return injectors;
74 }
75
limpbizkitc9465f92008-05-31 19:01:54 +000076 @Override public String toString() {
crazyboblee712705c2007-09-07 03:20:30 +000077 return new ToStringBuilder(ClassBinding.class)
78 .add("class", getBoundClass())
79 .add("scope", scope)
80 .add("source", source)
81 .toString();
82 }
83}