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 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 18 | |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 19 | import static com.google.inject.internal.Preconditions.checkNotNull; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 20 | import com.google.inject.matcher.Matcher; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 21 | import java.lang.reflect.Method; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 22 | import java.util.Arrays; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 23 | import java.util.List; |
| 24 | import org.aopalliance.intercept.MethodInterceptor; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
crazyboblee | 33ce069 | 2007-02-13 23:53:46 +0000 | [diff] [blame] | 27 | * Ties a matcher to a method interceptor. |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 28 | * |
| 29 | * @author crazybob@google.com (Bob Lee) |
| 30 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 31 | final class MethodAspect { |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 32 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 33 | private final Matcher<? super Class<?>> classMatcher; |
| 34 | private final Matcher<? super Method> methodMatcher; |
| 35 | private final List<MethodInterceptor> interceptors; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 36 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 37 | /** |
| 38 | * @param classMatcher matches classes the interceptor should apply to. For example: {@code |
| 39 | * only(Runnable.class)}. |
| 40 | * @param methodMatcher matches methods the interceptor should apply to. For example: {@code |
| 41 | * annotatedWith(Transactional.class)}. |
| 42 | * @param interceptors to apply |
| 43 | */ |
crazyboblee | 33ce069 | 2007-02-13 23:53:46 +0000 | [diff] [blame] | 44 | MethodAspect(Matcher<? super Class<?>> classMatcher, |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 45 | Matcher<? super Method> methodMatcher, List<MethodInterceptor> interceptors) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 46 | this.classMatcher = checkNotNull(classMatcher, "class matcher"); |
| 47 | this.methodMatcher = checkNotNull(methodMatcher, "method matcher"); |
| 48 | this.interceptors = checkNotNull(interceptors, "interceptors"); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 49 | } |
| 50 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 51 | MethodAspect(Matcher<? super Class<?>> classMatcher, |
| 52 | Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) { |
| 53 | this(classMatcher, methodMatcher, Arrays.asList(interceptors)); |
| 54 | } |
| 55 | |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 56 | boolean matches(Class<?> clazz) { |
crazyboblee | 33ce069 | 2007-02-13 23:53:46 +0000 | [diff] [blame] | 57 | return classMatcher.matches(clazz); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | boolean matches(Method method) { |
crazyboblee | 33ce069 | 2007-02-13 23:53:46 +0000 | [diff] [blame] | 61 | return methodMatcher.matches(method); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 62 | } |
| 63 | |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 64 | List<MethodInterceptor> interceptors() { |
| 65 | return interceptors; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 66 | } |
| 67 | } |