You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于将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 AnnotationTarget enum values that map closely to Java's ElementType, with Kotlin-specific additions like PROPERTY_GETTER/PROPERTY_SETTER to 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 uses KClass<*> for class references in annotations. For the payload parameter, KClass<out Payload> matches Java's Class<? extends Payload> to maintain the same upper-bounded wildcard behavior.
  • Repeatable Annotation: Kotlin references nested annotation classes via ::class syntax (Kotlin's class literal notation) in the @Repeatable annotation.
  • Nested Annotation: The inner List annotation follows Kotlin's annotation structure, with its own set of targets and retention to mirror the parent annotation's behavior.

内容的提问来源于stack exchange,提问作者BrokenCranium

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:54:05