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 import java.lang.reflect.Method;
25 import java.util.List;
26 import org.aopalliance.intercept.MethodInterceptor;
27 
28 /**
29  * Registration of interceptors for matching methods of matching classes. Instances are created
30  * explicitly in a module using {@link com.google.inject.Binder#bindInterceptor( Matcher, Matcher,
31  * MethodInterceptor[]) bindInterceptor()} statements:
32  *
33  * <pre>
34  *     bindInterceptor(Matchers.subclassesOf(MyAction.class),
35  *         Matchers.annotatedWith(Transactional.class),
36  *         new MyTransactionInterceptor());</pre>
37  *
38  * or from an injectable type listener using {@link TypeEncounter#bindInterceptor(Matcher,
39  * org.aopalliance.intercept.MethodInterceptor[]) TypeEncounter.bindInterceptor()}.
40  *
41  * @author jessewilson@google.com (Jesse Wilson)
42  * @since 2.0
43  */
44 public final class InterceptorBinding implements Element {
45   private final Object source;
46   private final Matcher<? super Class<?>> classMatcher;
47   private final Matcher<? super Method> methodMatcher;
48   private final ImmutableList<MethodInterceptor> interceptors;
49 
InterceptorBinding( Object source, Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor[] interceptors)50   InterceptorBinding(
51       Object source,
52       Matcher<? super Class<?>> classMatcher,
53       Matcher<? super Method> methodMatcher,
54       MethodInterceptor[] interceptors) {
55     this.source = checkNotNull(source, "source");
56     this.classMatcher = checkNotNull(classMatcher, "classMatcher");
57     this.methodMatcher = checkNotNull(methodMatcher, "methodMatcher");
58     this.interceptors = ImmutableList.copyOf(interceptors);
59   }
60 
61   @Override
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 
78   @Override
acceptVisitor(ElementVisitor<T> visitor)79   public <T> T acceptVisitor(ElementVisitor<T> visitor) {
80     return visitor.visit(this);
81   }
82 
83   @Override
applyTo(Binder binder)84   public void applyTo(Binder binder) {
85     binder
86         .withSource(getSource())
87         .bindInterceptor(
88             classMatcher,
89             methodMatcher,
90             interceptors.toArray(new MethodInterceptor[interceptors.size()]));
91   }
92 }
93