blob: 1e8e0320a2e9a50670ce1af3c937ece708c68aa6 [file] [log] [blame]
crazyboblee62fcdde2007-02-03 02:10:13 +00001/**
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 */
crazyboblee1b82a8f2007-02-02 23:30:42 +000016
crazyboblee10a3b022007-02-10 01:49:38 +000017package com.google.inject;
crazyboblee1b82a8f2007-02-02 23:30:42 +000018
kevinb9na99dca72007-02-11 04:48:57 +000019import java.lang.reflect.AccessibleObject;
20import java.lang.reflect.Method;
21import java.util.List;
crazyboblee1b82a8f2007-02-02 23:30:42 +000022import net.sf.cglib.proxy.MethodProxy;
crazyboblee1b82a8f2007-02-02 23:30:42 +000023import org.aopalliance.intercept.MethodInterceptor;
24import org.aopalliance.intercept.MethodInvocation;
25
crazyboblee1b82a8f2007-02-02 23:30:42 +000026/**
27 * Intercepts a method with a stack of interceptors.
28 *
29 * @author crazybob@google.com (Bob Lee)
30 */
31class 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;
limpbizkit696c5cd2008-12-30 21:48:17 +000039 this.interceptors = interceptors.toArray(new MethodInterceptor[interceptors.size()]);
crazyboblee1b82a8f2007-02-02 23:30:42 +000040 }
41
42 public Object intercept(Object proxy, Method method, Object[] arguments,
43 MethodProxy methodProxy) throws Throwable {
limpbizkit696c5cd2008-12-30 21:48:17 +000044 return new InterceptedMethodInvocation(proxy, methodProxy, arguments).proceed();
crazyboblee1b82a8f2007-02-02 23:30:42 +000045 }
46
47 class InterceptedMethodInvocation implements MethodInvocation {
48
49 final Object proxy;
50 final Object[] arguments;
51 final MethodProxy methodProxy;
52 int index = -1;
53
54 public InterceptedMethodInvocation(Object proxy, MethodProxy methodProxy,
55 Object[] arguments) {
56 this.proxy = proxy;
57 this.methodProxy = methodProxy;
58 this.arguments = arguments;
59 }
60
61 public Object proceed() throws Throwable {
62 try {
63 index++;
64 return index == interceptors.length
kevinb9na99dca72007-02-11 04:48:57 +000065 ? methodProxy.invokeSuper(proxy, arguments)
66 : interceptors[index].invoke(this);
67 }
68 finally {
crazyboblee1b82a8f2007-02-02 23:30:42 +000069 index--;
70 }
71 }
72
73 public Method getMethod() {
74 return method;
75 }
76
77 public Object[] getArguments() {
78 return arguments;
79 }
80
81 public Object getThis() {
82 return proxy;
83 }
84
85 public AccessibleObject getStaticPart() {
86 return getMethod();
87 }
88 }
89}