crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 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 | 5789ef4 | 2008-08-04 06:44:30 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.Annotations; |
| 20 | import com.google.inject.internal.MoreTypes; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 21 | import static com.google.inject.internal.Preconditions.checkArgument; |
| 22 | import static com.google.inject.internal.Preconditions.checkNotNull; |
limpbizkit | 5789ef4 | 2008-08-04 06:44:30 +0000 | [diff] [blame] | 23 | import com.google.inject.internal.ToStringBuilder; |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 24 | import java.lang.annotation.Annotation; |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 25 | import java.lang.reflect.Type; |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 26 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 27 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 28 | * Binding key consisting of an injection type and an optional annotation. |
| 29 | * Matches the type and annotation at a point of injection. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 30 | * |
crazyboblee | 61257a8 | 2007-03-03 00:23:40 +0000 | [diff] [blame] | 31 | * <p>For example, {@code Key.get(Service.class, Transactional.class)} will |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 32 | * match: |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 33 | * |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 34 | * <pre> |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 35 | * {@literal @}Inject |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 36 | * public void setService({@literal @}Transactional Service service) { |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 37 | * ... |
| 38 | * } |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 39 | * </pre> |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 40 | * |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 41 | * <p>{@code Key} supports generic types via subclassing just like {@link |
| 42 | * TypeLiteral}. |
| 43 | * |
limpbizkit | c0fe03b | 2008-06-02 04:46:53 +0000 | [diff] [blame] | 44 | * <p>Keys do not differentiate between primitive types (int, char, etc.) and |
| 45 | * their correpsonding wrapper types (Integer, Character, etc.). Primitive |
| 46 | * types will be replaced with their wrapper types when keys are created. |
| 47 | * |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 48 | * @author crazybob@google.com (Bob Lee) |
| 49 | */ |
limpbizkit | 6caa8dc | 2009-05-19 00:40:02 +0000 | [diff] [blame] | 50 | public class Key<T> { |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 51 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 52 | private final AnnotationStrategy annotationStrategy; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 53 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 54 | private final TypeLiteral<T> typeLiteral; |
| 55 | private final int hashCode; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 56 | |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Constructs a new key. Derives the type from this class's type parameter. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 59 | * |
| 60 | * <p>Clients create an empty anonymous subclass. Doing so embeds the type |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 61 | * parameter in the anonymous class's type hierarchy so we can reconstitute it |
| 62 | * at runtime despite erasure. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 63 | * |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 64 | * <p>Example usage for a binding of type {@code Foo} annotated with |
| 65 | * {@code @Bar}: |
| 66 | * |
| 67 | * <p>{@code new Key<Foo>(Bar.class) {}}. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 68 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 69 | @SuppressWarnings("unchecked") |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 70 | protected Key(Class<? extends Annotation> annotationType) { |
| 71 | this.annotationStrategy = strategyFor(annotationType); |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 72 | this.typeLiteral = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 73 | this.hashCode = computeHashCode(); |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 74 | } |
| 75 | |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 76 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 77 | * Constructs a new key. Derives the type from this class's type parameter. |
| 78 | * |
| 79 | * <p>Clients create an empty anonymous subclass. Doing so embeds the type |
| 80 | * parameter in the anonymous class's type hierarchy so we can reconstitute it |
| 81 | * at runtime despite erasure. |
| 82 | * |
| 83 | * <p>Example usage for a binding of type {@code Foo} annotated with |
| 84 | * {@code @Bar}: |
| 85 | * |
| 86 | * <p>{@code new Key<Foo>(new Bar()) {}}. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 87 | */ |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 88 | @SuppressWarnings("unchecked") |
| 89 | protected Key(Annotation annotation) { |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 90 | // no usages, not test-covered |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 91 | this.annotationStrategy = strategyFor(annotation); |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 92 | this.typeLiteral = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 93 | this.hashCode = computeHashCode(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Constructs a new key. Derives the type from this class's type parameter. |
| 98 | * |
| 99 | * <p>Clients create an empty anonymous subclass. Doing so embeds the type |
| 100 | * parameter in the anonymous class's type hierarchy so we can reconstitute it |
| 101 | * at runtime despite erasure. |
| 102 | * |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 103 | * <p>Example usage for a binding of type {@code Foo}: |
| 104 | * |
| 105 | * <p>{@code new Key<Foo>() {}}. |
| 106 | */ |
| 107 | @SuppressWarnings("unchecked") |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 108 | protected Key() { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 109 | this.annotationStrategy = NullAnnotationStrategy.INSTANCE; |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 110 | this.typeLiteral = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 111 | this.hashCode = computeHashCode(); |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Unsafe. Constructs a key from a manually specified type. |
| 116 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 117 | @SuppressWarnings("unchecked") |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 118 | private Key(Type type, AnnotationStrategy annotationStrategy) { |
| 119 | this.annotationStrategy = annotationStrategy; |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 120 | this.typeLiteral = MoreTypes.makeKeySafe((TypeLiteral<T>) TypeLiteral.get(type)); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 121 | this.hashCode = computeHashCode(); |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 122 | } |
| 123 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 124 | /** Constructs a key from a manually specified type. */ |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 125 | private Key(TypeLiteral<T> typeLiteral, AnnotationStrategy annotationStrategy) { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 126 | this.annotationStrategy = annotationStrategy; |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 127 | this.typeLiteral = MoreTypes.makeKeySafe(typeLiteral); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 128 | this.hashCode = computeHashCode(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 129 | } |
| 130 | |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 131 | private int computeHashCode() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 132 | return typeLiteral.hashCode() * 31 + annotationStrategy.hashCode(); |
crazyboblee | b944668 | 2006-12-11 07:16:41 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 136 | * Gets the key type. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 137 | */ |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 138 | public final TypeLiteral<T> getTypeLiteral() { |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 139 | return typeLiteral; |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 143 | * Gets the annotation type. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 144 | */ |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 145 | public final Class<? extends Annotation> getAnnotationType() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 146 | return annotationStrategy.getAnnotationType(); |
| 147 | } |
| 148 | |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 149 | /** |
| 150 | * Gets the annotation. |
| 151 | */ |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 152 | public final Annotation getAnnotation() { |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 153 | return annotationStrategy.getAnnotation(); |
| 154 | } |
| 155 | |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 156 | boolean hasAnnotationType() { |
| 157 | return annotationStrategy.getAnnotationType() != null; |
| 158 | } |
| 159 | |
| 160 | String getAnnotationName() { |
| 161 | Annotation annotation = annotationStrategy.getAnnotation(); |
| 162 | if (annotation != null) { |
| 163 | return annotation.toString(); |
| 164 | } |
| 165 | |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 166 | // not test-covered |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 167 | return annotationStrategy.getAnnotationType().toString(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 168 | } |
| 169 | |
crazyboblee | f1ba2b5 | 2007-01-29 21:19:53 +0000 | [diff] [blame] | 170 | Class<? super T> getRawType() { |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 171 | return typeLiteral.getRawType(); |
crazyboblee | d42a05b | 2006-12-07 01:00:33 +0000 | [diff] [blame] | 172 | } |
| 173 | |
crazyboblee | 552472f | 2007-09-07 16:52:39 +0000 | [diff] [blame] | 174 | /** |
| 175 | * Gets the key of this key's provider. |
| 176 | */ |
| 177 | Key<Provider<T>> providerKey() { |
| 178 | return ofType(typeLiteral.providerType()); |
| 179 | } |
| 180 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 181 | @Override public final boolean equals(Object o) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 182 | if (o == this) { |
| 183 | return true; |
| 184 | } |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 185 | if (!(o instanceof Key<?>)) { |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 186 | return false; |
| 187 | } |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 188 | Key<?> other = (Key<?>) o; |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 189 | return annotationStrategy.equals(other.annotationStrategy) |
| 190 | && typeLiteral.equals(other.typeLiteral); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 191 | } |
| 192 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 193 | @Override public final int hashCode() { |
| 194 | return this.hashCode; |
| 195 | } |
| 196 | |
| 197 | @Override public final String toString() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 198 | return new ToStringBuilder(Key.class) |
| 199 | .add("type", typeLiteral) |
| 200 | .add("annotation", annotationStrategy) |
| 201 | .toString(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 202 | } |
| 203 | |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 204 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 205 | * Gets a key for an injection type and an annotation strategy. |
| 206 | */ |
| 207 | static <T> Key<T> get(Class<T> type, |
| 208 | AnnotationStrategy annotationStrategy) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 209 | return new Key<T>(type, annotationStrategy); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Gets a key for an injection type. |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 214 | */ |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 215 | public static <T> Key<T> get(Class<T> type) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 216 | return new Key<T>(type, NullAnnotationStrategy.INSTANCE); |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 220 | * Gets a key for an injection type and an annotation type. |
| 221 | */ |
| 222 | public static <T> Key<T> get(Class<T> type, |
| 223 | Class<? extends Annotation> annotationType) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 224 | return new Key<T>(type, strategyFor(annotationType)); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 228 | * Gets a key for an injection type and an annotation. |
| 229 | */ |
| 230 | public static <T> Key<T> get(Class<T> type, Annotation annotation) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 231 | return new Key<T>(type, strategyFor(annotation)); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Gets a key for an injection type. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 236 | */ |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 237 | public static Key<?> get(Type type) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 238 | return new Key<Object>(type, NullAnnotationStrategy.INSTANCE); |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 242 | * Gets a key for an injection type and an annotation type. |
| 243 | */ |
| 244 | public static Key<?> get(Type type, |
| 245 | Class<? extends Annotation> annotationType) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 246 | return new Key<Object>(type, strategyFor(annotationType)); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Gets a key for an injection type and an annotation. |
| 251 | */ |
| 252 | public static Key<?> get(Type type, Annotation annotation) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 253 | return new Key<Object>(type, strategyFor(annotation)); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 257 | * Gets a key for an injection type. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 258 | */ |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 259 | public static <T> Key<T> get(TypeLiteral<T> typeLiteral) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 260 | return new Key<T>(typeLiteral, NullAnnotationStrategy.INSTANCE); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 264 | * Gets a key for an injection type and an annotation type. |
| 265 | */ |
| 266 | public static <T> Key<T> get(TypeLiteral<T> typeLiteral, |
| 267 | Class<? extends Annotation> annotationType) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 268 | return new Key<T>(typeLiteral, strategyFor(annotationType)); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Gets a key for an injection type and an annotation. |
| 273 | */ |
| 274 | public static <T> Key<T> get(TypeLiteral<T> typeLiteral, |
| 275 | Annotation annotation) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 276 | return new Key<T>(typeLiteral, strategyFor(annotation)); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 280 | * Returns a new key of the specified type with the same annotation as this |
| 281 | * key. |
| 282 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 283 | public <T> Key<T> ofType(Class<T> type) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 284 | return new Key<T>(type, annotationStrategy); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Returns a new key of the specified type with the same annotation as this |
| 289 | * key. |
| 290 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 291 | public Key<?> ofType(Type type) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 292 | return new Key<Object>(type, annotationStrategy); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 293 | } |
| 294 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 295 | /** |
crazyboblee | 552472f | 2007-09-07 16:52:39 +0000 | [diff] [blame] | 296 | * Returns a new key of the specified type with the same annotation as this |
| 297 | * key. |
| 298 | */ |
| 299 | <T> Key<T> ofType(TypeLiteral<T> type) { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 300 | return new Key<T>(type, annotationStrategy); |
crazyboblee | 552472f | 2007-09-07 16:52:39 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /** |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 304 | * Returns true if this key has annotation attributes. |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 305 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 306 | public boolean hasAttributes() { |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 307 | return annotationStrategy.hasAttributes(); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Returns this key without annotation attributes, i.e. with only the |
| 312 | * annotation type. |
| 313 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 314 | public Key<T> withoutAttributes() { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 315 | return new Key<T>(typeLiteral, annotationStrategy.withoutAttributes()); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 316 | } |
| 317 | |
limpbizkit | 6caa8dc | 2009-05-19 00:40:02 +0000 | [diff] [blame] | 318 | interface AnnotationStrategy { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 319 | Annotation getAnnotation(); |
| 320 | Class<? extends Annotation> getAnnotationType(); |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 321 | boolean hasAttributes(); |
| 322 | AnnotationStrategy withoutAttributes(); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 323 | } |
| 324 | |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 325 | /** |
| 326 | * Returns {@code true} if the given annotation type has no attributes. |
| 327 | */ |
| 328 | static boolean isMarker(Class<? extends Annotation> annotationType) { |
| 329 | return annotationType.getDeclaredMethods().length == 0; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Gets the strategy for an annotation. |
| 334 | */ |
| 335 | static AnnotationStrategy strategyFor(Annotation annotation) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 336 | checkNotNull(annotation, "annotation"); |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 337 | Class<? extends Annotation> annotationType = annotation.annotationType(); |
| 338 | ensureRetainedAtRuntime(annotationType); |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 339 | ensureIsBindingAnnotation(annotationType); |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 340 | |
| 341 | if (annotationType.getDeclaredMethods().length == 0) { |
| 342 | return new AnnotationTypeStrategy(annotationType, annotation); |
| 343 | } |
| 344 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 345 | return new AnnotationInstanceStrategy(annotation); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Gets the strategy for an annotation type. |
| 350 | */ |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 351 | static AnnotationStrategy strategyFor(Class<? extends Annotation> annotationType) { |
| 352 | checkNotNull(annotationType, "annotation type"); |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 353 | ensureRetainedAtRuntime(annotationType); |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 354 | ensureIsBindingAnnotation(annotationType); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 355 | return new AnnotationTypeStrategy(annotationType, null); |
| 356 | } |
| 357 | |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 358 | private static void ensureRetainedAtRuntime( |
| 359 | Class<? extends Annotation> annotationType) { |
limpbizkit | 5789ef4 | 2008-08-04 06:44:30 +0000 | [diff] [blame] | 360 | checkArgument(Annotations.isRetainedAtRuntime(annotationType), |
| 361 | "%s is not retained at runtime. Please annotate it with @Retention(RUNTIME).", |
| 362 | annotationType.getName()); |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 363 | } |
| 364 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 365 | private static void ensureIsBindingAnnotation( |
| 366 | Class<? extends Annotation> annotationType) { |
limpbizkit | 5789ef4 | 2008-08-04 06:44:30 +0000 | [diff] [blame] | 367 | checkArgument(isBindingAnnotation(annotationType), |
| 368 | "%s is not a binding annotation. Please annotate it with @BindingAnnotation.", |
| 369 | annotationType.getName()); |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 370 | } |
| 371 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 372 | static enum NullAnnotationStrategy implements AnnotationStrategy { |
| 373 | INSTANCE; |
| 374 | |
| 375 | public boolean hasAttributes() { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | public AnnotationStrategy withoutAttributes() { |
| 380 | throw new UnsupportedOperationException("Key already has no attributes."); |
| 381 | } |
| 382 | |
| 383 | public Annotation getAnnotation() { |
| 384 | return null; |
| 385 | } |
| 386 | |
| 387 | public Class<? extends Annotation> getAnnotationType() { |
| 388 | return null; |
| 389 | } |
| 390 | |
| 391 | @Override public String toString() { |
| 392 | return "[none]"; |
| 393 | } |
| 394 | } |
| 395 | |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 396 | // this class not test-covered |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 397 | static class AnnotationInstanceStrategy implements AnnotationStrategy { |
| 398 | |
| 399 | final Annotation annotation; |
| 400 | |
| 401 | AnnotationInstanceStrategy(Annotation annotation) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 402 | this.annotation = checkNotNull(annotation, "annotation"); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 403 | } |
| 404 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 405 | public boolean hasAttributes() { |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | public AnnotationStrategy withoutAttributes() { |
| 410 | return new AnnotationTypeStrategy(getAnnotationType(), annotation); |
| 411 | } |
| 412 | |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 413 | public Annotation getAnnotation() { |
| 414 | return annotation; |
| 415 | } |
| 416 | |
| 417 | public Class<? extends Annotation> getAnnotationType() { |
| 418 | return annotation.annotationType(); |
| 419 | } |
| 420 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 421 | @Override public boolean equals(Object o) { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 422 | if (!(o instanceof AnnotationInstanceStrategy)) { |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | AnnotationInstanceStrategy other = (AnnotationInstanceStrategy) o; |
| 427 | return annotation.equals(other.annotation); |
| 428 | } |
| 429 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 430 | @Override public int hashCode() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 431 | return annotation.hashCode(); |
| 432 | } |
| 433 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 434 | @Override public String toString() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 435 | return annotation.toString(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | static class AnnotationTypeStrategy implements AnnotationStrategy { |
| 440 | |
| 441 | final Class<? extends Annotation> annotationType; |
| 442 | |
| 443 | // Keep the instance around if we have it so the client can request it. |
| 444 | final Annotation annotation; |
| 445 | |
| 446 | AnnotationTypeStrategy(Class<? extends Annotation> annotationType, |
| 447 | Annotation annotation) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 448 | this.annotationType = checkNotNull(annotationType, "annotation type"); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 449 | this.annotation = annotation; |
| 450 | } |
| 451 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 452 | public boolean hasAttributes() { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | public AnnotationStrategy withoutAttributes() { |
| 457 | throw new UnsupportedOperationException("Key already has no attributes."); |
| 458 | } |
| 459 | |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 460 | public Annotation getAnnotation() { |
| 461 | return annotation; |
| 462 | } |
| 463 | |
| 464 | public Class<? extends Annotation> getAnnotationType() { |
| 465 | return annotationType; |
| 466 | } |
| 467 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 468 | @Override public boolean equals(Object o) { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 469 | if (!(o instanceof AnnotationTypeStrategy)) { |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | AnnotationTypeStrategy other = (AnnotationTypeStrategy) o; |
| 474 | return annotationType.equals(other.annotationType); |
| 475 | } |
| 476 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 477 | @Override public int hashCode() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 478 | return annotationType.hashCode(); |
| 479 | } |
| 480 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame] | 481 | @Override public String toString() { |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 482 | return "@" + annotationType.getName(); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 483 | } |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 484 | } |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 485 | |
| 486 | static boolean isBindingAnnotation(Annotation annotation) { |
| 487 | return isBindingAnnotation(annotation.annotationType()); |
| 488 | } |
| 489 | |
| 490 | static boolean isBindingAnnotation( |
| 491 | Class<? extends Annotation> annotationType) { |
| 492 | return annotationType.isAnnotationPresent(BindingAnnotation.class); |
| 493 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 494 | } |