blob: f9377a3c3d04b23ba6a15a3441508de66d6fc5d7 [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
crazyboblee1b82a8f2007-02-02 23:30:42 +000018
limpbizkit53664a72009-02-21 00:25:27 +000019import static com.google.inject.internal.Preconditions.checkNotNull;
limpbizkit5fb9d922008-10-14 23:35:56 +000020import com.google.inject.matcher.Matcher;
crazyboblee1b82a8f2007-02-02 23:30:42 +000021import java.lang.reflect.Method;
limpbizkit5fb9d922008-10-14 23:35:56 +000022import java.util.Arrays;
kevinb9na99dca72007-02-11 04:48:57 +000023import java.util.List;
24import org.aopalliance.intercept.MethodInterceptor;
crazyboblee1b82a8f2007-02-02 23:30:42 +000025
26/**
crazyboblee33ce0692007-02-13 23:53:46 +000027 * Ties a matcher to a method interceptor.
crazyboblee1b82a8f2007-02-02 23:30:42 +000028 *
29 * @author crazybob@google.com (Bob Lee)
30 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000031final class MethodAspect {
crazyboblee1b82a8f2007-02-02 23:30:42 +000032
limpbizkit5ae41eb2009-06-06 17:51:27 +000033 private final Matcher<? super Class<?>> classMatcher;
34 private final Matcher<? super Method> methodMatcher;
35 private final List<MethodInterceptor> interceptors;
crazyboblee1b82a8f2007-02-02 23:30:42 +000036
limpbizkit5fb9d922008-10-14 23:35:56 +000037 /**
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 */
crazyboblee33ce0692007-02-13 23:53:46 +000044 MethodAspect(Matcher<? super Class<?>> classMatcher,
limpbizkit3d58d6b2008-03-08 16:11:47 +000045 Matcher<? super Method> methodMatcher, List<MethodInterceptor> interceptors) {
kevinb9n1601ae52008-06-03 22:21:04 +000046 this.classMatcher = checkNotNull(classMatcher, "class matcher");
47 this.methodMatcher = checkNotNull(methodMatcher, "method matcher");
48 this.interceptors = checkNotNull(interceptors, "interceptors");
crazyboblee1b82a8f2007-02-02 23:30:42 +000049 }
50
limpbizkit5fb9d922008-10-14 23:35:56 +000051 MethodAspect(Matcher<? super Class<?>> classMatcher,
52 Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) {
53 this(classMatcher, methodMatcher, Arrays.asList(interceptors));
54 }
55
crazyboblee1b82a8f2007-02-02 23:30:42 +000056 boolean matches(Class<?> clazz) {
crazyboblee33ce0692007-02-13 23:53:46 +000057 return classMatcher.matches(clazz);
crazyboblee1b82a8f2007-02-02 23:30:42 +000058 }
59
60 boolean matches(Method method) {
crazyboblee33ce0692007-02-13 23:53:46 +000061 return methodMatcher.matches(method);
crazyboblee1b82a8f2007-02-02 23:30:42 +000062 }
63
crazyboblee62fcdde2007-02-03 02:10:13 +000064 List<MethodInterceptor> interceptors() {
65 return interceptors;
crazyboblee1b82a8f2007-02-02 23:30:42 +000066 }
67}