io.github.graphglue.graphql.extensions.kClassExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphglue-core Show documentation
Show all versions of graphglue-core Show documentation
A library to develop annotation-based code-first GraphQL servers using GraphQL Kotlin, Spring Boot and Neo4j - excluding Spring GraphQL server dependencies
package io.github.graphglue.graphql.extensions
import org.springframework.core.annotation.AnnotatedElementUtils
import org.springframework.core.annotation.AnnotationUtils
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
/**
* Uses [AnnotationUtils.findAnnotation] to find an annotation of type [A]
*
* @param A the type of the [Annotation] to find
* @return the found [Annotation] or `null` if not found
*/
inline fun KClass<*>.springFindAnnotation(): A? {
return AnnotationUtils.findAnnotation(this.java, A::class.java)
}
/**
* Uses [AnnotatedElementUtils.findMergedRepeatableAnnotations] to find all repeatable annotations of type [A]
* Includes annotations on superclasses
*
* @param A the type of the [Annotation]s to find
* @return the found [Annotation]s (can be empty)
*/
inline fun KClass<*>.springFindRepeatableAnnotations(): Collection {
return AnnotatedElementUtils.findMergedRepeatableAnnotations(this.java, A::class.java)
}
/**
* Uses [AnnotatedElementUtils.getMergedRepeatableAnnotations] to find all repeatable annotations of type [A]
* Excludes annotations on superclasses
*
* @param A the type of the [Annotation]s to find
* @return the found [Annotation]s (can be empty)
*/
inline fun KClass<*>.springGetRepeatableAnnotations(): Collection {
return AnnotatedElementUtils.getMergedRepeatableAnnotations(this.java, A::class.java)
}
/**
* Finds a property by name
*
* @param property the name of the property
* @return the found property
*/
fun KClass<*>.findProperty(property: String): KProperty1<*, *> {
return this.members.filterIsInstance>().first { it.name == property }
}