crazyboblee | 62fcdde | 2007-02-03 02:10:13 +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 | */ |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 16 | |
crazyboblee | 10a3b02 | 2007-02-10 01:49:38 +0000 | [diff] [blame] | 17 | package com.google.inject; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 18 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 19 | import java.lang.reflect.AccessibleObject; |
| 20 | import java.lang.reflect.Method; |
| 21 | import java.util.List; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 22 | import net.sf.cglib.proxy.MethodProxy; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 23 | import org.aopalliance.intercept.MethodInterceptor; |
| 24 | import org.aopalliance.intercept.MethodInvocation; |
| 25 | |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Intercepts a method with a stack of interceptors. |
| 28 | * |
| 29 | * @author crazybob@google.com (Bob Lee) |
| 30 | */ |
| 31 | class InterceptorStackCallback implements net.sf.cglib.proxy.MethodInterceptor { |
| 32 | |
| 33 | final MethodInterceptor[] interceptors; |
| 34 | final Method method; |
| 35 | |
| 36 | public InterceptorStackCallback(Method method, |
| 37 | List<MethodInterceptor> interceptors) { |
| 38 | this.method = method; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 39 | this.interceptors |
| 40 | = interceptors.toArray(new MethodInterceptor[interceptors.size()]); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | public Object intercept(Object proxy, Method method, Object[] arguments, |
| 44 | MethodProxy methodProxy) throws Throwable { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 45 | return new InterceptedMethodInvocation(proxy, methodProxy, arguments) |
| 46 | .proceed(); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | class InterceptedMethodInvocation implements MethodInvocation { |
| 50 | |
| 51 | final Object proxy; |
| 52 | final Object[] arguments; |
| 53 | final MethodProxy methodProxy; |
| 54 | int index = -1; |
| 55 | |
| 56 | public InterceptedMethodInvocation(Object proxy, MethodProxy methodProxy, |
| 57 | Object[] arguments) { |
| 58 | this.proxy = proxy; |
| 59 | this.methodProxy = methodProxy; |
| 60 | this.arguments = arguments; |
| 61 | } |
| 62 | |
| 63 | public Object proceed() throws Throwable { |
| 64 | try { |
| 65 | index++; |
| 66 | return index == interceptors.length |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 67 | ? methodProxy.invokeSuper(proxy, arguments) |
| 68 | : interceptors[index].invoke(this); |
| 69 | } |
| 70 | finally { |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 71 | index--; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public Method getMethod() { |
| 76 | return method; |
| 77 | } |
| 78 | |
| 79 | public Object[] getArguments() { |
| 80 | return arguments; |
| 81 | } |
| 82 | |
| 83 | public Object getThis() { |
| 84 | return proxy; |
| 85 | } |
| 86 | |
| 87 | public AccessibleObject getStaticPart() { |
| 88 | return getMethod(); |
| 89 | } |
| 90 | } |
| 91 | } |