crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.google.inject; |
| 18 | |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 19 | import com.google.inject.InjectorImpl.SingleParameterInjector; |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.ResolveFailedException; |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.ToStringBuilder; |
| 22 | import com.google.inject.spi.BindingVisitor; |
| 23 | import com.google.inject.spi.ClassBinding; |
| 24 | import com.google.inject.spi.Dependency; |
| 25 | |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 26 | import java.util.Collection; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * |
| 30 | * |
| 31 | */ |
| 32 | class ClassBindingImpl<T> extends BindingImpl<T> |
| 33 | implements ClassBinding<T> { |
| 34 | |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 35 | private final InjectorImpl.LateBoundConstructor<T> lateBoundConstructor; |
| 36 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 37 | ClassBindingImpl(InjectorImpl injector, Key<T> key, Object source, |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 38 | InternalFactory<? extends T> internalFactory, Scope scope, |
| 39 | InjectorImpl.LateBoundConstructor<T> lateBoundConstructor) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 40 | super(injector, key, source, internalFactory, scope); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 41 | this.lateBoundConstructor = lateBoundConstructor; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 42 | } |
| 43 | |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 44 | @Override void initialize(InjectorImpl injector) throws ResolveFailedException { |
| 45 | lateBoundConstructor.bind(injector, getBoundClass()); |
| 46 | } |
| 47 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 48 | 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 | |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 58 | public Collection<Dependency<?>> getDependencies() { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 59 | if (lateBoundConstructor == null) { |
| 60 | throw new AssertionError(); |
| 61 | } |
| 62 | |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 63 | Class<T> boundClass = getBoundClass(); |
| 64 | Collection<Dependency<?>> injectors |
| 65 | = injector.getModifiableFieldAndMethodDependenciesFor(boundClass); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 66 | ConstructorInjector<T> constructor = lateBoundConstructor.constructorInjector; |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 67 | if (constructor.parameterInjectors != null) { |
| 68 | for (SingleParameterInjector<?> parameterInjector |
| 69 | : constructor.parameterInjectors) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 70 | injectors.add(parameterInjector.injectionPoint); |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | return injectors; |
| 74 | } |
| 75 | |
limpbizkit | c9465f9 | 2008-05-31 19:01:54 +0000 | [diff] [blame^] | 76 | @Override public String toString() { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 77 | return new ToStringBuilder(ClassBinding.class) |
| 78 | .add("class", getBoundClass()) |
| 79 | .add("scope", scope) |
| 80 | .add("source", source) |
| 81 | .toString(); |
| 82 | } |
| 83 | } |