limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +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 | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.BindingImpl; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.Errors; |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.ImmutableList; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 22 | import com.google.inject.internal.Lists; |
| 23 | import com.google.inject.internal.Maps; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 24 | import com.google.inject.internal.MatcherAndConverter; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 25 | import static com.google.inject.internal.Preconditions.checkNotNull; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 26 | import com.google.inject.spi.TypeListenerBinding; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 27 | import java.lang.annotation.Annotation; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 28 | import java.util.ArrayList; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 29 | import java.util.Collections; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.Map; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * @author jessewilson@google.com (Jesse Wilson) |
| 35 | */ |
| 36 | class InheritingState implements State { |
| 37 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 38 | private final State parent; |
dhanji | 5c1ab7f | 2009-01-04 07:17:25 +0000 | [diff] [blame] | 39 | |
| 40 | // Must be a linked hashmap in order to preserve order of bindings in Modules. |
| 41 | private final Map<Key<?>, Binding<?>> explicitBindingsMutable = Maps.newLinkedHashMap(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 42 | private final Map<Key<?>, Binding<?>> explicitBindings |
| 43 | = Collections.unmodifiableMap(explicitBindingsMutable); |
| 44 | private final Map<Class<? extends Annotation>, Scope> scopes = Maps.newHashMap(); |
| 45 | private final List<MatcherAndConverter> converters = Lists.newArrayList(); |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 46 | /*if[AOP]*/ |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 47 | private final List<MethodAspect> methodAspects = Lists.newArrayList(); |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 48 | /*end[AOP]*/ |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 49 | private final List<TypeListenerBinding> listenerBindings = Lists.newArrayList(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 50 | private final WeakKeySet blacklistedKeys = new WeakKeySet(); |
limpbizkit | a8dccb3 | 2008-11-11 22:41:56 +0000 | [diff] [blame] | 51 | private final Object lock; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 52 | |
| 53 | InheritingState(State parent) { |
| 54 | this.parent = checkNotNull(parent, "parent"); |
limpbizkit | a8dccb3 | 2008-11-11 22:41:56 +0000 | [diff] [blame] | 55 | this.lock = (parent == State.NONE) ? this : parent.lock(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public State parent() { |
| 59 | return parent; |
| 60 | } |
| 61 | |
| 62 | @SuppressWarnings("unchecked") // we only put in BindingImpls that match their key types |
| 63 | public <T> BindingImpl<T> getExplicitBinding(Key<T> key) { |
| 64 | Binding<?> binding = explicitBindings.get(key); |
| 65 | return binding != null ? (BindingImpl<T>) binding : parent.getExplicitBinding(key); |
| 66 | } |
| 67 | |
| 68 | public Map<Key<?>, Binding<?>> getExplicitBindingsThisLevel() { |
| 69 | return explicitBindings; |
| 70 | } |
| 71 | |
| 72 | public void putBinding(Key<?> key, BindingImpl<?> binding) { |
| 73 | explicitBindingsMutable.put(key, binding); |
| 74 | } |
| 75 | |
| 76 | public Scope getScope(Class<? extends Annotation> annotationType) { |
| 77 | Scope scope = scopes.get(annotationType); |
| 78 | return scope != null ? scope : parent.getScope(annotationType); |
| 79 | } |
| 80 | |
| 81 | public void putAnnotation(Class<? extends Annotation> annotationType, Scope scope) { |
| 82 | scopes.put(annotationType, scope); |
| 83 | } |
| 84 | |
| 85 | public Iterable<MatcherAndConverter> getConvertersThisLevel() { |
| 86 | return converters; |
| 87 | } |
| 88 | |
| 89 | public void addConverter(MatcherAndConverter matcherAndConverter) { |
| 90 | converters.add(matcherAndConverter); |
| 91 | } |
| 92 | |
| 93 | public MatcherAndConverter getConverter( |
| 94 | String stringValue, TypeLiteral<?> type, Errors errors, Object source) { |
| 95 | MatcherAndConverter matchingConverter = null; |
| 96 | for (State s = this; s != State.NONE; s = s.parent()) { |
| 97 | for (MatcherAndConverter converter : s.getConvertersThisLevel()) { |
| 98 | if (converter.getTypeMatcher().matches(type)) { |
| 99 | if (matchingConverter != null) { |
| 100 | errors.ambiguousTypeConversion(stringValue, source, type, matchingConverter, converter); |
| 101 | } |
| 102 | matchingConverter = converter; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | return matchingConverter; |
| 107 | } |
| 108 | |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 109 | /*if[AOP]*/ |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 110 | public void addMethodAspect(MethodAspect methodAspect) { |
| 111 | methodAspects.add(methodAspect); |
| 112 | } |
| 113 | |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 114 | public ImmutableList<MethodAspect> getMethodAspects() { |
| 115 | return new ImmutableList.Builder<MethodAspect>() |
| 116 | .addAll(parent.getMethodAspects()) |
| 117 | .addAll(methodAspects) |
| 118 | .build(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 119 | } |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 120 | /*end[AOP]*/ |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 121 | |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 122 | public void addTypeListener(TypeListenerBinding listenerBinding) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 123 | listenerBindings.add(listenerBinding); |
| 124 | } |
| 125 | |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 126 | public List<TypeListenerBinding> getTypeListenerBindings() { |
| 127 | List<TypeListenerBinding> parentBindings = parent.getTypeListenerBindings(); |
| 128 | List<TypeListenerBinding> result |
| 129 | = new ArrayList<TypeListenerBinding>(parentBindings.size() + 1); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 130 | result.addAll(parentBindings); |
| 131 | result.addAll(listenerBindings); |
| 132 | return result; |
| 133 | } |
| 134 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 135 | public void blacklist(Key<?> key) { |
| 136 | parent.blacklist(key); |
| 137 | blacklistedKeys.add(key); |
| 138 | } |
| 139 | |
| 140 | public boolean isBlacklisted(Key<?> key) { |
| 141 | return blacklistedKeys.contains(key); |
| 142 | } |
limpbizkit | a8dccb3 | 2008-11-11 22:41:56 +0000 | [diff] [blame] | 143 | |
| 144 | public Object lock() { |
| 145 | return lock; |
| 146 | } |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 147 | } |