Java Reflection Method Annotation getMethodsAnnotated(Class anno, Class holder)

Here you can find the source of getMethodsAnnotated(Class anno, Class holder)

Description

get Methods Annotated

License

Apache License

Declaration

public static List<Method> getMethodsAnnotated(Class<? extends Annotation> anno, Class<?> holder) 

Method Source Code


//package com.java2s;
/*/*from w  w w .  ja  v a2  s .c o  m*/
 * Copyright 2002-2006,2009 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.annotation.Annotation;

import java.lang.reflect.Field;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {
    public static List<Method> getMethodsAnnotated(Class<? extends Annotation> anno, Class<?> holder) {
        ArrayList<Method> methods = new ArrayList<>();
        List<Method> all = Arrays.asList(holder.getMethods());
        all.stream().filter((method) -> (hasAnnotation(anno, method))).forEach((method) -> {
            methods.add(method);
        });
        return methods;
    }

    public static List<Method> getMethodsAnnotated(Class<? extends Annotation> anno, Object holder) {
        ArrayList<Method> methods = new ArrayList<>();
        List<Method> all = Arrays.asList(holder.getClass().getMethods());
        all.stream().filter((method) -> (hasAnnotation(anno, method))).forEach((method) -> {
            methods.add(method);
        });
        return methods;
    }

    public static Boolean hasAnnotation(Class<? extends Annotation> anno, Field field) {
        return field.isAnnotationPresent(anno);
    }

    public static Boolean hasAnnotation(Class<? extends Annotation> anno, Method meth) {
        return meth.isAnnotationPresent(anno);
    }

    public static Boolean hasAnnotation(Class<? extends Annotation> anno, Object obj) {
        return (getAnnotation(anno, obj) != null);
    }

    public static Boolean hasAnnotation(Class<? extends Annotation> anno, Class<?> cl) {
        return (getAnnotation(anno, cl) != null);
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Class<?> cl) {
        return cl.getAnnotation(anno);
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Object obj) {
        return obj.getClass().getAnnotation(anno);
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Method meth) {
        return meth.getAnnotation(anno);
    }

    public static Annotation getAnnotation(Class<? extends Annotation> anno, Field field) {
        return field.getAnnotation(anno);
    }
}

Related

  1. getMethodOrClassLevelAnnotation(Class annotationClass, Method method, Class clazz)
  2. getMethods(Annotation anno)
  3. getMethods(Class clazz, Class annotation)
  4. getMethods(final Class clazz, final Class annotation)
  5. getMethods(final Class t, final Class a)
  6. getMethodsAnnotatedBy(Class clazz, Class annotClass)
  7. getMethodsAnnotatedWith(Class owner, Class annotationClass, boolean includeSuper)
  8. getMethodsAnnotatedWith(Class clazz, Class annotClazz)
  9. getMethodsAnnotatedWith(Class clazz, Class annotation)