Git Product home page Git Product logo

jsinterop-annotations's Introduction

JsInterop Annotations · Build Status

JsInterop Annotations contains a set of java annotations that drive the javascript code generation of J2CL transpiler or GWT

Bazel dependency

If your project uses Bazel, add this repository as an external dependency in your WORKSPACE file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
_JSINTEROP_ANNOTATIONS_VERSION = "2.0.2"
http_archive(
    name = "com_google_jsinterop_annotations",
    strip_prefix = "jsinterop-annotations-%s" % _JSINTEROP_ANNOTATIONS_VERSION,
    url = "https://github.com/google/jsinterop-annotations/archive/%s.zip" % _JSINTEROP_ANNOTATIONS_VERSION,
)

Then add @com_google_jsinterop_annotations//:jsinterop-annotations-j2cl to your j2cl_library deps.

Maven dependency

If your project uses Maven, add the following maven dependency in your pom.xml:

<dependency>
    <groupId>com.google.jsinterop</groupId>
    <artifactId>jsinterop-annotations</artifactId>
    <version>2.0.2</version>
</dependency>

Download the jar file

You can also download manually the jar file.

Build GWT compatible maven jar files

If you want to build the last version on your own, follow the instructions below:

    $ npm install -g @bazel/bazelisk
    $ alias bazel=bazelisk
  • Clone this git repository:
    $ git clone https://github.com/google/jsinterop-annotations.git
  • Run the release script:
        $ cd jsinterop-annotations
        $ ./maven/release_jsinterop_annotations.sh --version local --no-deploy

The script will output the directory containing the generated jar files that can be used with maven.

Contributing

Please refer to the contributing document.

Licensing

Please refer to the license file.

Disclaimer

This is not an official Google product.

jsinterop-annotations's People

Contributors

gkdn avatar jdramaix avatar jparachoniak avatar kevinoconnor7 avatar martinkretzschmar avatar mollyibot avatar realityforge avatar wcn3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsinterop-annotations's Issues

Proposed change in behavior for `@JsProperty`/`@JsType` with non-native Record types

https://openjdk.java.net/jeps/395 added Records to Java 16 as a finalized feature. In the course of implementing this for GWT 2, we've observed that it might make sense to slightly modify @JsProperty for non-native types in a way that doesn't automatically produce errors. It could also make sense to change how @JsType behaves on non-native records to automatically expose record components as properties rather than methods.

From the JEP:

A record class, and the components in its header, may be decorated with annotations. Any annotations on the record components are propagated to the automatically derived fields, methods, and constructor parameters, according to the set of applicable targets for the annotation. Type annotations on the types of record components are also propagated to the corresponding type uses in the automatically derived members.

This means that if @JsProperty is annotated on a record component, it will propagate to both the accessor method and the private final field. That is,

record Point(@JsProperty int x, @JsProperty int y) {}

would effectively become

record Point(@JsProperty int x, @JsProperty int y) {
  @JsProperty
  private final int x;
  @JsProperty
  private final int y;
  @JsProperty
  public int x() {
    return this.x;
  }
  @JsProperty
  public int y() {
    return this.y;
  }
}

This seems problematic for two reasons:

  • First, while jsinterop annotations are allowed on private members, they are not allowed on two members with the same name - x and x() would collide.
  • Second, x() and y() do not follow the expected name pattern.

Possible workarounds:

  • Changing the record component annotation from @JsProperty int x to @JsProperty("x") int x would technically resolve the naming issue, but with unnecessary verbosity, and still not correct the first problem
  • Even more verbosity could let a developer skip the annotation on the record component and instead add it to the accessor directly, writing:
record Point(int x, int y) {
  @JsProperty
  public int x() {
    return this.x;
  }
  @JsProperty
  public int y() {
    return this.y;
  }
}

This is usable, but arguably what the user clearly meant to begin with.

I propose instead that

  • @JsProperty on record component accessors not require that the accessor be named as a Bean getter, but reflecting the immutable nature of the record, only be named for the component.
  • @JsProperty on record instance fields are ignored if they are also present on the matching accessor method. This would allow a collision in naming, where the accessor should always "win" when exposing to JS.

Next, decorating a record with @JsType. This is unlikely to reflect the developer's intent, as by default the constructor and accessor methods would be treated as if decorated with their default jsinterop annotation. Instead, I suggest that the methods should be exported as properties. This makes the above example even simpler, offering a simple immutable record/struct to JS:

@JsType
public record Point(int x, int y) {}

As the constructor and accessors are implicit (but can be explicit, and in the case of the constructor, compact), if declared they could have their own jsinterop annotations present (@JsIgnore, for example), and if other methods are added, they would follow the standard rules that JsType follows - if public, they are annotated as expected. As records cannot have non-static fields added explicitly, this effectively means only constructors and methods will be able to be exposed in this way.

One observation: the compact constructor, in conjunction with these rules could simplify records with many components. Instead of the initial example where each component was annotated with @JsProperty, we could instead see

@JsType
public record Book(String title, int pageCount, List<String> authors) {
  @JsIgnore
  Book {
  }
}

It may also make sense to offer new behavior for native records, so a native Java record means something - perhaps a closure struct could make sense here? This would mean that invoking a native Java record's constructor would serve to create an object with the expected components, simple syntactic sugar to replace the interfaces with create() factory method that jsinterop-generator currently produces. This is a little more opinionated though (dealing with immutability, etc), so could be taken up separately.

Single impl interfaces?

In GWT2 with JavaScriptObject it was possible to have exactly one subclass implement an interface - the compiler would detect this and where necessary devirtualize the calls and dispatch correctly to either plain java implementations or the single native implementation.

With JSO, there were two ways to implement these:

  1. provide a simple native java method which would use JSNI to invoke the true method
  2. provide an overlay method which would then call the appropriate native methods/properties

From the original design doc option 1 is still apparently possible in jsinterop-annotations assuming the interface itself can be modified to be marked as native ("A native @JsType can only extend/implement native @JsTypes."), but option 2 is not possible at all ("A native @JsType class can only have ... Final non-native JsOverlay methods that do not override any other methods").

In the context of GWT2 these limitations may not necessarily make sense, but with j2cl they clearly seem to, since the interface and implementation might be compiled separately, so generating the dispatch mechanism when the interface is transpiled would need to depend on knowing about the implementation.

Are there any suggested patterns to solve these sorts of issues? Ideas I've entertained and their downsides:

  • Rewrite the interface to be marked as @JsType(isNative=true) and to only have declared members which 100% match the eventual JS implementation - this is not always possible when the interface comes from some upstream source (org.w3c.dom, org.json are some such examples where overlays can bridge the gap and let code share an interface).
  • "Monkeypatching" the underlying JS object solves the above by avoiding the need for overlays, but clearly adds its own level of ick
  • Provide a java wrapper per native instance - clearly this adds a bit of runtime overhead, depending on how the interfaces are declared

No such target '@com_google_jsinterop_annotations//:com_google_jsinterop_annotations'

Sorry, again I might be confused or be looking at the wrong project here.

bazel run ..._dev_server in a j2cl project fails with the error:

ERROR: /home/.../5d3af3ef2f570227985f1da47f50df97/external/com_google_common_html_types/BUILD:7:12: no such target '@com_google_jsinterop_annotations//:com_google_jsinterop_annotations': target 'com_google_jsinterop_annotations' not declared in package '' defined by /home/.../5d3af3ef2f570227985f1da47f50df97/external/com_google_jsinterop_annotations/BUILD and referenced by '@com_google_common_html_types//:com_google_common_html_types'

It looks like by convention the com_google_jsinterop_annotations target should exist but in the BUILD here there's only jsinterop-annotations.

AFAICT safe-html-types depends on jsinterop-annotations here https://github.com/google/safe-html-types/blob/8507735457ea41a37dfa027fb176d49d5783c4ba/types/pom.xml#L38 . The BUILD files seem to be generated automatically from rules_jvm_external however I'm not sure how it comes up with the name com_google_jsinterop_annotations - is this taken from the workspace name?

1.0.2 is not compatible with GWT 2.10.0

According to the documentation we should use version 1.0.2 in combination with GWT 2.10.0, but this gave me a lot of errors about @JsNonNull not being found.

I switched to 2.0.1 and now my project is compiling.

Is the documentation outdated or is GWT 2.10.0 depending on an annotation that it shouldn't ?

Visibility error when added as dep per readme

I'm new to j2cl and Bazel FWIW.

I was missing the annotations dep and got this error:

error: [strict] Using type jsinterop.annotations.JsType from an indirect dependency (TOOL_INFO: "@com_google_jsinterop_annotations-j2cl//java/jsinterop/annotations"). See command below **
import jsinterop.annotations.JsType;
                            ^
 ** Please add the following dependencies: 
  @com_google_jsinterop_annotations-j2cl//java/jsinterop/annotations to //...
 ** You can use the following buildozer command: 
buildozer 'add deps @com_google_jsinterop_annotations-j2cl//java/jsinterop/annotations' //...

Adding @com_google_jsinterop_annotations-j2cl//java/jsinterop/annotations didn't work:

in deps attribute of j2cl_library rule //...: '@com_google_jsinterop_annotations-j2cl//java/jsinterop/annotations:annotations' does not have mandatory providers: 'closure_js_library'. Since this rule was created by the macro 'j2cl_library', the error might have been caused by the macro implementation

Searching brought me here -- I think this is the same library. The readme says to add @com_google_jsinterop_annotations//:jsinterop-annotations-j2cl to deps, but this produces the error:

in j2cl_library rule //com/zarbosoft/merman:editor: alias '@com_google_jsinterop_annotations//:jsinterop-annotations-j2cl' referring to target '@com_google_jsinterop_annotations//java/jsinterop/annotations:annotations-j2cl' is not visible from target '//...'. Check the visibility declaration of the former target if you think the dependency is legitimate

Eventually I found an example of the annotations in the guava j2cl sample, which uses @com_google_j2cl//:jsinterop-annotations-j2cl and this worked.

I'm not sure if this is the recommended usage though.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.