limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 1 | /** |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008 Google Inc. |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 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.spi; |
| 18 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 19 | import com.google.inject.ConfigurationException; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 20 | import com.google.inject.Inject; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 21 | import com.google.inject.Key; |
limpbizkit | 4f5d1f7 | 2008-11-14 08:43:40 +0000 | [diff] [blame] | 22 | import com.google.inject.TypeLiteral; |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 23 | import com.google.inject.internal.Annotations; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 24 | import com.google.inject.internal.Errors; |
| 25 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 26 | import static com.google.inject.internal.MoreTypes.getRawType; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 27 | import com.google.inject.internal.Nullability; |
sberlin | dfee9fd | 2010-10-30 14:15:06 +0000 | [diff] [blame] | 28 | import com.google.inject.internal.util.Classes; |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 29 | import com.google.common.collect.ImmutableList; |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | import com.google.common.collect.Lists; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 32 | import java.lang.annotation.Annotation; |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 33 | import java.lang.reflect.AnnotatedElement; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 34 | import java.lang.reflect.Constructor; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 35 | import java.lang.reflect.Field; |
| 36 | import java.lang.reflect.Member; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 37 | import java.lang.reflect.Method; |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 38 | import java.lang.reflect.Modifier; |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 39 | import java.util.ArrayList; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 40 | import java.util.Arrays; |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 41 | import java.util.Collections; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 42 | import java.util.HashMap; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 43 | import java.util.Iterator; |
| 44 | import java.util.List; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 45 | import java.util.Map; |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 46 | import java.util.Set; |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 47 | import java.util.logging.Level; |
| 48 | import java.util.logging.Logger; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 51 | * A constructor, field or method that can receive injections. Typically this is a member with the |
| 52 | * {@literal @}{@link Inject} annotation. For non-private, no argument constructors, the member may |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 53 | * omit the annotation. |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 54 | * |
| 55 | * @author crazybob@google.com (Bob Lee) |
limpbizkit | c489adf | 2008-11-18 07:01:33 +0000 | [diff] [blame] | 56 | * @since 2.0 |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 57 | */ |
limpbizkit | 6caa8dc | 2009-05-19 00:40:02 +0000 | [diff] [blame] | 58 | public final class InjectionPoint { |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 59 | |
| 60 | private static final Logger logger = Logger.getLogger(InjectionPoint.class.getName()); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 61 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 62 | private final boolean optional; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 63 | private final Member member; |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 64 | private final TypeLiteral<?> declaringType; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 65 | private final ImmutableList<Dependency<?>> dependencies; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 66 | |
limpbizkit | 365f834 | 2009-09-07 21:00:57 +0000 | [diff] [blame] | 67 | InjectionPoint(TypeLiteral<?> declaringType, Method method, boolean optional) { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 68 | this.member = method; |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 69 | this.declaringType = declaringType; |
limpbizkit | 365f834 | 2009-09-07 21:00:57 +0000 | [diff] [blame] | 70 | this.optional = optional; |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 71 | this.dependencies = forMember(method, declaringType, method.getParameterAnnotations()); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 72 | } |
| 73 | |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 74 | InjectionPoint(TypeLiteral<?> declaringType, Constructor<?> constructor) { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 75 | this.member = constructor; |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 76 | this.declaringType = declaringType; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 77 | this.optional = false; |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 78 | this.dependencies = forMember( |
| 79 | constructor, declaringType, constructor.getParameterAnnotations()); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 80 | } |
| 81 | |
limpbizkit | 365f834 | 2009-09-07 21:00:57 +0000 | [diff] [blame] | 82 | InjectionPoint(TypeLiteral<?> declaringType, Field field, boolean optional) { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 83 | this.member = field; |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 84 | this.declaringType = declaringType; |
limpbizkit | 365f834 | 2009-09-07 21:00:57 +0000 | [diff] [blame] | 85 | this.optional = optional; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 86 | |
| 87 | Annotation[] annotations = field.getAnnotations(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 88 | |
| 89 | Errors errors = new Errors(field); |
| 90 | Key<?> key = null; |
| 91 | try { |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 92 | key = Annotations.getKey(declaringType.getFieldType(field), field, annotations, errors); |
limpbizkit | 39e9886 | 2009-06-21 18:13:01 +0000 | [diff] [blame] | 93 | } catch (ConfigurationException e) { |
| 94 | errors.merge(e.getErrorMessages()); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 95 | } catch (ErrorsException e) { |
| 96 | errors.merge(e.getErrors()); |
| 97 | } |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 98 | errors.throwConfigurationExceptionIfErrorsExist(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 99 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 100 | this.dependencies = ImmutableList.<Dependency<?>>of( |
| 101 | newDependency(key, Nullability.allowsNull(annotations), -1)); |
| 102 | } |
| 103 | |
limpbizkit | 4f5d1f7 | 2008-11-14 08:43:40 +0000 | [diff] [blame] | 104 | private ImmutableList<Dependency<?>> forMember(Member member, TypeLiteral<?> type, |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 105 | Annotation[][] paramterAnnotations) { |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 106 | Errors errors = new Errors(member); |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 107 | Iterator<Annotation[]> annotationsIterator = Arrays.asList(paramterAnnotations).iterator(); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 108 | |
| 109 | List<Dependency<?>> dependencies = Lists.newArrayList(); |
| 110 | int index = 0; |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 111 | |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 112 | for (TypeLiteral<?> parameterType : type.getParameterTypes(member)) { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 113 | try { |
| 114 | Annotation[] parameterAnnotations = annotationsIterator.next(); |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 115 | Key<?> key = Annotations.getKey(parameterType, member, parameterAnnotations, errors); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 116 | dependencies.add(newDependency(key, Nullability.allowsNull(parameterAnnotations), index)); |
| 117 | index++; |
limpbizkit | 39e9886 | 2009-06-21 18:13:01 +0000 | [diff] [blame] | 118 | } catch (ConfigurationException e) { |
| 119 | errors.merge(e.getErrorMessages()); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 120 | } catch (ErrorsException e) { |
| 121 | errors.merge(e.getErrors()); |
| 122 | } |
| 123 | } |
| 124 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 125 | errors.throwConfigurationExceptionIfErrorsExist(); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 126 | return ImmutableList.copyOf(dependencies); |
| 127 | } |
| 128 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 129 | // This metohd is necessary to create a Dependency<T> with proper generic type information |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 130 | private <T> Dependency<T> newDependency(Key<T> key, boolean allowsNull, int parameterIndex) { |
| 131 | return new Dependency<T>(this, key, allowsNull, parameterIndex); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 132 | } |
| 133 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 134 | /** |
| 135 | * Returns the injected constructor, field, or method. |
| 136 | */ |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 137 | public Member getMember() { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 138 | // TODO: Don't expose the original member (which probably has setAccessible(true)). |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 139 | return member; |
| 140 | } |
| 141 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 142 | /** |
| 143 | * Returns the dependencies for this injection point. If the injection point is for a method or |
| 144 | * constructor, the dependencies will correspond to that member's parameters. Field injection |
| 145 | * points always have a single dependency for the field itself. |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 146 | * |
| 147 | * @return a possibly-empty list |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 148 | */ |
| 149 | public List<Dependency<?>> getDependencies() { |
| 150 | return dependencies; |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 151 | } |
| 152 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 153 | /** |
| 154 | * Returns true if this injection point shall be skipped if the injector cannot resolve bindings |
| 155 | * for all required dependencies. Both explicit bindings (as specified in a module), and implicit |
| 156 | * bindings ({@literal @}{@link com.google.inject.ImplementedBy ImplementedBy}, default |
| 157 | * constructors etc.) may be used to satisfy optional injection points. |
| 158 | */ |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 159 | public boolean isOptional() { |
| 160 | return optional; |
| 161 | } |
sberlin | 97c2271 | 2010-02-05 21:12:05 +0000 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * Returns true if the element is annotated with {@literal @}{@link Toolable}. |
sberlin | e1290f3 | 2010-07-03 15:42:51 +0000 | [diff] [blame] | 165 | * |
| 166 | * @since 3.0 |
sberlin | 97c2271 | 2010-02-05 21:12:05 +0000 | [diff] [blame] | 167 | */ |
| 168 | public boolean isToolable() { |
| 169 | return ((AnnotatedElement)member).isAnnotationPresent(Toolable.class); |
| 170 | } |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 171 | |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 172 | /** |
| 173 | * Returns the generic type that defines this injection point. If the member exists on a |
| 174 | * parameterized type, the result will include more type information than the member's {@link |
| 175 | * Member#getDeclaringClass() raw declaring class}. |
sberlin | e1290f3 | 2010-07-03 15:42:51 +0000 | [diff] [blame] | 176 | * |
| 177 | * @since 3.0 |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 178 | */ |
| 179 | public TypeLiteral<?> getDeclaringType() { |
| 180 | return declaringType; |
| 181 | } |
| 182 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 183 | @Override public boolean equals(Object o) { |
| 184 | return o instanceof InjectionPoint |
limpbizkit | 62600d2 | 2009-06-20 18:01:04 +0000 | [diff] [blame] | 185 | && member.equals(((InjectionPoint) o).member) |
| 186 | && declaringType.equals(((InjectionPoint) o).declaringType); |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override public int hashCode() { |
limpbizkit | 62600d2 | 2009-06-20 18:01:04 +0000 | [diff] [blame] | 190 | return member.hashCode() ^ declaringType.hashCode(); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 191 | } |
| 192 | |
limpbizkit | 76a2b9a | 2008-08-14 04:58:23 +0000 | [diff] [blame] | 193 | @Override public String toString() { |
sberlin | dfee9fd | 2010-10-30 14:15:06 +0000 | [diff] [blame] | 194 | return Classes.toString(member); |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 195 | } |
| 196 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 197 | /** |
limpbizkit | 4447564 | 2009-06-20 17:30:23 +0000 | [diff] [blame] | 198 | * Returns a new injection point for the specified constructor. If the declaring type of {@code |
| 199 | * constructor} is parameterized (such as {@code List<T>}), prefer the overload that includes a |
| 200 | * type literal. |
limpbizkit | 9b8bdc7 | 2009-06-07 19:37:28 +0000 | [diff] [blame] | 201 | * |
| 202 | * @param constructor any single constructor present on {@code type}. |
sberlin | e1290f3 | 2010-07-03 15:42:51 +0000 | [diff] [blame] | 203 | * |
| 204 | * @since 3.0 |
limpbizkit | 9b8bdc7 | 2009-06-07 19:37:28 +0000 | [diff] [blame] | 205 | */ |
| 206 | public static <T> InjectionPoint forConstructor(Constructor<T> constructor) { |
limpbizkit | 9b8bdc7 | 2009-06-07 19:37:28 +0000 | [diff] [blame] | 207 | return new InjectionPoint(TypeLiteral.get(constructor.getDeclaringClass()), constructor); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns a new injection point for the specified constructor of {@code type}. |
| 212 | * |
| 213 | * @param constructor any single constructor present on {@code type}. |
| 214 | * @param type the concrete type that defines {@code constructor}. |
sberlin | e1290f3 | 2010-07-03 15:42:51 +0000 | [diff] [blame] | 215 | * |
| 216 | * @since 3.0 |
limpbizkit | 9b8bdc7 | 2009-06-07 19:37:28 +0000 | [diff] [blame] | 217 | */ |
| 218 | public static <T> InjectionPoint forConstructor( |
| 219 | Constructor<T> constructor, TypeLiteral<? extends T> type) { |
| 220 | if (type.getRawType() != constructor.getDeclaringClass()) { |
| 221 | new Errors(type) |
| 222 | .constructorNotDefinedByType(constructor, type) |
| 223 | .throwConfigurationExceptionIfErrorsExist(); |
| 224 | } |
| 225 | |
limpbizkit | 9b8bdc7 | 2009-06-07 19:37:28 +0000 | [diff] [blame] | 226 | return new InjectionPoint(type, constructor); |
| 227 | } |
| 228 | |
| 229 | /** |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 230 | * Returns a new injection point for the injectable constructor of {@code type}. |
| 231 | * |
| 232 | * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject}, |
| 233 | * or a no-arguments constructor that is not private. |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 234 | * @throws ConfigurationException if there is no injectable constructor, more than one injectable |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 235 | * constructor, or if parameters of the injectable constructor are malformed, such as a |
| 236 | * parameter with multiple binding annotations. |
| 237 | */ |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 238 | public static InjectionPoint forConstructorOf(TypeLiteral<?> type) { |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 239 | Class<?> rawType = getRawType(type.getType()); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 240 | Errors errors = new Errors(rawType); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 241 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 242 | Constructor<?> injectableConstructor = null; |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 243 | for (Constructor<?> constructor : rawType.getDeclaredConstructors()) { |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 244 | |
limpbizkit | 365f834 | 2009-09-07 21:00:57 +0000 | [diff] [blame] | 245 | boolean optional; |
| 246 | Inject guiceInject = constructor.getAnnotation(Inject.class); |
| 247 | if (guiceInject == null) { |
| 248 | javax.inject.Inject javaxInject = constructor.getAnnotation(javax.inject.Inject.class); |
| 249 | if (javaxInject == null) { |
| 250 | continue; |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 251 | } |
limpbizkit | 2acabce | 2009-09-28 02:51:24 +0000 | [diff] [blame] | 252 | optional = false; |
limpbizkit | 365f834 | 2009-09-07 21:00:57 +0000 | [diff] [blame] | 253 | } else { |
| 254 | optional = guiceInject.optional(); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 255 | } |
limpbizkit | 365f834 | 2009-09-07 21:00:57 +0000 | [diff] [blame] | 256 | |
| 257 | if (optional) { |
| 258 | errors.optionalConstructor(constructor); |
| 259 | } |
| 260 | |
| 261 | if (injectableConstructor != null) { |
| 262 | errors.tooManyConstructors(rawType); |
| 263 | } |
| 264 | |
| 265 | injectableConstructor = constructor; |
| 266 | checkForMisplacedBindingAnnotations(injectableConstructor, errors); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 267 | } |
| 268 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 269 | errors.throwConfigurationExceptionIfErrorsExist(); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 270 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 271 | if (injectableConstructor != null) { |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 272 | return new InjectionPoint(type, injectableConstructor); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 273 | } |
| 274 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 275 | // If no annotated constructor is found, look for a no-arg constructor instead. |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 276 | try { |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 277 | Constructor<?> noArgConstructor = rawType.getDeclaredConstructor(); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 278 | |
| 279 | // Disallow private constructors on non-private classes (unless they have @Inject) |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 280 | if (Modifier.isPrivate(noArgConstructor.getModifiers()) |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 281 | && !Modifier.isPrivate(rawType.getModifiers())) { |
| 282 | errors.missingConstructor(rawType); |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 283 | throw new ConfigurationException(errors.getMessages()); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 284 | } |
| 285 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 286 | checkForMisplacedBindingAnnotations(noArgConstructor, errors); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 287 | return new InjectionPoint(type, noArgConstructor); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 288 | } catch (NoSuchMethodException e) { |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 289 | errors.missingConstructor(rawType); |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 290 | throw new ConfigurationException(errors.getMessages()); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 294 | /** |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 295 | * Returns a new injection point for the injectable constructor of {@code type}. |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 296 | * |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 297 | * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject}, |
| 298 | * or a no-arguments constructor that is not private. |
| 299 | * @throws ConfigurationException if there is no injectable constructor, more than one injectable |
| 300 | * constructor, or if parameters of the injectable constructor are malformed, such as a |
| 301 | * parameter with multiple binding annotations. |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 302 | */ |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 303 | public static InjectionPoint forConstructorOf(Class<?> type) { |
| 304 | return forConstructorOf(TypeLiteral.get(type)); |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 308 | * Returns all static method and field injection points on {@code type}. |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 309 | * |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 310 | * @return a possibly empty set of injection points. The set has a specified iteration order. All |
| 311 | * fields are returned and then all methods. Within the fields, supertype fields are returned |
| 312 | * before subtype fields. Similarly, supertype methods are returned before subtype methods. |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 313 | * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 314 | * a field with multiple binding annotations. The exception's {@link |
| 315 | * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} |
| 316 | * of the valid injection points. |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 317 | */ |
crazyboblee | 3c84c57 | 2009-09-29 04:32:55 +0000 | [diff] [blame] | 318 | public static Set<InjectionPoint> forStaticMethodsAndFields(TypeLiteral<?> type) { |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 319 | Errors errors = new Errors(); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 320 | |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 321 | Set<InjectionPoint> result = getInjectionPoints(type, true, errors); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 322 | if (errors.hasErrors()) { |
| 323 | throw new ConfigurationException(errors.getMessages()).withPartialValue(result); |
| 324 | } |
| 325 | return result; |
| 326 | } |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 327 | |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 328 | /** |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 329 | * Returns all static method and field injection points on {@code type}. |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 330 | * |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 331 | * @return a possibly empty set of injection points. The set has a specified iteration order. All |
| 332 | * fields are returned and then all methods. Within the fields, supertype fields are returned |
| 333 | * before subtype fields. Similarly, supertype methods are returned before subtype methods. |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 334 | * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as |
| 335 | * a field with multiple binding annotations. The exception's {@link |
| 336 | * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} |
| 337 | * of the valid injection points. |
| 338 | */ |
| 339 | public static Set<InjectionPoint> forStaticMethodsAndFields(Class<?> type) { |
| 340 | return forStaticMethodsAndFields(TypeLiteral.get(type)); |
| 341 | } |
| 342 | |
| 343 | /** |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 344 | * Returns all instance method and field injection points on {@code type}. |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 345 | * |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 346 | * @return a possibly empty set of injection points. The set has a specified iteration order. All |
| 347 | * fields are returned and then all methods. Within the fields, supertype fields are returned |
| 348 | * before subtype fields. Similarly, supertype methods are returned before subtype methods. |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 349 | * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as |
| 350 | * a field with multiple binding annotations. The exception's {@link |
| 351 | * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} |
| 352 | * of the valid injection points. |
| 353 | */ |
| 354 | public static Set<InjectionPoint> forInstanceMethodsAndFields(TypeLiteral<?> type) { |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 355 | Errors errors = new Errors(); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 356 | Set<InjectionPoint> result = getInjectionPoints(type, false, errors); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 357 | if (errors.hasErrors()) { |
| 358 | throw new ConfigurationException(errors.getMessages()).withPartialValue(result); |
| 359 | } |
| 360 | return result; |
| 361 | } |
| 362 | |
| 363 | /** |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 364 | * Returns all instance method and field injection points on {@code type}. |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 365 | * |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 366 | * @return a possibly empty set of injection points. The set has a specified iteration order. All |
| 367 | * fields are returned and then all methods. Within the fields, supertype fields are returned |
| 368 | * before subtype fields. Similarly, supertype methods are returned before subtype methods. |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 369 | * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as |
| 370 | * a field with multiple binding annotations. The exception's {@link |
| 371 | * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} |
| 372 | * of the valid injection points. |
| 373 | */ |
| 374 | public static Set<InjectionPoint> forInstanceMethodsAndFields(Class<?> type) { |
| 375 | return forInstanceMethodsAndFields(TypeLiteral.get(type)); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 376 | } |
| 377 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 378 | /** |
| 379 | * Returns true if the binding annotation is in the wrong place. |
| 380 | */ |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 381 | private static boolean checkForMisplacedBindingAnnotations(Member member, Errors errors) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 382 | Annotation misplacedBindingAnnotation = Annotations.findBindingAnnotation( |
| 383 | errors, member, ((AnnotatedElement) member).getAnnotations()); |
limpbizkit | 9bf6585 | 2008-11-29 22:01:13 +0000 | [diff] [blame] | 384 | if (misplacedBindingAnnotation == null) { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 385 | return false; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 386 | } |
limpbizkit | 9bf6585 | 2008-11-29 22:01:13 +0000 | [diff] [blame] | 387 | |
| 388 | // don't warn about misplaced binding annotations on methods when there's a field with the same |
| 389 | // name. In Scala, fields always get accessor methods (that we need to ignore). See bug 242. |
| 390 | if (member instanceof Method) { |
| 391 | try { |
| 392 | if (member.getDeclaringClass().getDeclaredField(member.getName()) != null) { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 393 | return false; |
limpbizkit | 9bf6585 | 2008-11-29 22:01:13 +0000 | [diff] [blame] | 394 | } |
| 395 | } catch (NoSuchFieldException ignore) { |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | errors.misplacedBindingAnnotation(member, misplacedBindingAnnotation); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 400 | return true; |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 401 | } |
| 402 | |
crazyboblee | 6383ccd | 2009-09-29 18:11:40 +0000 | [diff] [blame] | 403 | /** |
| 404 | * Node in the doubly-linked list of injectable members (fields and methods). |
| 405 | */ |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 406 | static abstract class InjectableMember { |
| 407 | final TypeLiteral<?> declaringType; |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 408 | final boolean optional; |
| 409 | final boolean jsr330; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 410 | InjectableMember previous; |
| 411 | InjectableMember next; |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 412 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 413 | InjectableMember(TypeLiteral<?> declaringType, Annotation atInject) { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 414 | this.declaringType = declaringType; |
| 415 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 416 | if (atInject.annotationType() == javax.inject.Inject.class) { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 417 | optional = false; |
| 418 | jsr330 = true; |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | jsr330 = false; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 423 | optional = ((Inject) atInject).optional(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 424 | } |
| 425 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 426 | abstract InjectionPoint toInjectionPoint(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 427 | } |
| 428 | |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 429 | static class InjectableField extends InjectableMember { |
| 430 | final Field field; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 431 | InjectableField(TypeLiteral<?> declaringType, Field field, |
| 432 | Annotation atInject) { |
| 433 | super(declaringType, atInject); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 434 | this.field = field; |
| 435 | } |
| 436 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 437 | InjectionPoint toInjectionPoint() { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 438 | return new InjectionPoint(declaringType, field, optional); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static class InjectableMethod extends InjectableMember { |
| 443 | final Method method; |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 444 | /** |
| 445 | * true if this method overrode a method that was annotated |
| 446 | * with com.google.inject.Inject. used to allow different |
| 447 | * override behavior for guice inject vs javax.inject.Inject |
| 448 | */ |
| 449 | boolean overrodeGuiceInject; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 450 | InjectableMethod(TypeLiteral<?> declaringType, Method method, |
| 451 | Annotation atInject) { |
| 452 | super(declaringType, atInject); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 453 | this.method = method; |
| 454 | } |
| 455 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 456 | InjectionPoint toInjectionPoint() { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 457 | return new InjectionPoint(declaringType, method, optional); |
| 458 | } |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 459 | |
| 460 | public boolean isFinal() { |
| 461 | return Modifier.isFinal(method.getModifiers()); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | static Annotation getAtInject(AnnotatedElement member) { |
| 466 | Annotation a = member.getAnnotation(javax.inject.Inject.class); |
| 467 | return a == null ? member.getAnnotation(Inject.class) : a; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Linked list of injectable members. |
| 472 | */ |
| 473 | static class InjectableMembers { |
| 474 | InjectableMember head; |
| 475 | InjectableMember tail; |
| 476 | |
| 477 | void add(InjectableMember member) { |
| 478 | if (head == null) { |
| 479 | head = tail = member; |
| 480 | } else { |
| 481 | member.previous = tail; |
| 482 | tail.next = member; |
| 483 | tail = member; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void remove(InjectableMember member) { |
| 488 | if (member.previous != null) { |
| 489 | member.previous.next = member.next; |
| 490 | } |
| 491 | if (member.next != null) { |
| 492 | member.next.previous = member.previous; |
| 493 | } |
| 494 | if (head == member) { |
| 495 | head = member.next; |
| 496 | } |
| 497 | if (tail == member) { |
| 498 | tail = member.previous; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | boolean isEmpty() { |
| 503 | return head == null; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** Position in type hierarchy. */ |
| 508 | enum Position { |
| 509 | TOP, // No need to check for overridden methods |
| 510 | MIDDLE, |
| 511 | BOTTOM // Methods won't be overridden |
| 512 | } |
| 513 | |
crazyboblee | 8fbdc4e | 2009-09-29 18:05:06 +0000 | [diff] [blame] | 514 | /** |
| 515 | * Keeps track of injectable methods so we can remove methods that get overridden in O(1) time. |
| 516 | * Uses our position in the type hierarchy to perform optimizations. |
| 517 | */ |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 518 | static class OverrideIndex { |
| 519 | final InjectableMembers injectableMembers; |
| 520 | Map<Signature, List<InjectableMethod>> bySignature; |
| 521 | Position position = Position.TOP; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 522 | |
| 523 | OverrideIndex(InjectableMembers injectableMembers) { |
| 524 | this.injectableMembers = injectableMembers; |
| 525 | } |
| 526 | |
| 527 | /* Caches the signature for the last method. */ |
| 528 | Method lastMethod; |
| 529 | Signature lastSignature; |
| 530 | |
crazyboblee | ed61f2c | 2009-09-29 18:13:40 +0000 | [diff] [blame] | 531 | /** |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 532 | * Removes a method overridden by the given method, if present. In order to |
| 533 | * remain backwards compatible with prior Guice versions, this will *not* |
| 534 | * remove overridden methods if 'alwaysRemove' is false and the overridden |
| 535 | * signature was annotated with a com.google.inject.Inject. |
| 536 | * |
| 537 | * @param method |
| 538 | * The method used to determine what is overridden and should be |
| 539 | * removed. |
| 540 | * @param alwaysRemove |
| 541 | * true if overridden methods should be removed even if they were |
| 542 | * guice @Inject |
| 543 | * @param injectableMethod |
| 544 | * if this method overrode any guice @Inject methods, |
| 545 | * {@link InjectableMethod#overrodeGuiceInject} is set to true |
crazyboblee | ed61f2c | 2009-09-29 18:13:40 +0000 | [diff] [blame] | 546 | */ |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 547 | boolean removeIfOverriddenBy(Method method, boolean alwaysRemove, |
| 548 | InjectableMethod injectableMethod) { |
crazyboblee | 6ee3634 | 2009-09-29 18:07:22 +0000 | [diff] [blame] | 549 | if (position == Position.TOP) { |
| 550 | // If we're at the top of the hierarchy, there's nothing to override. |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 551 | return false; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | if (bySignature == null) { |
| 555 | // We encountered a method in a subclass. Time to index the |
| 556 | // methods in the parent class. |
| 557 | bySignature = new HashMap<Signature, List<InjectableMethod>>(); |
| 558 | for (InjectableMember member = injectableMembers.head; member != null; |
| 559 | member = member.next) { |
| 560 | if (!(member instanceof InjectableMethod)) continue; |
| 561 | InjectableMethod im = (InjectableMethod) member; |
| 562 | if (im.isFinal()) continue; |
| 563 | List<InjectableMethod> methods = new ArrayList<InjectableMethod>(); |
| 564 | methods.add(im); |
| 565 | bySignature.put(new Signature(im.method), methods); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | lastMethod = method; |
| 570 | Signature signature = lastSignature = new Signature(method); |
| 571 | List<InjectableMethod> methods = bySignature.get(signature); |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 572 | boolean removed = false; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 573 | if (methods != null) { |
| 574 | for (Iterator<InjectableMethod> iterator = methods.iterator(); |
| 575 | iterator.hasNext();) { |
| 576 | InjectableMethod possiblyOverridden = iterator.next(); |
| 577 | if (overrides(method, possiblyOverridden.method)) { |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 578 | boolean wasGuiceInject = |
| 579 | !possiblyOverridden.jsr330 || possiblyOverridden.overrodeGuiceInject; |
| 580 | if(injectableMethod != null) { |
| 581 | injectableMethod.overrodeGuiceInject = wasGuiceInject; |
| 582 | } |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 583 | // Only actually remove the methods if we want to force |
| 584 | // remove or if the signature never specified @com.google.inject.Inject |
| 585 | // somewhere. |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 586 | if(alwaysRemove || !wasGuiceInject) { |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 587 | removed = true; |
| 588 | iterator.remove(); |
| 589 | injectableMembers.remove(possiblyOverridden); |
| 590 | } |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | } |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 594 | return removed; |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 595 | } |
| 596 | |
crazyboblee | ed61f2c | 2009-09-29 18:13:40 +0000 | [diff] [blame] | 597 | /** |
| 598 | * Adds the given method to the list of injection points. Keeps track of it in this index |
| 599 | * in case it gets overridden. |
| 600 | */ |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 601 | void add(InjectableMethod injectableMethod) { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 602 | injectableMembers.add(injectableMethod); |
| 603 | if (position == Position.BOTTOM |
| 604 | || injectableMethod.isFinal()) { |
| 605 | // This method can't be overridden, so there's no need to index it. |
| 606 | return; |
| 607 | } |
| 608 | if (bySignature != null) { |
| 609 | // Try to reuse the signature we created during removal |
| 610 | Signature signature = injectableMethod.method == lastMethod |
| 611 | ? lastSignature : new Signature(injectableMethod.method); |
| 612 | List<InjectableMethod> methods = bySignature.get(signature); |
| 613 | if (methods == null) { |
| 614 | methods = new ArrayList<InjectableMethod>(); |
| 615 | bySignature.put(signature, methods); |
| 616 | } |
| 617 | methods.add(injectableMethod); |
| 618 | } |
| 619 | } |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 620 | } |
| 621 | |
crazyboblee | 6383ccd | 2009-09-29 18:11:40 +0000 | [diff] [blame] | 622 | /** |
| 623 | * Returns an ordered, immutable set of injection points for the given type. Members in |
| 624 | * superclasses come before members in subclasses. Within a class, fields come before methods. |
| 625 | * Overridden methods are filtered out. |
| 626 | * |
| 627 | * @param statics true is this method should return static members, false for instance members |
| 628 | * @param errors used to record errors |
| 629 | */ |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 630 | private static Set<InjectionPoint> getInjectionPoints(final TypeLiteral<?> type, |
| 631 | boolean statics, Errors errors) { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 632 | InjectableMembers injectableMembers = new InjectableMembers(); |
| 633 | OverrideIndex overrideIndex = null; |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 634 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 635 | List<TypeLiteral<?>> hierarchy = hierarchyFor(type); |
| 636 | int topIndex = hierarchy.size() - 1; |
| 637 | for (int i = topIndex; i >= 0; i--) { |
| 638 | if (overrideIndex != null && i < topIndex) { |
| 639 | // Knowing the position within the hierarchy helps us make optimizations. |
| 640 | if (i == 0) { |
| 641 | overrideIndex.position = Position.BOTTOM; |
| 642 | } else { |
| 643 | overrideIndex.position = Position.MIDDLE; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | TypeLiteral<?> current = hierarchy.get(i); |
| 648 | |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 649 | for (Field field : current.getRawType().getDeclaredFields()) { |
| 650 | if (Modifier.isStatic(field.getModifiers()) == statics) { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 651 | Annotation atInject = getAtInject(field); |
| 652 | if (atInject != null) { |
| 653 | InjectableField injectableField = new InjectableField(current, field, atInject); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 654 | if (injectableField.jsr330 && Modifier.isFinal(field.getModifiers())) { |
| 655 | errors.cannotInjectFinalField(field); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 656 | } |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 657 | injectableMembers.add(injectableField); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 660 | } |
| 661 | |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 662 | for (Method method : current.getRawType().getDeclaredMethods()) { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 663 | if (Modifier.isStatic(method.getModifiers()) == statics) { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 664 | Annotation atInject = getAtInject(method); |
| 665 | if (atInject != null) { |
| 666 | InjectableMethod injectableMethod = new InjectableMethod( |
| 667 | current, method, atInject); |
| 668 | if (checkForMisplacedBindingAnnotations(method, errors) |
| 669 | | !isValidMethod(injectableMethod, errors)) { |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 670 | if (overrideIndex != null) { |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 671 | boolean removed = overrideIndex.removeIfOverriddenBy(method, false, injectableMethod); |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 672 | if(removed) { |
| 673 | logger.log(Level.WARNING, "Method: {0} is not a valid injectable method (" |
| 674 | + "because it either has misplaced binding annotations " |
| 675 | + "or specifies type parameters) but is overriding a method that is valid. " |
| 676 | + "Because it is not valid, the method will not be injected. " |
| 677 | + "To fix this, make the method a valid injectable method.", method); |
| 678 | } |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 679 | } |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 680 | continue; |
| 681 | } |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 682 | if (statics) { |
| 683 | injectableMembers.add(injectableMethod); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 684 | } else { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 685 | if (overrideIndex == null) { |
crazyboblee | 570c181 | 2009-09-29 18:00:49 +0000 | [diff] [blame] | 686 | /* |
| 687 | * Creating the override index lazily means that the first type in the hierarchy |
crazyboblee | 8fbdc4e | 2009-09-29 18:05:06 +0000 | [diff] [blame] | 688 | * with injectable methods (not necessarily the top most type) will be treated as |
crazyboblee | 570c181 | 2009-09-29 18:00:49 +0000 | [diff] [blame] | 689 | * the TOP position and will enjoy the same optimizations (no checks for overridden |
| 690 | * methods, etc.). |
| 691 | */ |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 692 | overrideIndex = new OverrideIndex(injectableMembers); |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 693 | } else { |
| 694 | // Forcibly remove the overriden method, otherwise we'll inject |
| 695 | // it twice. |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 696 | overrideIndex.removeIfOverriddenBy(method, true, injectableMethod); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 697 | } |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 698 | overrideIndex.add(injectableMethod); |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 699 | } |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 700 | } else { |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 701 | if(overrideIndex != null) { |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 702 | boolean removed = overrideIndex.removeIfOverriddenBy(method, false, null); |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 703 | if(removed) { |
| 704 | logger.log(Level.WARNING, "Method: {0} is not annotated with @Inject but " |
sberlin@gmail.com | e2a8afd | 2010-06-23 05:13:56 +0000 | [diff] [blame] | 705 | + "is overriding a method that is annotated with @javax.inject.Inject. Because " |
sberlin@gmail.com | f51c690 | 2010-06-22 17:20:57 +0000 | [diff] [blame] | 706 | + "it is not annotated with @Inject, the method will not be injected. " |
| 707 | + "To fix this, annotate the method with @Inject.", method); |
| 708 | } |
sberlin | 8ad6468 | 2010-05-12 17:10:39 +0000 | [diff] [blame] | 709 | } |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | if (injectableMembers.isEmpty()) { |
| 716 | return Collections.emptySet(); |
| 717 | } |
| 718 | |
| 719 | ImmutableSet.Builder<InjectionPoint> builder = ImmutableSet.builder(); |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 720 | for (InjectableMember im = injectableMembers.head; im != null; |
| 721 | im = im.next) { |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 722 | try { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 723 | builder.add(im.toInjectionPoint()); |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 724 | } catch (ConfigurationException ignorable) { |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 725 | if (!im.optional) { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 726 | errors.merge(ignorable.getErrorMessages()); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | } |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 730 | return builder.build(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 731 | } |
| 732 | |
crazyboblee | e7a727c | 2009-09-29 07:52:01 +0000 | [diff] [blame] | 733 | private static boolean isValidMethod(InjectableMethod injectableMethod, |
| 734 | Errors errors) { |
| 735 | boolean result = true; |
| 736 | if (injectableMethod.jsr330) { |
| 737 | Method method = injectableMethod.method; |
| 738 | if (Modifier.isAbstract(method.getModifiers())) { |
| 739 | errors.cannotInjectAbstractMethod(method); |
| 740 | result = false; |
| 741 | } |
| 742 | if (method.getTypeParameters().length > 0) { |
| 743 | errors.cannotInjectMethodWithTypeParameters(method); |
| 744 | result = false; |
| 745 | } |
| 746 | } |
| 747 | return result; |
| 748 | } |
| 749 | |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 750 | private static List<TypeLiteral<?>> hierarchyFor(TypeLiteral<?> type) { |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 751 | List<TypeLiteral<?>> hierarchy = new ArrayList<TypeLiteral<?>>(); |
| 752 | TypeLiteral<?> current = type; |
| 753 | while (current.getRawType() != Object.class) { |
| 754 | hierarchy.add(current); |
| 755 | current = current.getSupertype(current.getRawType().getSuperclass()); |
| 756 | } |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 757 | return hierarchy; |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 758 | } |
| 759 | |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 760 | /** |
| 761 | * Returns true if a overrides b. Assumes signatures of a and b are the same and a's declaring |
| 762 | * class is a subclass of b's declaring class. |
| 763 | */ |
| 764 | private static boolean overrides(Method a, Method b) { |
| 765 | // See JLS section 8.4.8.1 |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 766 | int modifiers = b.getModifiers(); |
| 767 | if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) { |
| 768 | return true; |
| 769 | } |
| 770 | if (Modifier.isPrivate(modifiers)) { |
| 771 | return false; |
| 772 | } |
| 773 | // b must be package-private |
| 774 | return a.getDeclaringClass().getPackage().equals(b.getDeclaringClass().getPackage()); |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * A method signature. Used to handle method overridding. |
| 779 | */ |
| 780 | static class Signature { |
| 781 | |
| 782 | final String name; |
| 783 | final Class[] parameterTypes; |
| 784 | final int hash; |
| 785 | |
| 786 | Signature(Method method) { |
| 787 | this.name = method.getName(); |
| 788 | this.parameterTypes = method.getParameterTypes(); |
| 789 | |
| 790 | int h = name.hashCode(); |
| 791 | h = h * 31 + parameterTypes.length; |
| 792 | for (Class parameterType : parameterTypes) { |
| 793 | h = h * 31 + parameterType.hashCode(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 794 | } |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 795 | this.hash = h; |
| 796 | } |
| 797 | |
| 798 | @Override public int hashCode() { |
| 799 | return this.hash; |
| 800 | } |
| 801 | |
| 802 | @Override public boolean equals(Object o) { |
| 803 | if (!(o instanceof Signature)) { |
| 804 | return false; |
| 805 | } |
| 806 | |
| 807 | Signature other = (Signature) o; |
| 808 | if (!name.equals(other.name)) { |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | if (parameterTypes.length != other.parameterTypes.length) { |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | for (int i = 0; i < parameterTypes.length; i++) { |
| 817 | if (parameterTypes[i] != other.parameterTypes[i]) { |
limpbizkit | b47d75a | 2009-09-08 00:17:45 +0000 | [diff] [blame] | 818 | return false; |
| 819 | } |
limpbizkit | b47d75a | 2009-09-08 00:17:45 +0000 | [diff] [blame] | 820 | } |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 821 | |
crazyboblee | eedf816 | 2009-09-29 02:57:14 +0000 | [diff] [blame] | 822 | return true; |
| 823 | } |
limpbizkit | 76a2b9a | 2008-08-14 04:58:23 +0000 | [diff] [blame] | 824 | } |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 825 | } |