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

io.orangebuffalo.testcontainers.playwright.junit.AnnotationsUtils.kt Maven / Gradle / Ivy

There is a newer version: 0.11.13
Show newest version
package io.orangebuffalo.testcontainers.playwright.junit

import java.lang.reflect.AnnotatedElement
import kotlin.reflect.KClass

internal fun hasAnnotation(element: AnnotatedElement, annotationClass: KClass): Boolean {
    return findAnnotation(element, annotationClass) != null
            || findMetaAnnotation(element, annotationClass) != null
}

internal fun  getAnnotation(element: AnnotatedElement, annotationClass: KClass): A? {
    return findAnnotation(element, annotationClass)
        ?: findMetaAnnotation(element, annotationClass)
}

private fun  findAnnotation(element: AnnotatedElement, annotationClass: KClass): A? {
    return element.getAnnotation(annotationClass.java)
}

@Suppress("UNCHECKED_CAST")
private fun  findMetaAnnotation(
    element: AnnotatedElement,
    annotationClass: KClass,
    visited: MutableSet = mutableSetOf()
): A? {
    val annotations = element.annotations
    for (annotation in annotations) {
        if (annotation.annotationClass == annotationClass) {
            return annotation as A
        }
        val next = annotation.annotationClass.java
        if (visited.add(next)) {
            val nextResult = findMetaAnnotation(next, annotationClass, visited)
            if (nextResult != null) {
                return nextResult
            }
        }
    }
    return null
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy