1 /**
2  * Copyright (C) 2008 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.spi;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import com.google.common.collect.ImmutableList;
22 import com.google.inject.Binder;
23 import com.google.inject.matcher.Matcher;
24 
25 import org.aopalliance.intercept.MethodInterceptor;
26 
27 import java.lang.reflect.Method;
28 import java.util.List;
29 
30 /**
31  * Registration of interceptors for matching methods of matching classes. Instances are created
32  * explicitly in a module using {@link com.google.inject.Binder#bindInterceptor(
33  * Matcher, Matcher, MethodInterceptor[]) bindInterceptor()} statements:
34  * <pre>
35  *     bindInterceptor(Matchers.subclassesOf(MyAction.class),
36  *         Matchers.annotatedWith(Transactional.class),
37  *         new MyTransactionInterceptor());</pre>
38  *
39  * or from an injectable type listener using {@link TypeEncounter#bindInterceptor(Matcher,
40  * org.aopalliance.intercept.MethodInterceptor[]) TypeEncounter.bindInterceptor()}.
41  *
42  * @author jessewilson@google.com (Jesse Wilson)
43  * @since 2.0
44  */
45 public final class InterceptorBinding implements Element {
46   private final Object source;
47   private final Matcher<? super Class<?>> classMatcher;
48   private final Matcher<? super Method> methodMatcher;
49   private final ImmutableList<MethodInterceptor> interceptors;
50 
InterceptorBinding( Object source, Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor[] interceptors)51   InterceptorBinding(
52       Object source,
53       Matcher<? super Class<?>> classMatcher,
54       Matcher<? super Method> methodMatcher,
55       MethodInterceptor[] interceptors) {
56     this.source = checkNotNull(source, "source");
57     this.classMatcher = checkNotNull(classMatcher, "classMatcher");
58     this.methodMatcher = checkNotNull(methodMatcher, "methodMatcher");
59     this.interceptors = ImmutableList.copyOf(interceptors);
60   }
61 
getSource()62   public Object getSource() {
63     return source;
64   }
65 
getClassMatcher()66   public Matcher<? super Class<?>> getClassMatcher() {
67     return classMatcher;
68   }
69 
getMethodMatcher()70   public Matcher<? super Method> getMethodMatcher() {
71     return methodMatcher;
72   }
73 
getInterceptors()74   public List<MethodInterceptor> getInterceptors() {
75     return interceptors;
76   }
77 
acceptVisitor(ElementVisitor<T> visitor)78   public <T> T acceptVisitor(ElementVisitor<T> visitor) {
79     return visitor.visit(this);
80   }
81 
applyTo(Binder binder)82   public void applyTo(Binder binder) {
83     binder.withSource(getSource()).bindInterceptor(classMatcher, methodMatcher,
84         interceptors.toArray(new MethodInterceptor[interceptors.size()]));
85   }
86 }
87