blob: d0f4c9504f3910035f07bb5ac89515cf44adfe6f [file] [log] [blame]
limpbizkit06898062008-11-02 05:14:55 +00001/**
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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit06898062008-11-02 05:14:55 +000018
limpbizkit06898062008-11-02 05:14:55 +000019import com.google.inject.internal.BytecodeGen.Visibility;
limpbizkit5ae41eb2009-06-06 17:51:27 +000020import com.google.inject.internal.InjectorImpl.MethodInvoker;
limpbizkit06898062008-11-02 05:14:55 +000021import com.google.inject.spi.InjectionPoint;
22import java.lang.reflect.InvocationTargetException;
23import java.lang.reflect.Method;
24import java.lang.reflect.Modifier;
limpbizkit06898062008-11-02 05:14:55 +000025
26/**
27 * Invokes an injectable method.
28 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000029final class SingleMethodInjector implements SingleMemberInjector {
30 private final MethodInvoker methodInvoker;
31 private final SingleParameterInjector<?>[] parameterInjectors;
32 private final InjectionPoint injectionPoint;
limpbizkit06898062008-11-02 05:14:55 +000033
limpbizkit5ae41eb2009-06-06 17:51:27 +000034 SingleMethodInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors)
limpbizkit06898062008-11-02 05:14:55 +000035 throws ErrorsException {
36 this.injectionPoint = injectionPoint;
37 final Method method = (Method) injectionPoint.getMember();
limpbizkit4f6274a2009-02-19 21:57:55 +000038 methodInvoker = createMethodInvoker(method);
39 parameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
40 }
41
42 private MethodInvoker createMethodInvoker(final Method method) {
limpbizkit06898062008-11-02 05:14:55 +000043
44 // We can't use FastMethod if the method is private.
45 int modifiers = method.getModifiers();
limpbizkit4f6274a2009-02-19 21:57:55 +000046 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);
limpbizkit06898062008-11-02 05:14:55 +000051
limpbizkit4f6274a2009-02-19 21:57:55 +000052 return new MethodInvoker() {
limpbizkit06898062008-11-02 05:14:55 +000053 public Object invoke(Object target, Object... parameters)
54 throws IllegalAccessException, InvocationTargetException {
55 return fastMethod.invoke(target, parameters);
56 }
57 };
limpbizkit4f6274a2009-02-19 21:57:55 +000058 /*end[AOP]*/
limpbizkit06898062008-11-02 05:14:55 +000059 }
60
limpbizkit4f6274a2009-02-19 21:57:55 +000061 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 };
limpbizkit06898062008-11-02 05:14:55 +000071 }
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 }
limpbizkita7184cf2008-11-30 01:01:36 +000097}