Skip to content
Anjunar/ DOCS

Annotations

Every element of the model is Annotated. The difference between annotations and declaredAnnotations is where the search stops, and scala-universe looks further than plain reflection.

Lookup

findAnnotation

findAnnotation returns the annotation of exactly that type, or null. findDeclaredAnnotation looks only at the element itself.

scala
val column = TypeResolver.resolve(classOf[Customer]).findField("nickName") val schemaId = column.findAnnotation(classOf[SchemaId]) if schemaId != null then println(schemaId.value()) // f34e45b6
Inheritance

Where annotations travel

annotations follows the hierarchy for every kind of element. This is independent of Java's @Inherited, which only applies to class annotations of superclasses.

ResolvedClass
Its own plus those of every superclass and interface.
ResolvedMethod
Its own plus those of every method it overrides.
ResolvedField
Its own plus those of the fields it hides.
ResolvedParameter
Its own plus those of the same parameter in overridden methods.
AbstractProperty
Those of its field, getter and setter together.
Scala

Which annotations exist at runtime

Only Java annotations with RetentionPolicy.RUNTIME reach the class file in a form reflection can read. Scala annotations extending StaticAnnotation are invisible here.

scala
import scala.annotation.meta.{field, getter, setter} class Account { @(Exposed @field) var owner: String = "Ada" // on the field @(Exposed @getter) var email: String = "" // on the method email() @Exposed var phone: String = "" // Scala's default target for a var: the field }
Meta-annotations pick the target

A Scala var or val is a field plus accessors. @field, @getter and @setter say which of them carries the annotation; properties find it either way, because they merge field and accessors.