Convert Java annotation non-constant field to Kotlin

Hello) I have next annotation in java:

@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatedTest {
	String CURRENT_REPETITION_PLACEHOLDER = "{currentRepetition}";
	String TOTAL_REPETITIONS_PLACEHOLDER = "{totalRepetitions}";
	String SHORT_DISPLAY_NAME = "repetition " + CURRENT_REPETITION_PLACEHOLDER + " of " + TOTAL_REPETITIONS_PLACEHOLDER;
	int value();
}

When I was converting this code to Kotlin I’ve got the error:
Default value of annotation parameter must be a compile-time constant

@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyRepeatedTest(val value: Int,
                                val CURRENT_REPETITION_PLACEHOLDER: String = "{currentRepetition}",
                                val TOTAL_REPETITIONS_PLACEHOLDER: String = "{totalRepetitions}",
                                val SHORT_DISPLAY_NAME: String = "repetition " + CURRENT_REPETITION_PLACEHOLDER + " of " + TOTAL_REPETITIONS_PLACEHOLDER
)

How can I change
val SHORT_DISPLAY_NAME: String = "repetition " + CURRENT_REPETITION_PLACEHOLDER + " of " + TOTAL_REPETITIONS_PLACEHOLDER
)
so that won’t get this error?

You can extract constants as global private:

private const val CURRENT_DEFAULT = "{currentRepetition}"
private const val CURRENT_TOTAL = "{totalRepetitions}"

@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class MyRepeatedTest(
    val value: Int,
    val CURRENT_REPETITION_PLACEHOLDER: String = CURRENT_DEFAULT,
    val TOTAL_REPETITIONS_PLACEHOLDER: String = CURRENT_TOTAL,
    val SHORT_DISPLAY_NAME: String = "repetition $CURRENT_DEFAULT of $CURRENT_TOTAL"
)
1 Like

Thank you!) It’s work!:slight_smile: