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