01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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: package org.kuali.core.util.spring;
17:
18: import java.lang.annotation.Annotation;
19: import java.lang.reflect.Method;
20:
21: import org.springframework.aop.ClassFilter;
22: import org.springframework.transaction.annotation.Transactional;
23:
24: /**
25: * Matches if the annotation specified during construction is present on the class or any of methods returned by class.getMethods().
26: */
27: public class ClassOrMethodAnnotationFilter implements ClassFilter {
28: private final Class<? extends Annotation> annotationType;
29:
30: public ClassOrMethodAnnotationFilter(
31: Class<? extends Annotation> annotationType) {
32: this .annotationType = annotationType;
33: }
34:
35: public boolean matches(Class clazz) {
36: boolean isAnnotationPresent = clazz
37: .isAnnotationPresent(this .annotationType);
38: if (!isAnnotationPresent) {
39: for (Method method : clazz.getMethods()) {
40: if (method.isAnnotationPresent(annotationType)) {
41: isAnnotationPresent = true;
42: if (Transactional.class.equals(this .annotationType)) {
43: throw new RuntimeException(
44: "The @Transactional annotation should be specified at the class level and overriden at the method level, if need be.");
45: }
46: break;
47: }
48: }
49: }
50: return isAnnotationPresent;
51: }
52: }
|