关于将Java的@Age约束注解实现转换为Kotlin的技术咨询
Converting Java's @Age Constraint Annotation to Kotlin
Got it, let's work through converting that Java @Age validation annotation to clean Kotlin code. First, let's start with the full, corrected version of your original Java code (since it was truncated):
import javax.validation.Constraint; import javax.validation.Payload; import java.lang.annotation.*; @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) @Repeatable(Age.List.class) @Documented @Constraint(validatedBy = {}) public @interface Age { String message() default "Must be greater than {value}"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; int value() default 18; // Assuming this was missing, since it's referenced in the message @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE}) @Retention(RetentionPolicy.RUNTIME) @Documented @interface List { Age[] value(); } }
Here's the equivalent Kotlin code, optimized for Kotlin's annotation syntax:
import javax.validation.Constraint import javax.validation.Payload import kotlin.annotation.AnnotationRetention.RUNTIME import kotlin.annotation.AnnotationTarget.* @Target(FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER, FIELD, ANNOTATION_CLASS, CONSTRUCTOR, VALUE_PARAMETER, TYPE_USE) @Retention(RUNTIME) @Repeatable(Age.List::class) @Documented @Constraint(validatedBy = []) annotation class Age( val message: String = "Must be greater than {value}", val groups: Array<KClass<*>> = [], val payload: Array<KClass<out Payload>> = [], val value: Int = 18 // Matches the placeholder in the default message ) { @Target(FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER, FIELD, ANNOTATION_CLASS, CONSTRUCTOR, VALUE_PARAMETER, TYPE_USE) @Retention(RUNTIME) @Documented annotation class List(val value: Array<Age>) }
Key Notes on the Conversion:
- Annotation Targets: Kotlin uses
AnnotationTargetenum values that map closely to Java'sElementType, with Kotlin-specific additions likePROPERTY_GETTER/PROPERTY_SETTERto cover property accessors (since Kotlin properties compile to Java getter/setter methods). - Default Values: In Kotlin, annotation parameter defaults are declared directly in the annotation class constructor, which is more concise than Java's separate method-style declarations.
- KClass: Instead of Java's
Class<?>, Kotlin usesKClass<*>for class references in annotations. For thepayloadparameter,KClass<out Payload>matches Java'sClass<? extends Payload>to maintain the same upper-bounded wildcard behavior. - Repeatable Annotation: Kotlin references nested annotation classes via
::classsyntax (Kotlin's class literal notation) in the@Repeatableannotation. - Nested Annotation: The inner
Listannotation follows Kotlin's annotation structure, with its own set of targets and retention to mirror the parent annotation's behavior.
内容的提问来源于stack exchange,提问作者BrokenCranium
相关产品推荐
相关产品推荐

