001/*
002 * Copyright 2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.support;
017
018import java.lang.annotation.Annotation;
019import java.lang.reflect.Method;
020import java.util.HashSet;
021import java.util.Set;
022
023import org.springframework.core.annotation.AnnotationUtils;
024
025/**
026 * Provides reflection based utilities for Spring Batch that are not available
027 * via Spring Core
028 *
029 * @author Michael Minella
030 * @since 2.2.6
031 */
032public class ReflectionUtils {
033
034        private ReflectionUtils() {}
035
036        /**
037         * Returns a {@link java.util.Set} of {@link java.lang.reflect.Method} instances that
038         * are annotated with the annotation provided.
039         *
040         * @param clazz The class to search for a method with the given annotation type
041         * @param annotationType The type of annotation to look for
042         * @return a set of {@link java.lang.reflect.Method} instances if any are found, an empty set if not.
043         */
044        @SuppressWarnings("rawtypes")
045        public static final Set<Method> findMethod(Class clazz, Class<? extends Annotation> annotationType) {
046
047                Method [] declaredMethods = org.springframework.util.ReflectionUtils.getAllDeclaredMethods(clazz);
048                Set<Method> results = new HashSet<>();
049
050                for (Method curMethod : declaredMethods) {
051                        Annotation annotation = AnnotationUtils.findAnnotation(curMethod, annotationType);
052
053                        if(annotation != null) {
054                                results.add(curMethod);
055                        }
056                }
057
058                return results;
059        }
060}