You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/jvm/jvm-records.md
+23-26Lines changed: 23 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -65,45 +65,24 @@ JVM records require the `16` target version or higher of the generated JVM bytec
65
65
66
66
To specify it explicitly, use the `jvmTarget` compiler option in [Gradle](gradle-compiler-options.md#attributes-specific-to-jvm) or [Maven](maven.md#attributes-specific-to-jvm).
67
67
68
-
## Annotate records in Kotlin
69
-
70
-
To make an [annotation](annotations.md) available on both Kotlin properties **and** Java record components, add the
71
-
following meta-annotations to your annotation declaration:
72
-
73
-
* For Kotlin: [`@Target`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.annotation/-target/index.html)
74
-
* For Java record components: [`@java.lang.annotation.Target`](https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Target.html)
The `exampleClass` annotation can now be applied to Kotlin classes and properties, as well as Java classes and record
85
-
components.
86
-
87
-
### Propagate annotations of a record component
68
+
## Annotate record components in Kotlin
88
69
89
70
<primary-labelref="experimental-general"/>
90
71
91
-
<!-- Add warning about `@all` needing opt-in -->
92
-
93
-
In Java, annotations on a record component are automatically propagated to the backing field, getter, and constructor parameter.
72
+
In Java, [annotations](annotations.md) on a record component are automatically propagated to the backing field, getter, setter, and constructor parameter.
94
73
You can replicate this behavior in Kotlin by using the [`all`](annotations.md) use-site target.
95
74
96
75
> To use the `all` use-site target, you must opt in. Either use the `-Xannotation-target-all` compiler option or add
97
76
> the following to your `build.gradle.kts` file:
98
-
>
77
+
>
99
78
> ```kotlin
100
79
> kotlin {
101
80
> compilerOptions {
102
81
> freeCompilerArgs.add("-Xannotation-target-all")
103
82
> }
104
83
> }
105
84
> ```
106
-
>
85
+
>
107
86
{style="warning"}
108
87
109
88
For example:
@@ -115,9 +94,27 @@ data class Person(val name: String, @all:Positive val age: Int)
115
94
116
95
When you use `@JvmRecord` with `@all:`, Kotlin:
117
96
118
-
* Propagates the annotation to the property, backing field, constructor parameter, and getter.
97
+
* Propagates the annotation to the property, backing field, constructor parameter, getter and setter.
119
98
* Applies the annotation to the record component as well, if the annotation supports Java's `RECORD_COMPONENT`.
120
99
100
+
## Make annotations work with record components
101
+
102
+
To make an [annotation](annotations.md) available on both Kotlin properties **and** Java record components, add the
103
+
following meta-annotations to your annotation declaration:
104
+
105
+
* For Kotlin: [`@Target`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.annotation/-target/index.html)
106
+
* For Java record components: [`@java.lang.annotation.Target`](https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Target.html)
You can now apply `@ExampleClass` to Kotlin classes and properties, as well as Java classes and record components.
117
+
121
118
## Further discussion
122
119
123
120
See this [language proposal for JVM records](https://github.com/Kotlin/KEEP/blob/master/proposals/jvm-records.md) for further technical details and discussion.
0 commit comments