Annotated properties
AnnotationIntrospector builds properties from the fields and methods that carry a given annotation. It understands Scala accessors as well as JavaBean names.
Selection
The annotation decides
Only annotated fields and methods become properties. For each, the introspector finds the matching getter and setter: name and name_$eq in Scala, getName, isName and setName in Java.
java
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Exposed {}
scala
import com.anjunar.scala.universe.introspector.AnnotationIntrospector
import scala.annotation.meta.field
class Account {
@(Exposed @field) var owner: String = "Ada"
var internal: String = "hidden"
}
val model = AnnotationIntrospector.createWithType(classOf[Account], classOf[Exposed])
println(model.properties.map(_.name).mkString(", ")) // owner
println(model.findProperty("owner").isWriteable) // true
Names
How a member becomes a property name
A field keeps its name. An annotated method is mapped back to the property it belongs to.
- owner · owner_$eq
- Scala getter and setter → owner.
- getOwner · isActive · setOwner
- JavaBean names → owner, active, owner.
- anything else
- The method name itself.
Use
The base of json-mapper
json-mapper builds its model with AnnotationIntrospector and @JsonbProperty. The same pattern fits any framework that lets classes opt in property by property.
scala
val model = AnnotationIntrospector.create(TypeResolver.resolve(classOf[UserDto]), classOf[JsonbProperty])
model.properties.foreach(property => println(s"${property.name}: ${property.propertyType.name}"))
Output
name: String
age: Integer
address: AddressDto