blob: 053a40618554033a227a5a006e4b1cb28235c983 [file] [log] [blame]
crazybobleeb8cf1e52007-02-02 21:48:16 +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 */
crazybobleee3adfd62007-02-02 21:30:08 +000016
crazyboblee10a3b022007-02-10 01:49:38 +000017package com.google.inject;
18
crazyboblee4f79e402007-02-14 02:11:47 +000019import com.google.inject.matcher.Matcher;
limpbizkit916f5482008-04-16 20:51:14 +000020import com.google.inject.internal.ErrorHandler;
21
crazyboblee1b82a8f2007-02-02 23:30:42 +000022import java.lang.reflect.Method;
kevinb9n6a565c72007-02-11 01:58:33 +000023import java.util.ArrayList;
24import java.util.List;
limpbizkit3d58d6b2008-03-08 16:11:47 +000025import java.util.Collection;
26import java.util.Arrays;
27
kevinb9na99dca72007-02-11 04:48:57 +000028import org.aopalliance.intercept.MethodInterceptor;
crazyboblee1b82a8f2007-02-02 23:30:42 +000029
crazybobleee3adfd62007-02-02 21:30:08 +000030/**
crazyboblee1b82a8f2007-02-02 23:30:42 +000031 * Creates a {@link ProxyFactory}.
32 *
crazybobleee3adfd62007-02-02 21:30:08 +000033 * @author crazybob@google.com (Bob Lee)
34 */
crazyboblee10a3b022007-02-10 01:49:38 +000035class ProxyFactoryBuilder {
crazybobleee3adfd62007-02-02 21:30:08 +000036
limpbizkit916f5482008-04-16 20:51:14 +000037 final ErrorHandler errorHandler;
crazyboblee1b82a8f2007-02-02 23:30:42 +000038 final List<MethodAspect> methodAspects = new ArrayList<MethodAspect>();
39
limpbizkit916f5482008-04-16 20:51:14 +000040 ProxyFactoryBuilder(ErrorHandler errorHandler) {
41 this.errorHandler = errorHandler;
42 }
43
crazyboblee1b82a8f2007-02-02 23:30:42 +000044 /**
45 * Applies the given method interceptor to the methods matched by the class
crazyboblee71733532007-02-14 02:35:21 +000046 * and method matchers.
crazyboblee62fcdde2007-02-03 02:10:13 +000047 *
crazyboblee33ce0692007-02-13 23:53:46 +000048 * @param classMatcher matches classes the interceptor should apply to. For
kevinb9na99dca72007-02-11 04:48:57 +000049 * example: {@code only(Runnable.class)}.
crazyboblee33ce0692007-02-13 23:53:46 +000050 * @param methodMatcher matches methods the interceptor should apply to. For
kevinb9na99dca72007-02-11 04:48:57 +000051 * example: {@code annotatedWith(Transactional.class)}.
crazyboblee62fcdde2007-02-03 02:10:13 +000052 * @param interceptors to apply
crazyboblee1b82a8f2007-02-02 23:30:42 +000053 */
crazyboblee33ce0692007-02-13 23:53:46 +000054 public ProxyFactoryBuilder intercept(Matcher<? super Class<?>> classMatcher,
crazyboblee71733532007-02-14 02:35:21 +000055 Matcher<? super Method> methodMatcher,
limpbizkit3d58d6b2008-03-08 16:11:47 +000056 List<MethodInterceptor> interceptors) {
57 methodAspects.add(new MethodAspect(classMatcher, methodMatcher, interceptors));
crazyboblee1b82a8f2007-02-02 23:30:42 +000058 return this;
59 }
60
limpbizkit3d58d6b2008-03-08 16:11:47 +000061 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
crazyboblee1b82a8f2007-02-02 23:30:42 +000067 /**
68 * Creates a {@code ProxyFactory}.
69 */
70 public ProxyFactory create() {
limpbizkit916f5482008-04-16 20:51:14 +000071 return new ProxyFactory(errorHandler, new ArrayList<MethodAspect>(methodAspects));
crazyboblee1b82a8f2007-02-02 23:30:42 +000072 }
crazybobleee3adfd62007-02-02 21:30:08 +000073}