01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.google.inject;
16:
17: import java.lang.reflect.AccessibleObject;
18: import java.lang.reflect.Method;
19: import java.util.List;
20: import net.sf.cglib.proxy.MethodProxy;
21: import org.aopalliance.intercept.MethodInterceptor;
22: import org.aopalliance.intercept.MethodInvocation;
23:
24: /**
25: * Intercepts a method with a stack of interceptors.
26: *
27: * @author crazybob@google.com (Bob Lee)
28: */
29: class InterceptorStackCallback implements
30: net.sf.cglib.proxy.MethodInterceptor {
31:
32: final MethodInterceptor[] interceptors;
33: final Method method;
34:
35: public InterceptorStackCallback(Method method,
36: List<MethodInterceptor> interceptors) {
37: this .method = method;
38: this .interceptors = interceptors
39: .toArray(new MethodInterceptor[interceptors.size()]);
40: }
41:
42: public Object intercept(Object proxy, Method method,
43: Object[] arguments, MethodProxy methodProxy)
44: throws Throwable {
45: return new InterceptedMethodInvocation(proxy, methodProxy,
46: arguments).proceed();
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,
57: MethodProxy methodProxy, 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 ? methodProxy
67: .invokeSuper(proxy, arguments)
68: : interceptors[index].invoke(this );
69: } finally {
70: index--;
71: }
72: }
73:
74: public Method getMethod() {
75: return method;
76: }
77:
78: public Object[] getArguments() {
79: return arguments;
80: }
81:
82: public Object getThis() {
83: return proxy;
84: }
85:
86: public AccessibleObject getStaticPart() {
87: return getMethod();
88: }
89: }
90: }
|