All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.optionfactory.spring.upstream.annotations.Annotations Maven / Gradle / Ivy

There is a newer version: 20.1
Show newest version
package net.optionfactory.spring.upstream.annotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

public class Annotations {

    public static  List closestRepeatable(Method m, Class annotation) {
        final var manns = m.getAnnotationsByType(annotation);
        if (manns.length > 0) {
            return List.of(manns);
        }
        return closestRepeatable(m.getDeclaringClass(), annotation);
    }

    public static  List closestRepeatable(Class k, Class annotation) {
        for (var curr = k; curr != null; curr = curr.getSuperclass()) {
            var canns = curr.getAnnotationsByType(annotation);
            if (canns.length > 0) {
                return List.of(canns);
            }
        }
        return List.of();
    }

    public static  Optional closest(Method m, Class annotation) {
        final var mann = m.getAnnotation(annotation);
        if (mann != null) {
            return Optional.of(mann);
        }
        return closest(m.getDeclaringClass(), annotation);
    }

    public static  Optional closest(Class k, Class annotation) {
        for (var curr = k; curr != null; curr = curr.getSuperclass()) {
            var cann = curr.getAnnotation(annotation);
            if (cann != null) {
                return Optional.of(cann);
            }
        }
        return Optional.empty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy