crazyboblee | b8cf1e5 | 2007-02-02 21:48:16 +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 | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 16 | |
crazyboblee | 10a3b02 | 2007-02-10 01:49:38 +0000 | [diff] [blame] | 17 | package com.google.inject; |
| 18 | |
crazyboblee | 4f79e40 | 2007-02-14 02:11:47 +0000 | [diff] [blame] | 19 | import com.google.inject.matcher.Matcher; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.ErrorHandler; |
| 21 | |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 22 | import java.lang.reflect.Method; |
kevinb9n | 6a565c7 | 2007-02-11 01:58:33 +0000 | [diff] [blame] | 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 25 | import java.util.Collection; |
| 26 | import java.util.Arrays; |
| 27 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 28 | import org.aopalliance.intercept.MethodInterceptor; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 29 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 30 | /** |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 31 | * Creates a {@link ProxyFactory}. |
| 32 | * |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 33 | * @author crazybob@google.com (Bob Lee) |
| 34 | */ |
crazyboblee | 10a3b02 | 2007-02-10 01:49:38 +0000 | [diff] [blame] | 35 | class ProxyFactoryBuilder { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 36 | |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 37 | final ErrorHandler errorHandler; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 38 | final List<MethodAspect> methodAspects = new ArrayList<MethodAspect>(); |
| 39 | |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 40 | ProxyFactoryBuilder(ErrorHandler errorHandler) { |
| 41 | this.errorHandler = errorHandler; |
| 42 | } |
| 43 | |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Applies the given method interceptor to the methods matched by the class |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 46 | * and method matchers. |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 47 | * |
crazyboblee | 33ce069 | 2007-02-13 23:53:46 +0000 | [diff] [blame] | 48 | * @param classMatcher matches classes the interceptor should apply to. For |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 49 | * example: {@code only(Runnable.class)}. |
crazyboblee | 33ce069 | 2007-02-13 23:53:46 +0000 | [diff] [blame] | 50 | * @param methodMatcher matches methods the interceptor should apply to. For |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 51 | * example: {@code annotatedWith(Transactional.class)}. |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 52 | * @param interceptors to apply |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 53 | */ |
crazyboblee | 33ce069 | 2007-02-13 23:53:46 +0000 | [diff] [blame] | 54 | public ProxyFactoryBuilder intercept(Matcher<? super Class<?>> classMatcher, |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 55 | Matcher<? super Method> methodMatcher, |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 56 | List<MethodInterceptor> interceptors) { |
| 57 | methodAspects.add(new MethodAspect(classMatcher, methodMatcher, interceptors)); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 58 | return this; |
| 59 | } |
| 60 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 61 | public ProxyFactoryBuilder intercept(Matcher<? super Class<?>> classMatcher, |
| 62 | Matcher<? super Method> methodMatcher, |
| 63 | MethodInterceptor... interceptors) { |
| 64 | return intercept(classMatcher, methodMatcher, Arrays.asList(interceptors)); |
| 65 | } |
| 66 | |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 67 | /** |
| 68 | * Creates a {@code ProxyFactory}. |
| 69 | */ |
| 70 | public ProxyFactory create() { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 71 | return new ProxyFactory(errorHandler, new ArrayList<MethodAspect>(methodAspects)); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 72 | } |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 73 | } |