Java Reflection Method Annotation getMethodsAnnotatedBy(Class clazz, Class annotClass)

Here you can find the source of getMethodsAnnotatedBy(Class clazz, Class annotClass)

Description

get Methods Annotated By

License

Open Source License

Declaration

private static List<Method> getMethodsAnnotatedBy(Class<?> clazz, Class<? extends Annotation> annotClass) 

Method Source Code


//package com.java2s;
/*/*from www . ja  v a  2s .c o  m*/
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 3 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 3 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

public class Main {
    private static List<Method> getMethodsAnnotatedBy(Class<?> clazz, Class<? extends Annotation> annotClass) {
        List<Method> annotMethList = new ArrayList<>(
                Arrays.asList(clazz.getDeclaredMethods()).stream().filter(dm -> {
                    return dm.getAnnotation(annotClass) != null;
                }).collect(Collectors.toList()));

        if (clazz.getSuperclass() != Object.class) {
            annotMethList.addAll(getMethodsAnnotatedBy(clazz.getSuperclass(), annotClass));
        }

        return annotMethList;
    }
}

Related

  1. getMethods(Annotation anno)
  2. getMethods(Class clazz, Class annotation)
  3. getMethods(final Class clazz, final Class annotation)
  4. getMethods(final Class t, final Class a)
  5. getMethodsAnnotated(Class anno, Class holder)
  6. getMethodsAnnotatedWith(Class owner, Class annotationClass, boolean includeSuper)
  7. getMethodsAnnotatedWith(Class clazz, Class annotClazz)
  8. getMethodsAnnotatedWith(Class clazz, Class annotation)
  9. getMethodsAnnotatedWith(final Class type, final Class annotation)