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; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 23 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Handles {@link Binder#requestStaticInjection} commands. |
| 28 | * |
| 29 | * @author crazybob@google.com (Bob Lee) |
| 30 | * @author jessewilson@google.com (Jesse Wilson) |
| 31 | */ |
| 32 | class RequestStaticInjectionCommandProcessor extends CommandProcessor { |
| 33 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 34 | private final List<StaticInjection> staticInjections = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 35 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 36 | RequestStaticInjectionCommandProcessor(Errors errors) { |
| 37 | super(errors); |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 38 | } |
| 39 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 40 | @Override public Boolean visitRequestStaticInjection(RequestStaticInjectionCommand command) { |
| 41 | for (Class<?> type : command.getTypes()) { |
| 42 | staticInjections.add(new StaticInjection(command.getSource(), type)); |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | public void validate(InjectorImpl injector) { |
| 48 | for (StaticInjection staticInjection : staticInjections) { |
| 49 | staticInjection.validate(injector); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public void injectMembers(InjectorImpl injector) { |
| 54 | for (StaticInjection staticInjection : staticInjections) { |
| 55 | staticInjection.injectMembers(injector); |
| 56 | } |
| 57 | } |
| 58 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 59 | /** A requested static injection. */ |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 60 | private class StaticInjection { |
| 61 | final Object source; |
| 62 | final Class<?> type; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 63 | final List<SingleMemberInjector> memberInjectors = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 64 | |
| 65 | public StaticInjection(Object source, Class type) { |
| 66 | this.source = source; |
| 67 | this.type = type; |
| 68 | } |
| 69 | |
| 70 | void validate(final InjectorImpl injector) { |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 71 | errors.pushSource(source); |
| 72 | try { |
| 73 | injector.addSingleInjectorsForFields( |
| 74 | type.getDeclaredFields(), true, memberInjectors, errors); |
| 75 | injector.addSingleInjectorsForMethods( |
| 76 | type.getDeclaredMethods(), true, memberInjectors, errors); |
| 77 | } finally { |
| 78 | errors.popSource(source); |
| 79 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 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 | }); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 92 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 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 | } |