1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.bull.javamelody;
19
20 import java.lang.reflect.Method;
21
22 import org.springframework.aop.ClassFilter;
23 import org.springframework.aop.MethodMatcher;
24 import org.springframework.aop.Pointcut;
25
26 /**
27  * Pointcut composed of a delegate pointcut and an excluding pointcut.
28  * @author Emeric Vernat
29  */

30 class ExcludingPointcut implements Pointcut {
31     private final Pointcut delegatePointcut;
32     private Pointcut excludingPointcut;
33
34     private final MethodMatcher methodMatcher = new MethodMatcher() {
35         @Override
36         public boolean isRuntime() {
37             return getDelegatePointcut().getMethodMatcher().isRuntime()
38                     || getExcludingPointcut().getMethodMatcher().isRuntime();
39         }
40
41         @Override
42         public boolean matches(Method method, Class<?> targetClass) {
43             return getDelegatePointcut().getMethodMatcher().matches(method, targetClass)
44                     && !(getExcludingPointcut().getClassFilter().matches(targetClass)
45                             && getExcludingPointcut().getMethodMatcher().matches(method,
46                                     targetClass));
47         }
48
49         @Override
50         public boolean matches(Method method, Class<?> targetClass, Object... args) {
51             return getDelegatePointcut().getMethodMatcher().matches(method, targetClass, args)
52                     && !(getExcludingPointcut().getClassFilter().matches(targetClass)
53                             && getExcludingPointcut().getMethodMatcher().matches(method,
54                                     targetClass, args));
55         }
56     };
57
58     ExcludingPointcut(Pointcut delegatePointcut) {
59         super();
60         assert delegatePointcut != null;
61         this.delegatePointcut = delegatePointcut;
62     }
63
64     Pointcut exclude(Pointcut pointcut) {
65         assert pointcut != null;
66         this.excludingPointcut = pointcut;
67         return this;
68     }
69
70     Pointcut getDelegatePointcut() {
71         assert delegatePointcut != null;
72         return delegatePointcut;
73     }
74
75     Pointcut getExcludingPointcut() {
76         assert excludingPointcut != null;
77         return excludingPointcut;
78     }
79
80     @Override
81     public ClassFilter getClassFilter() {
82         return delegatePointcut.getClassFilter();
83     }
84
85     @Override
86     public MethodMatcher getMethodMatcher() {
87         return methodMatcher;
88     }
89 }
90