| 1 | /* |
| 2 | * Copyright 2014 the original author or authors. |
| 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 | package org.springframework.batch.support; |
| 17 | |
| 18 | import org.springframework.core.annotation.AnnotationUtils; |
| 19 | |
| 20 | import java.lang.annotation.Annotation; |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | /** |
| 26 | * Provides reflection based utilities for Spring Batch that are not available |
| 27 | * via Spring Core |
| 28 | * |
| 29 | * @author Michael Minella |
| 30 | * @since 2.2.6 |
| 31 | */ |
| 32 | public class ReflectionUtils { |
| 33 | |
| 34 | private ReflectionUtils() {} |
| 35 | |
| 36 | /** |
| 37 | * Returns a {@link java.util.Set} of {@link java.lang.reflect.Method} instances that |
| 38 | * are annotated with the annotation provided. |
| 39 | * |
| 40 | * @param clazz The class to search for a method with the given annotation type |
| 41 | * @param annotationType The type of annotation to look for |
| 42 | * @return a set of {@link java.lang.reflect.Method} instances if any are found, an empty set if not. |
| 43 | */ |
| 44 | public static final Set<Method> findMethod(Class clazz, Class<? extends Annotation> annotationType) { |
| 45 | |
| 46 | Method [] declaredMethods = org.springframework.util.ReflectionUtils.getAllDeclaredMethods(clazz); |
| 47 | Set<Method> results = new HashSet<Method>(); |
| 48 | |
| 49 | for (Method curMethod : declaredMethods) { |
| 50 | Annotation annotation = AnnotationUtils.findAnnotation(curMethod, annotationType); |
| 51 | |
| 52 | if(annotation != null) { |
| 53 | results.add(curMethod); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return results; |
| 58 | } |
| 59 | } |