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 */
16
17package com.google.inject.internal;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.inject.matcher.Matcher;
22
23import org.aopalliance.intercept.MethodInterceptor;
24
25import java.lang.reflect.Method;
26import java.util.Arrays;
27import java.util.List;
28
29/**
30 * Ties a matcher to a method interceptor.
31 *
32 * @author crazybob@google.com (Bob Lee)
33 */
34final class MethodAspect {
35
36  private final Matcher<? super Class<?>> classMatcher;
37  private final Matcher<? super Method> methodMatcher;
38  private final List<MethodInterceptor> interceptors;
39
40  /**
41   * @param classMatcher matches classes the interceptor should apply to. For example: {@code
42   *     only(Runnable.class)}.
43   * @param methodMatcher matches methods the interceptor should apply to. For example: {@code
44   *     annotatedWith(Transactional.class)}.
45   * @param interceptors to apply
46   */
47  MethodAspect(Matcher<? super Class<?>> classMatcher,
48      Matcher<? super Method> methodMatcher, List<MethodInterceptor> interceptors) {
49    this.classMatcher = checkNotNull(classMatcher, "class matcher");
50    this.methodMatcher = checkNotNull(methodMatcher, "method matcher");
51    this.interceptors = checkNotNull(interceptors, "interceptors");
52  }
53
54  MethodAspect(Matcher<? super Class<?>> classMatcher,
55      Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) {
56    this(classMatcher, methodMatcher, Arrays.asList(interceptors));
57  }
58
59  boolean matches(Class<?> clazz) {
60    return classMatcher.matches(clazz);
61  }
62
63  boolean matches(Method method) {
64    return methodMatcher.matches(method);
65  }
66
67  List<MethodInterceptor> interceptors() {
68    return interceptors;
69  }
70}
71