limpbizkit | 0689806 | 2008-11-02 05:14:55 +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 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 18 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.BytecodeGen.Visibility; |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 20 | import com.google.inject.internal.InjectorImpl.MethodInvoker; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 21 | import com.google.inject.spi.InjectionPoint; |
| 22 | import java.lang.reflect.InvocationTargetException; |
| 23 | import java.lang.reflect.Method; |
| 24 | import java.lang.reflect.Modifier; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Invokes an injectable method. |
| 28 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 29 | final class SingleMethodInjector implements SingleMemberInjector { |
| 30 | private final MethodInvoker methodInvoker; |
| 31 | private final SingleParameterInjector<?>[] parameterInjectors; |
| 32 | private final InjectionPoint injectionPoint; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 33 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 34 | SingleMethodInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 35 | throws ErrorsException { |
| 36 | this.injectionPoint = injectionPoint; |
| 37 | final Method method = (Method) injectionPoint.getMember(); |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 38 | methodInvoker = createMethodInvoker(method); |
| 39 | parameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors); |
| 40 | } |
| 41 | |
| 42 | private MethodInvoker createMethodInvoker(final Method method) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 43 | |
| 44 | // We can't use FastMethod if the method is private. |
| 45 | int modifiers = method.getModifiers(); |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 46 | if (!Modifier.isPrivate(modifiers) && !Modifier.isProtected(modifiers)) { |
| 47 | /*if[AOP]*/ |
| 48 | final net.sf.cglib.reflect.FastMethod fastMethod |
| 49 | = BytecodeGen.newFastClass(method.getDeclaringClass(), Visibility.forMember(method)) |
| 50 | .getMethod(method); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 51 | |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 52 | return new MethodInvoker() { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 53 | public Object invoke(Object target, Object... parameters) |
| 54 | throws IllegalAccessException, InvocationTargetException { |
| 55 | return fastMethod.invoke(target, parameters); |
| 56 | } |
| 57 | }; |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 58 | /*end[AOP]*/ |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 59 | } |
| 60 | |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 61 | if (!Modifier.isPublic(modifiers)) { |
| 62 | method.setAccessible(true); |
| 63 | } |
| 64 | |
| 65 | return new MethodInvoker() { |
| 66 | public Object invoke(Object target, Object... parameters) |
| 67 | throws IllegalAccessException, InvocationTargetException { |
| 68 | return method.invoke(target, parameters); |
| 69 | } |
| 70 | }; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public InjectionPoint getInjectionPoint() { |
| 74 | return injectionPoint; |
| 75 | } |
| 76 | |
| 77 | public void inject(Errors errors, InternalContext context, Object o) { |
| 78 | Object[] parameters; |
| 79 | try { |
| 80 | parameters = SingleParameterInjector.getAll(errors, context, parameterInjectors); |
| 81 | } catch (ErrorsException e) { |
| 82 | errors.merge(e.getErrors()); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | try { |
| 87 | methodInvoker.invoke(o, parameters); |
| 88 | } catch (IllegalAccessException e) { |
| 89 | throw new AssertionError(e); // a security manager is blocking us, we're hosed |
| 90 | } catch (InvocationTargetException userException) { |
| 91 | Throwable cause = userException.getCause() != null |
| 92 | ? userException.getCause() |
| 93 | : userException; |
| 94 | errors.withSource(injectionPoint).errorInjectingMethod(cause); |
| 95 | } |
| 96 | } |
limpbizkit | a7184cf | 2008-11-30 01:01:36 +0000 | [diff] [blame] | 97 | } |