limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.inject.internal; |
| 18 | |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 22 | import com.google.inject.Binder; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 23 | import com.google.inject.Exposed; |
| 24 | import com.google.inject.Key; |
crazyboblee | 9119370 | 2009-02-05 22:18:55 +0000 | [diff] [blame] | 25 | import com.google.inject.PrivateBinder; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 26 | import com.google.inject.Provider; |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 27 | import com.google.inject.Provides; |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 28 | import com.google.inject.internal.BytecodeGen.Visibility; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 29 | import com.google.inject.internal.util.StackTraceElements; |
Sam Berlin | 29ce12b | 2014-03-10 12:52:01 -0400 | [diff] [blame] | 30 | import com.google.inject.spi.BindingTargetVisitor; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 31 | import com.google.inject.spi.Dependency; |
Sam Berlin | 29ce12b | 2014-03-10 12:52:01 -0400 | [diff] [blame] | 32 | import com.google.inject.spi.HasDependencies; |
| 33 | import com.google.inject.spi.ProviderInstanceBinding; |
| 34 | import com.google.inject.spi.ProviderWithExtensionVisitor; |
| 35 | import com.google.inject.spi.ProvidesMethodBinding; |
| 36 | import com.google.inject.spi.ProvidesMethodTargetVisitor; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 37 | |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 38 | import java.lang.annotation.Annotation; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 39 | import java.lang.reflect.InvocationTargetException; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 40 | import java.lang.reflect.Method; |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 41 | import java.lang.reflect.Modifier; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 42 | import java.util.List; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 43 | import java.util.Set; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * A provider that invokes a method and returns its result. |
| 47 | * |
| 48 | * @author jessewilson@google.com (Jesse Wilson) |
| 49 | */ |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 50 | public abstract class ProviderMethod<T> implements ProviderWithExtensionVisitor<T>, HasDependencies, |
Sam Berlin | 29ce12b | 2014-03-10 12:52:01 -0400 | [diff] [blame] | 51 | ProvidesMethodBinding<T> { |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Creates a {@link ProviderMethod}. |
| 55 | * |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 56 | * <p>Unless {@code skipFastClassGeneration} is set, this will use |
| 57 | * {@link net.sf.cglib.reflect.FastClass} to invoke the actual method, since it is significantly |
| 58 | * faster. However, this will fail if the method is {@code private} or {@code protected}, since |
| 59 | * fastclass is subject to java access policies. |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 60 | */ |
| 61 | static <T> ProviderMethod<T> create(Key<T> key, Method method, Object instance, |
| 62 | ImmutableSet<Dependency<?>> dependencies, List<Provider<?>> parameterProviders, |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 63 | Class<? extends Annotation> scopeAnnotation, boolean skipFastClassGeneration, |
| 64 | Annotation annotation) { |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 65 | int modifiers = method.getModifiers(); |
| 66 | /*if[AOP]*/ |
Sam Berlin | 58d6074 | 2014-07-21 17:13:22 -0400 | [diff] [blame] | 67 | if (!skipFastClassGeneration && !Modifier.isPrivate(modifiers) |
| 68 | && !Modifier.isProtected(modifiers)) { |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 69 | try { |
| 70 | // We use an index instead of FastMethod to save a stack frame. |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 71 | return new FastClassProviderMethod<T>(key, |
| 72 | method, |
| 73 | instance, |
| 74 | dependencies, |
| 75 | parameterProviders, |
| 76 | scopeAnnotation, |
| 77 | annotation); |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 78 | } catch (net.sf.cglib.core.CodeGenerationException e) {/* fall-through */} |
| 79 | } |
| 80 | /*end[AOP]*/ |
| 81 | |
| 82 | if (!Modifier.isPublic(modifiers) || |
| 83 | !Modifier.isPublic(method.getDeclaringClass().getModifiers())) { |
| 84 | method.setAccessible(true); |
| 85 | } |
| 86 | |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 87 | return new ReflectionProviderMethod<T>(key, |
| 88 | method, |
| 89 | instance, |
| 90 | dependencies, |
| 91 | parameterProviders, |
| 92 | scopeAnnotation, |
| 93 | annotation); |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | protected final Object instance; |
| 97 | protected final Method method; |
| 98 | |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 99 | private final Key<T> key; |
| 100 | private final Class<? extends Annotation> scopeAnnotation; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 101 | private final ImmutableSet<Dependency<?>> dependencies; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 102 | private final List<Provider<?>> parameterProviders; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 103 | private final boolean exposed; |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 104 | private final Annotation annotation; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * @param method the method to invoke. It's return type must be the same type as {@code key}. |
| 108 | */ |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 109 | private ProviderMethod(Key<T> key, Method method, Object instance, |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 110 | ImmutableSet<Dependency<?>> dependencies, List<Provider<?>> parameterProviders, |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 111 | Class<? extends Annotation> scopeAnnotation, Annotation annotation) { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 112 | this.key = key; |
| 113 | this.scopeAnnotation = scopeAnnotation; |
| 114 | this.instance = instance; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 115 | this.dependencies = dependencies; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 116 | this.method = method; |
| 117 | this.parameterProviders = parameterProviders; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 118 | this.exposed = method.isAnnotationPresent(Exposed.class); |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 119 | this.annotation = annotation; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 122 | @Override |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 123 | public Key<T> getKey() { |
| 124 | return key; |
| 125 | } |
| 126 | |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 127 | @Override |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 128 | public Method getMethod() { |
| 129 | return method; |
| 130 | } |
| 131 | |
limpbizkit | 714df3c | 2009-01-08 02:40:04 +0000 | [diff] [blame] | 132 | // exposed for GIN |
| 133 | public Object getInstance() { |
| 134 | return instance; |
| 135 | } |
Sam Berlin | 29ce12b | 2014-03-10 12:52:01 -0400 | [diff] [blame] | 136 | |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 137 | @Override |
Sam Berlin | 29ce12b | 2014-03-10 12:52:01 -0400 | [diff] [blame] | 138 | public Object getEnclosingInstance() { |
| 139 | return instance; |
| 140 | } |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 141 | |
| 142 | @Override |
| 143 | public Annotation getAnnotation() { |
| 144 | return annotation; |
| 145 | } |
limpbizkit | 714df3c | 2009-01-08 02:40:04 +0000 | [diff] [blame] | 146 | |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 147 | public void configure(Binder binder) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 148 | binder = binder.withSource(method); |
| 149 | |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 150 | if (scopeAnnotation != null) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 151 | binder.bind(key).toProvider(this).in(scopeAnnotation); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 152 | } else { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 153 | binder.bind(key).toProvider(this); |
| 154 | } |
| 155 | |
| 156 | if (exposed) { |
limpbizkit | 0551b64 | 2008-11-26 22:25:05 +0000 | [diff] [blame] | 157 | // the cast is safe 'cause the only binder we have implements PrivateBinder. If there's a |
| 158 | // misplaced @Exposed, calling this will add an error to the binder's error queue |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 159 | ((PrivateBinder) binder).expose(key); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 163 | @Override |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 164 | public T get() { |
| 165 | Object[] parameters = new Object[parameterProviders.size()]; |
| 166 | for (int i = 0; i < parameters.length; i++) { |
| 167 | parameters[i] = parameterProviders.get(i).get(); |
| 168 | } |
| 169 | |
| 170 | try { |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 171 | @SuppressWarnings({ "unchecked", "UnnecessaryLocalVariable" }) |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 172 | T result = (T) doProvision(parameters); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 173 | return result; |
| 174 | } catch (IllegalAccessException e) { |
| 175 | throw new AssertionError(e); |
| 176 | } catch (InvocationTargetException e) { |
sberlin | 132a5db | 2011-06-05 18:32:05 +0000 | [diff] [blame] | 177 | throw Exceptions.rethrowCause(e); |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 180 | |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 181 | /** Extension point for our subclasses to implement the provisioning strategy. */ |
| 182 | abstract Object doProvision(Object[] parameters) |
| 183 | throws IllegalAccessException, InvocationTargetException; |
| 184 | |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 185 | @Override |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 186 | public Set<Dependency<?>> getDependencies() { |
| 187 | return dependencies; |
| 188 | } |
Sam Berlin | 29ce12b | 2014-03-10 12:52:01 -0400 | [diff] [blame] | 189 | |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 190 | @Override |
Sam Berlin | 29ce12b | 2014-03-10 12:52:01 -0400 | [diff] [blame] | 191 | @SuppressWarnings("unchecked") |
| 192 | public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, |
| 193 | ProviderInstanceBinding<? extends B> binding) { |
| 194 | if (visitor instanceof ProvidesMethodTargetVisitor) { |
| 195 | return ((ProvidesMethodTargetVisitor<T, V>)visitor).visit(this); |
| 196 | } |
| 197 | return visitor.visit(binding); |
| 198 | } |
limpbizkit | f03bdb8 | 2009-10-05 21:54:38 +0000 | [diff] [blame] | 199 | |
| 200 | @Override public String toString() { |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 201 | String annotationString = annotation.toString(); |
| 202 | // Show @Provides w/o the com.google.inject prefix. |
| 203 | if (annotation.annotationType() == Provides.class) { |
| 204 | annotationString = "@Provides"; |
| 205 | } else if (annotationString.endsWith("()")) { |
| 206 | // Remove the common "()" suffix if there are no values. |
| 207 | annotationString = annotationString.substring(0, annotationString.length() - 2); |
| 208 | } |
| 209 | return annotationString + " " + StackTraceElements.forMember(method); |
limpbizkit | f03bdb8 | 2009-10-05 21:54:38 +0000 | [diff] [blame] | 210 | } |
sberlin | 1068a4a | 2010-12-16 01:38:08 +0000 | [diff] [blame] | 211 | |
| 212 | @Override |
| 213 | public boolean equals(Object obj) { |
| 214 | if (obj instanceof ProviderMethod) { |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 215 | ProviderMethod<?> o = (ProviderMethod<?>) obj; |
sberlin | 7691f9e | 2010-12-16 02:34:54 +0000 | [diff] [blame] | 216 | return method.equals(o.method) |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 217 | && instance.equals(o.instance) |
| 218 | && annotation.equals(o.annotation); |
sberlin | 1068a4a | 2010-12-16 01:38:08 +0000 | [diff] [blame] | 219 | } else { |
| 220 | return false; |
| 221 | } |
| 222 | } |
sberlin | 672d074 | 2010-12-16 14:46:42 +0000 | [diff] [blame] | 223 | |
| 224 | @Override |
| 225 | public int hashCode() { |
| 226 | // Avoid calling hashCode on 'instance', which is a user-object |
| 227 | // that might not be expecting it. |
| 228 | // (We need to call equals, so we do. But we can avoid hashCode.) |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 229 | return Objects.hashCode(method, annotation); |
sberlin | 672d074 | 2010-12-16 14:46:42 +0000 | [diff] [blame] | 230 | } |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 231 | |
| 232 | /*if[AOP]*/ |
| 233 | /** |
| 234 | * A {@link ProviderMethod} implementation that uses {@link net.sf.cglib.reflect.FastClass#invoke} |
| 235 | * to invoke the provider method. |
| 236 | */ |
| 237 | private static final class FastClassProviderMethod<T> extends ProviderMethod<T> { |
| 238 | final net.sf.cglib.reflect.FastClass fastClass; |
| 239 | final int methodIndex; |
| 240 | |
| 241 | FastClassProviderMethod(Key<T> key, |
| 242 | Method method, |
| 243 | Object instance, |
| 244 | ImmutableSet<Dependency<?>> dependencies, |
| 245 | List<Provider<?>> parameterProviders, |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 246 | Class<? extends Annotation> scopeAnnotation, |
| 247 | Annotation annotation) { |
| 248 | super(key, |
| 249 | method, |
| 250 | instance, |
| 251 | dependencies, |
| 252 | parameterProviders, |
| 253 | scopeAnnotation, |
| 254 | annotation); |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 255 | // We need to generate a FastClass for the method's class, not the object's class. |
| 256 | this.fastClass = |
| 257 | BytecodeGen.newFastClass(method.getDeclaringClass(), Visibility.forMember(method)); |
| 258 | // Use the Signature overload of getIndex because it properly uses return types to identify |
| 259 | // particular methods. This is normally irrelevant, except in the case of covariant overrides |
| 260 | // which java implements with a compiler generated bridge method to implement the override. |
| 261 | this.methodIndex = fastClass.getIndex( |
| 262 | new net.sf.cglib.core.Signature( |
| 263 | method.getName(), org.objectweb.asm.Type.getMethodDescriptor(method))); |
| 264 | Preconditions.checkArgument(this.methodIndex >= 0, |
| 265 | "Could not find method %s in fast class for class %s", |
| 266 | method, |
| 267 | method.getDeclaringClass()); |
| 268 | } |
| 269 | |
| 270 | @Override public Object doProvision(Object[] parameters) |
| 271 | throws IllegalAccessException, InvocationTargetException { |
| 272 | return fastClass.invoke(methodIndex, instance, parameters); |
| 273 | } |
| 274 | } |
| 275 | /*end[AOP]*/ |
| 276 | |
| 277 | /** |
| 278 | * A {@link ProviderMethod} implementation that invokes the method using normal java reflection. |
| 279 | */ |
| 280 | private static final class ReflectionProviderMethod<T> extends ProviderMethod<T> { |
| 281 | ReflectionProviderMethod(Key<T> key, |
| 282 | Method method, |
| 283 | Object instance, |
| 284 | ImmutableSet<Dependency<?>> dependencies, |
| 285 | List<Provider<?>> parameterProviders, |
sameb | e18906a | 2015-02-02 14:07:53 -0800 | [diff] [blame^] | 286 | Class<? extends Annotation> scopeAnnotation, |
| 287 | Annotation annotation) { |
| 288 | super(key, |
| 289 | method, |
| 290 | instance, |
| 291 | dependencies, |
| 292 | parameterProviders, |
| 293 | scopeAnnotation, |
| 294 | annotation); |
Sam Berlin | 409e0f5 | 2014-05-10 10:34:16 -0400 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override Object doProvision(Object[] parameters) throws IllegalAccessException, |
| 298 | InvocationTargetException { |
| 299 | return method.invoke(instance, parameters); |
| 300 | } |
| 301 | } |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 302 | } |