Hi,
Consider the following delegate that handles a String property. Delegation is used to trim the String value before setting it.
class TrimmedStringProperty(var value: String?) {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String? = value
operator fun setValue(thisRef: Any?, property: KProperty<*>, v: String?) {
value = v?.trim()
}
}
Here is a class using the above:
import org.hibernate.validator.constraints.NotBlank
import javax.validation.constraints.Size
class Jsr303Bean {
@delegate:NotBlank
@delegate:Size(max = 12)
var something: String? by TrimmedStringProperty(null)
}
I’m trying to validate the delegated property following JSR 303. On doing this I observed the following error:
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'TrimmedStringProperty'. Check configuration for 'description$delegate'
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:229)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:310)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping(ConstraintTree.java:244)
What am I missing here? I understand that the use-site mechanism is used to attach annotations to fields/getter/setter etc. (How) does it work in conjunction with delegates?
Thanks,
Manoj