Java Utililty Methods Reflection Method Annotation

List of utility methods to do Reflection Method Annotation

Description

The list of methods to do Reflection Method Annotation are organized into topic(s).

Method

ListgetMethods(final Class t, final Class a)
get Methods
final List<Method> ms = new ArrayList<Method>();
Class<?> c = t;
while (c != null && c != Object.class) {
    for (final Method m : c.getDeclaredMethods())
        if (m.isAnnotationPresent(a)) {
            m.setAccessible(true);
            ms.add(m);
    c = c.getSuperclass();
return ms;
ListgetMethodsAnnotated(Class anno, Class holder)
get Methods Annotated
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;
ListgetMethodsAnnotatedBy(Class clazz, Class annotClass)
get Methods Annotated By
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;
...
ListgetMethodsAnnotatedWith(Class owner, Class annotationClass, boolean includeSuper)
get Methods Annotated With
List<Method> selectedMethods = new ArrayList<Method>();
Method[] allMethods = owner.getMethods();
for (Method method : allMethods) {
    if (!includeSuper && isMethodInherited(owner, method)) {
        continue;
    if (method.isAnnotationPresent(annotationClass)) {
        selectedMethods.add(method);
...
ListgetMethodsAnnotatedWith(Class clazz, Class annotClazz)
Gets a List of all the Method s in the given class, annotated with the given annotation class.
return Arrays.stream(clazz.getDeclaredMethods()).filter(m -> m.getClass().isAnnotationPresent(annotClazz))
        .collect(Collectors.toList());
SetgetMethodsAnnotatedWith(Class clazz, Class annotation)
Returns the given class's (and superclasses) declared methods that are marked with the given annotation
return getMethodsAnnotatedWith(clazz, annotation, true);
ListgetMethodsAnnotatedWith(final Class type, final Class annotation)
get Methods Annotated With
final List<Method> methods = new ArrayList<Method>();
Class<?> klass = type;
while (klass != Object.class) {
    final List<Method> allMethods = new ArrayList<Method>(Arrays.asList(klass.getDeclaredMethods()));
    for (final Method method : allMethods) {
        if (annotation == null || method.isAnnotationPresent(annotation)) {
            Annotation annotInstance = method.getAnnotation(annotation);
            methods.add(method);
...
Method[]getMethodsInAnnotation(Annotation anno)
Returns an array containing all methods occur in the given Annotation
Method[] methods = anno.annotationType().getDeclaredMethods();
return methods;
ListgetMethodsWithAnnotation(Class annotation, Class clazz)
Gets a list of methods with the given annotation
Method[] methods = clazz.getMethods();
List<Method> annotated = new ArrayList<Method>();
for (Method m : methods)
    if (m.isAnnotationPresent(annotation))
        annotated.add(m);
return annotated;
Method[]getMethodsWithAnnotation(Class type, Class obj)
get Methods With Annotation
return getMethodsWithAnnotation(type, obj, false);