limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.google.inject; |
| 18 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.inject.InjectorImpl.SingleMemberInjector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 21 | import com.google.inject.commands.RequestStaticInjectionCommand; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 22 | import com.google.inject.internal.Errors; |
| 23 | import com.google.inject.internal.ResolveFailedException; |
| 24 | import com.google.inject.spi.SourceProviders; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Handles {@link Binder#requestStaticInjection} commands. |
| 29 | * |
| 30 | * @author crazybob@google.com (Bob Lee) |
| 31 | * @author jessewilson@google.com (Jesse Wilson) |
| 32 | */ |
| 33 | class RequestStaticInjectionCommandProcessor extends CommandProcessor { |
| 34 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 35 | private final List<StaticInjection> staticInjections = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 36 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 37 | RequestStaticInjectionCommandProcessor(Errors errors) { |
| 38 | super(errors); |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 39 | } |
| 40 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 41 | @Override public Boolean visitRequestStaticInjection(RequestStaticInjectionCommand command) { |
| 42 | for (Class<?> type : command.getTypes()) { |
| 43 | staticInjections.add(new StaticInjection(command.getSource(), type)); |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | public void validate(InjectorImpl injector) { |
| 49 | for (StaticInjection staticInjection : staticInjections) { |
| 50 | staticInjection.validate(injector); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public void injectMembers(InjectorImpl injector) { |
| 55 | for (StaticInjection staticInjection : staticInjections) { |
| 56 | staticInjection.injectMembers(injector); |
| 57 | } |
| 58 | } |
| 59 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 60 | /** A requested static injection. */ |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 61 | private class StaticInjection { |
| 62 | final Object source; |
| 63 | final Class<?> type; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 64 | final List<SingleMemberInjector> memberInjectors = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 65 | |
| 66 | public StaticInjection(Object source, Class type) { |
| 67 | this.source = source; |
| 68 | this.type = type; |
| 69 | } |
| 70 | |
| 71 | void validate(final InjectorImpl injector) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 72 | SourceProviders.withDefault(source, new Runnable() { |
| 73 | public void run() { |
| 74 | injector.addSingleInjectorsForFields( |
| 75 | type.getDeclaredFields(), true, memberInjectors, errors); |
| 76 | injector.addSingleInjectorsForMethods( |
| 77 | type.getDeclaredMethods(), true, memberInjectors, errors); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 78 | } |
| 79 | }); |
| 80 | } |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 81 | |
| 82 | void injectMembers(InjectorImpl injector) { |
| 83 | try { |
| 84 | injector.callInContext(new ContextualCallable<Void>() { |
| 85 | public Void call(InternalContext context) { |
| 86 | for (SingleMemberInjector injector : memberInjectors) { |
| 87 | injector.inject(errors, context, null); |
| 88 | } |
| 89 | return null; |
| 90 | } |
| 91 | }); |
| 92 | } catch (ResolveFailedException e) { |
| 93 | throw new AssertionError(); |
| 94 | } |
| 95 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 96 | } |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame^] | 97 | } |