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 | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.Annotations; |
kevinb9n | cad2c2b | 2007-05-15 17:28:03 +0000 | [diff] [blame] | 20 | import static com.google.inject.internal.Objects.nonNull; |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.ToStringBuilder; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 22 | |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 23 | import java.io.Serializable; |
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 | * |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 44 | * @author crazybob@google.com (Bob Lee) |
| 45 | */ |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 46 | public class Key<T> implements Serializable { |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 47 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 48 | private final AnnotationStrategy annotationStrategy; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 49 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 50 | private final TypeLiteral<T> typeLiteral; |
| 51 | private final int hashCode; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 52 | |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Constructs a new key. Derives the type from this class's type parameter. |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 55 | * |
| 56 | * <p>Clients create an empty anonymous subclass. Doing so embeds the type |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 57 | * parameter in the anonymous class's type hierarchy so we can reconstitute it |
| 58 | * at runtime despite erasure. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 59 | * |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 60 | * <p>Example usage for a binding of type {@code Foo} annotated with |
| 61 | * {@code @Bar}: |
| 62 | * |
| 63 | * <p>{@code new Key<Foo>(Bar.class) {}}. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 64 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 65 | @SuppressWarnings("unchecked") |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 66 | protected Key(Class<? extends Annotation> annotationType) { |
| 67 | this.annotationStrategy = strategyFor(annotationType); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 68 | this.typeLiteral |
| 69 | = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 70 | this.hashCode = computeHashCode(); |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 71 | } |
| 72 | |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 73 | /** |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 74 | * Constructs a new key. Derives the type from this class's type parameter. |
| 75 | * |
| 76 | * <p>Clients create an empty anonymous subclass. Doing so embeds the type |
| 77 | * parameter in the anonymous class's type hierarchy so we can reconstitute it |
| 78 | * at runtime despite erasure. |
| 79 | * |
| 80 | * <p>Example usage for a binding of type {@code Foo} annotated with |
| 81 | * {@code @Bar}: |
| 82 | * |
| 83 | * <p>{@code new Key<Foo>(new Bar()) {}}. |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 84 | */ |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 85 | @SuppressWarnings("unchecked") |
| 86 | protected Key(Annotation annotation) { |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 87 | // no usages, not test-covered |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 88 | this.annotationStrategy = strategyFor(annotation); |
| 89 | this.typeLiteral |
| 90 | = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()); |
| 91 | this.hashCode = computeHashCode(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Constructs a new key. Derives the type from this class's type parameter. |
| 96 | * |
| 97 | * <p>Clients create an empty anonymous subclass. Doing so embeds the type |
| 98 | * parameter in the anonymous class's type hierarchy so we can reconstitute it |
| 99 | * at runtime despite erasure. |
| 100 | * |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 101 | * <p>Example usage for a binding of type {@code Foo}: |
| 102 | * |
| 103 | * <p>{@code new Key<Foo>() {}}. |
| 104 | */ |
| 105 | @SuppressWarnings("unchecked") |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 106 | protected Key() { |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 107 | this.annotationStrategy = NullAnnotationStrategy.INSTANCE; |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 108 | this.typeLiteral |
| 109 | = (TypeLiteral<T>) TypeLiteral.fromSuperclassTypeParameter(getClass()); |
| 110 | this.hashCode = computeHashCode(); |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 111 | } |
| 112 | |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 113 | /** |
| 114 | * Unsafe. Constructs a key from a manually specified type. |
| 115 | */ |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 116 | @SuppressWarnings("unchecked") |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 117 | private Key(Type type, AnnotationStrategy annotationStrategy) { |
| 118 | this.annotationStrategy = annotationStrategy; |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 119 | this.typeLiteral = (TypeLiteral<T>) TypeLiteral.get(type); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 120 | this.hashCode = computeHashCode(); |
crazyboblee | 6ab7e1f | 2006-12-02 00:20:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 123 | /** Constructs a key from a manually specified type. */ |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 124 | private Key(TypeLiteral<T> typeLiteral, |
| 125 | AnnotationStrategy annotationStrategy) { |
| 126 | this.annotationStrategy = annotationStrategy; |
crazyboblee | 0baa9fc | 2007-01-31 02:38:54 +0000 | [diff] [blame] | 127 | this.typeLiteral = 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 | */ |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 283 | <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 | */ |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 291 | 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 | */ |
| 306 | boolean hasAttributes() { |
| 307 | return annotationStrategy.hasAttributes(); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Returns this key without annotation attributes, i.e. with only the |
| 312 | * annotation type. |
| 313 | */ |
| 314 | 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 | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 318 | interface AnnotationStrategy extends Serializable { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 319 | |
| 320 | Annotation getAnnotation(); |
| 321 | Class<? extends Annotation> getAnnotationType(); |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 322 | boolean hasAttributes(); |
| 323 | AnnotationStrategy withoutAttributes(); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 324 | } |
| 325 | |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 326 | /** |
| 327 | * Returns {@code true} if the given annotation type has no attributes. |
| 328 | */ |
| 329 | static boolean isMarker(Class<? extends Annotation> annotationType) { |
| 330 | return annotationType.getDeclaredMethods().length == 0; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Gets the strategy for an annotation. |
| 335 | */ |
| 336 | static AnnotationStrategy strategyFor(Annotation annotation) { |
| 337 | nonNull(annotation, "annotation"); |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 338 | Class<? extends Annotation> annotationType = annotation.annotationType(); |
| 339 | ensureRetainedAtRuntime(annotationType); |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 340 | ensureIsBindingAnnotation(annotationType); |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 341 | |
| 342 | if (annotationType.getDeclaredMethods().length == 0) { |
| 343 | return new AnnotationTypeStrategy(annotationType, annotation); |
| 344 | } |
| 345 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 346 | return new AnnotationInstanceStrategy(annotation); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Gets the strategy for an annotation type. |
| 351 | */ |
| 352 | static AnnotationStrategy strategyFor( |
| 353 | Class<? extends Annotation> annotationType) { |
| 354 | nonNull(annotationType, "annotation type"); |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 355 | ensureRetainedAtRuntime(annotationType); |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 356 | ensureIsBindingAnnotation(annotationType); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 357 | return new AnnotationTypeStrategy(annotationType, null); |
| 358 | } |
| 359 | |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 360 | private static void ensureRetainedAtRuntime( |
| 361 | Class<? extends Annotation> annotationType) { |
| 362 | if (!Annotations.isRetainedAtRuntime(annotationType)) { |
| 363 | throw new IllegalArgumentException(annotationType.getName() |
| 364 | + " is not retained at runtime." |
| 365 | + " Please annotate it with @Retention(RUNTIME)."); |
| 366 | } |
| 367 | } |
| 368 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 369 | private static void ensureIsBindingAnnotation( |
| 370 | Class<? extends Annotation> annotationType) { |
| 371 | if (!isBindingAnnotation(annotationType)) { |
| 372 | throw new IllegalArgumentException(annotationType.getName() |
| 373 | + " is not a binding annotation." |
| 374 | + " Please annotate it with @BindingAnnotation."); |
| 375 | } |
| 376 | } |
| 377 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 378 | static enum NullAnnotationStrategy implements AnnotationStrategy { |
| 379 | INSTANCE; |
| 380 | |
| 381 | public boolean hasAttributes() { |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | public AnnotationStrategy withoutAttributes() { |
| 386 | throw new UnsupportedOperationException("Key already has no attributes."); |
| 387 | } |
| 388 | |
| 389 | public Annotation getAnnotation() { |
| 390 | return null; |
| 391 | } |
| 392 | |
| 393 | public Class<? extends Annotation> getAnnotationType() { |
| 394 | return null; |
| 395 | } |
| 396 | |
| 397 | @Override public String toString() { |
| 398 | return "[none]"; |
| 399 | } |
| 400 | } |
| 401 | |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 402 | // this class not test-covered |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 403 | static class AnnotationInstanceStrategy implements AnnotationStrategy { |
| 404 | |
| 405 | final Annotation annotation; |
| 406 | |
| 407 | AnnotationInstanceStrategy(Annotation annotation) { |
| 408 | this.annotation = nonNull(annotation, "annotation"); |
| 409 | } |
| 410 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 411 | public boolean hasAttributes() { |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | public AnnotationStrategy withoutAttributes() { |
| 416 | return new AnnotationTypeStrategy(getAnnotationType(), annotation); |
| 417 | } |
| 418 | |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 419 | public Annotation getAnnotation() { |
| 420 | return annotation; |
| 421 | } |
| 422 | |
| 423 | public Class<? extends Annotation> getAnnotationType() { |
| 424 | return annotation.annotationType(); |
| 425 | } |
| 426 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 427 | @Override public boolean equals(Object o) { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 428 | if (!(o instanceof AnnotationInstanceStrategy)) { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | AnnotationInstanceStrategy other = (AnnotationInstanceStrategy) o; |
| 433 | return annotation.equals(other.annotation); |
| 434 | } |
| 435 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 436 | @Override public int hashCode() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 437 | return annotation.hashCode(); |
| 438 | } |
| 439 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 440 | @Override public String toString() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 441 | return annotation.toString(); |
| 442 | } |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 443 | |
| 444 | private static final long serialVersionUID = 0; |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static class AnnotationTypeStrategy implements AnnotationStrategy { |
| 448 | |
| 449 | final Class<? extends Annotation> annotationType; |
| 450 | |
| 451 | // Keep the instance around if we have it so the client can request it. |
| 452 | final Annotation annotation; |
| 453 | |
| 454 | AnnotationTypeStrategy(Class<? extends Annotation> annotationType, |
| 455 | Annotation annotation) { |
| 456 | this.annotationType = nonNull(annotationType, "annotation type"); |
| 457 | this.annotation = annotation; |
| 458 | } |
| 459 | |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 460 | public boolean hasAttributes() { |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | public AnnotationStrategy withoutAttributes() { |
| 465 | throw new UnsupportedOperationException("Key already has no attributes."); |
| 466 | } |
| 467 | |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 468 | public Annotation getAnnotation() { |
| 469 | return annotation; |
| 470 | } |
| 471 | |
| 472 | public Class<? extends Annotation> getAnnotationType() { |
| 473 | return annotationType; |
| 474 | } |
| 475 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 476 | @Override public boolean equals(Object o) { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 477 | if (!(o instanceof AnnotationTypeStrategy)) { |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | AnnotationTypeStrategy other = (AnnotationTypeStrategy) o; |
| 482 | return annotationType.equals(other.annotationType); |
| 483 | } |
| 484 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 485 | @Override public int hashCode() { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 486 | return annotationType.hashCode(); |
| 487 | } |
| 488 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 489 | @Override public String toString() { |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 490 | return "@" + annotationType.getName(); |
crazyboblee | 41bc852 | 2006-12-08 07:06:55 +0000 | [diff] [blame] | 491 | } |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 492 | |
| 493 | private static final long serialVersionUID = 0; |
crazyboblee | ed8825f | 2006-12-06 01:28:10 +0000 | [diff] [blame] | 494 | } |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 495 | |
| 496 | static boolean isBindingAnnotation(Annotation annotation) { |
| 497 | return isBindingAnnotation(annotation.annotationType()); |
| 498 | } |
| 499 | |
| 500 | static boolean isBindingAnnotation( |
| 501 | Class<? extends Annotation> annotationType) { |
| 502 | return annotationType.isAnnotationPresent(BindingAnnotation.class); |
| 503 | } |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 504 | |
limpbizkit | f530b25 | 2008-05-27 23:03:42 +0000 | [diff] [blame^] | 505 | /** |
| 506 | * Returns the canonical form of this key for serialization. The returned |
| 507 | * instance is always a {@code Key}, never a subclass. This prevents problems |
| 508 | * caused by serializing anonymous types. |
| 509 | */ |
| 510 | protected final Object writeReplace() { |
| 511 | return getClass() == Key.class |
| 512 | ? this |
| 513 | : new Key<T>(typeLiteral, annotationStrategy); |
| 514 | } |
| 515 | |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 516 | private static final long serialVersionUID = 0; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 517 | } |