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 that identifies methods/classes with the {@link MonitoredWithSpring} annotation.
28  *
29  * Inspired by Erik van Oosten (Java Simon, Licence LGPL)
30  * @author Emeric Vernat
31  */

32 public class MonitoredWithAnnotationPointcut implements Pointcut {
33     /**
34      * @return a class filter that lets all class through.
35      */

36     @Override
37     public ClassFilter getClassFilter() {
38         return ClassFilter.TRUE;
39     }
40
41     /**
42      * @return a method matcher that matches any method that has the {@link MonitoredWithSpring} annotation,
43      *         or is in a class with the {@link MonitoredWithSpring} annotation
44      */

45     @Override
46     public MethodMatcher getMethodMatcher() {
47         return MonitoredMethodMatcher.INSTANCE;
48     }
49
50     private enum MonitoredMethodMatcher implements MethodMatcher {
51         INSTANCE;
52
53         /** {@inheritDoc} */
54         @Override
55         public boolean matches(Method method, Class<?> targetClass) {
56             return targetClass.isAnnotationPresent(MonitoredWithSpring.class)
57                     || method.getDeclaringClass().isAnnotationPresent(MonitoredWithSpring.class)
58                     || method.isAnnotationPresent(MonitoredWithSpring.class);
59         }
60
61         /** {@inheritDoc} */
62         @Override
63         public boolean isRuntime() {
64             return false;
65         }
66
67         /** {@inheritDoc} */
68         @Override
69         public boolean matches(Method method, Class<?> targetClass, Object... args) {
70             throw new UnsupportedOperationException("This is not a runtime method matcher");
71         }
72     }
73 }
74