blob: 9eb27568b95694b798530e92b3d365a5bc120989 [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;
limpbizkit3b1cd582008-04-28 00:06:01 +000020import com.google.inject.internal.ToStringBuilder;
21import com.google.inject.spi.BindingVisitor;
22import com.google.inject.spi.ClassBinding;
23import com.google.inject.spi.Dependency;
24
crazybobleed71c19e2007-09-08 01:55:10 +000025import java.util.Collection;
crazyboblee712705c2007-09-07 03:20:30 +000026
27/**
28 *
29 *
30 */
31class ClassBindingImpl<T> extends BindingImpl<T>
32 implements ClassBinding<T> {
33
limpbizkit3b1cd582008-04-28 00:06:01 +000034 private final InjectorImpl.LateBoundConstructor<T> lateBoundConstructor;
35
crazyboblee712705c2007-09-07 03:20:30 +000036 ClassBindingImpl(InjectorImpl injector, Key<T> key, Object source,
limpbizkit3b1cd582008-04-28 00:06:01 +000037 InternalFactory<? extends T> internalFactory, Scope scope,
38 InjectorImpl.LateBoundConstructor<T> lateBoundConstructor) {
crazyboblee712705c2007-09-07 03:20:30 +000039 super(injector, key, source, internalFactory, scope);
limpbizkit3b1cd582008-04-28 00:06:01 +000040 this.lateBoundConstructor = lateBoundConstructor;
crazyboblee712705c2007-09-07 03:20:30 +000041 }
42
43 public void accept(BindingVisitor<? super T> visitor) {
44 visitor.visit(this);
45 }
46
47 @SuppressWarnings("unchecked")
48 public Class<T> getBoundClass() {
49 // T should always be the class itself.
50 return (Class<T>) key.getRawType();
51 }
52
crazybobleed71c19e2007-09-08 01:55:10 +000053 public Collection<Dependency<?>> getDependencies() {
limpbizkit3b1cd582008-04-28 00:06:01 +000054 if (lateBoundConstructor == null) {
55 throw new AssertionError();
56 }
57
crazybobleed71c19e2007-09-08 01:55:10 +000058 Class<T> boundClass = getBoundClass();
59 Collection<Dependency<?>> injectors
60 = injector.getModifiableFieldAndMethodDependenciesFor(boundClass);
limpbizkit3b1cd582008-04-28 00:06:01 +000061 ConstructorInjector<T> constructor = lateBoundConstructor.constructorInjector;
crazybobleed71c19e2007-09-08 01:55:10 +000062 if (constructor.parameterInjectors != null) {
63 for (SingleParameterInjector<?> parameterInjector
64 : constructor.parameterInjectors) {
limpbizkitfcf2b8c2007-10-21 18:23:43 +000065 injectors.add(parameterInjector.injectionPoint);
crazybobleed71c19e2007-09-08 01:55:10 +000066 }
67 }
68 return injectors;
69 }
70
crazyboblee712705c2007-09-07 03:20:30 +000071 @Override
72 public String toString() {
73 return new ToStringBuilder(ClassBinding.class)
74 .add("class", getBoundClass())
75 .add("scope", scope)
76 .add("source", source)
77 .toString();
78 }
79}