Skip to content

Commit 8fd99b0

Browse files
gkdncopybara-github
authored andcommitted
[JS] Implement JsInterop support for Java Records.
This change adds support for using JsInterop annotations on Java Records and their components in J2CL. - Record component fields are implicitly treated as JsProperties. - Record component accessors inherit their JsInterop configuration from the components and are restricted from having explicit annotations to avoid conflicts. - Native records are disallowed as they don't have a meaningful representation in this context. - `@JsOptional` is disallowed on record components. Maybe will be later allowed on explicit canonical constructors. More details are covered in docs/design/records-jsinterop.md In particular this CL only includes changes and tests for Java frontend. Kotlin-Java interop on Record with jsinterop is currently blocked due the problem where private fields are hidden. I will include that with a patch to kotlinc in a later iteration. PiperOrigin-RevId: 910517409
1 parent d2d3d14 commit 8fd99b0

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

java/jsinterop/annotations/JsMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* JsMethod marks a method that will be directly translated into a JavaScript method preserving its
26-
* name.
26+
* name. They are not allowed on record component accessors.
2727
*
2828
* <p>Note: In JavaScript, instance members are defined on the prototype and class members are
2929
* defined on the constructor function of the type.

java/jsinterop/annotations/JsType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@
4040
* <p>Name collisions must be avoided by providing custom names (e.g. {@link JsProperty#name}) or by
4141
* ignoring members using {@link JsIgnore}.
4242
*
43+
* <p>When applied to a Java record, all public record components are implicitly exposed as
44+
* read-only properties. The canonical constructor is also implicitly exposed as a {@link
45+
* JsConstructor}. Non-canonical constructors cannot be exposed so they should be marked with {@link
46+
* JsIgnore}.
47+
*
4348
* <p>If the JsType is marked as "native" via {@link #isNative}, then the type is considered a stub
4449
* for an existing class that is available in native JavaScript. Unlike non-native JsTypes, all
4550
* members are considered {@link JsProperty}/{@link JsMethod}/{@link JsConstructor} unless they are
46-
* explicitly marked with {@link JsOverlay}.
51+
* explicitly marked with {@link JsOverlay}. Records cannot be marked as native.
4752
*
4853
* <p>For native interfaces with no particular JavaScript type associated with them (e.g. structural
4954
* types) it is recommended to use {@code namespace = JsPackage.GLOBAL} and {@code name = '?'}.

0 commit comments

Comments
 (0)