Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax JsInteorp restriction checking so one can perform feature check #9327

Closed
gkdn opened this issue May 4, 2016 · 8 comments
Closed

Relax JsInteorp restriction checking so one can perform feature check #9327

gkdn opened this issue May 4, 2016 · 8 comments
Assignees
Milestone

Comments

@gkdn
Copy link
Contributor

gkdn commented May 4, 2016

Relax JsInteorp restriction checking so one can perform feature check; like:

class Performance { 
  @JsMethod(namespace="performance")
  public static native double now();
  public static boolean hasNow() { return getNow() != null; }
  @JsProperty(namespace="performance")
  private static native Object getNow();
}

Above snippet fails due to name restriction checking.

Current workaround:

class Performance { 
  @JsMethod(namespace="performance")
  public static native double now();
  public static boolean hasNow() { return Helper.getNow() != null; }

  private static class Helper {
    @JsProperty(namespace="performance")
    public static native Object getNow();
  }
}
@gkdn gkdn added this to the 2.8 milestone May 4, 2016
@ibaca
Copy link
Contributor

ibaca commented May 5, 2016

Null primitives is quite annoying I hope GWT team will find good solution (manolo already makes a comment in the NextGen doc, and you said that Double and now Boolean can be used) but...
For example (easy to say, sorry... just ignore if makes no sense 😉):
@JsProperty(orElse="0") double now(); where orElse is a literal string. Or at least some primitive flag which if is true, all call will be handled as similar as possible as java primitives so now() will be always translated as (now||0), booleans now||false, etc.

Other more weird idea:

// field is substitude by the field name, this is useful to save the string as a constant
@JsProperty(getter=“$field++”) int increment();
// using a constant
@JsProperty(getter=INCREMENT) int increment();
// even more weird example (not sure if this makes sense)
@JsProperty(
  getter=“var l=@java.util.ArrayList::new()();l.@java.util.ArrayList::array=$field;return l;”, 
  setter=“$field = $set.@java.util.ArrayList::array”
) ArrayList<String> tokens();
// using a constant
@JsProperty(getter=ARRAY_LIST_GET, setter=ARRAY_LIST_SET) ArrayList<String> tokens();
// and just because is so easy to say thing a do not implement them...
@JsProperty(getter=ARRAY_LIST_GET, setter=ARRAY_LIST_SET)
@Target(FIELD) @Retention(RUNTIME) @JsDef
public @interface JsList {}
// so can be used as javax.validation annotation composition, just to clean up the code
@JsList ArrayList<String> tokens();

@gkdn
Copy link
Contributor Author

gkdn commented May 5, 2016

In general JsInterop v1 tries to be close to original JavaScript and avoids features that modifies the underlying abstraction heavily. The future plan to do abstraction modifications is via @JsConvert which will basically let you run the object through a function supplied by you.

@ibaca
Copy link
Contributor

ibaca commented May 5, 2016

Mmm... but... a JsInterop which returns a null primitive java value might be discutible as something that will be solved in v2. At least some module property which allow you to add the minimal safety to use primitive which behaves like java primitives, so primitives getter will be written as (val || def) where def is the default java value for the java primitive. I'll try to stop commenting, and I will try to try it myself 😉, thanks anyways.

@rluble
Copy link
Contributor

rluble commented May 5, 2016

@ignacio, I think you misread the point. This is not about primitive types.
This is using JsInterop in an unusual way using a JsFunction type property
to check for the existence of a instance method in a native type.

On Wed, May 4, 2016 at 11:48 PM, Ignacio Baca Moreno-Torres <
notifications@github.com> wrote:

Null primitives is quite annoying I hope GWT team will find good solution
(manolo already makes a comment in the NextGen doc, and you said that
Double and now Boolean can be used) but...
For example (easy to say, sorry... just ignore if makes no sense 😉):
@JsProperty(orElse="0") double now(); where orElse is a literal string.
Or at least some primitive flag which if is true, all call will be handled
as similar as possible as java primitives so now() will be always
translated as (now||0), booleans now||false, etc.

Other more weird idea:

// field is substitude by the field name, this is useful to save the string as a constant somewhere else
@JsProperty(getter=“$field++”) int increment();// using a constant
@JsProperty(getter=INCREMENT) int increment();// even more weird example (not sure if this makes sense)
@JsProperty(
getter=“var l=@java.util.ArrayList::new()();l.@java.util.ArrayList::array=$field;return l;”,
setter=“$field = $set.@java.util.ArrayList::array”
) ArrayList tokens();// using a constant
@JsProperty(getter=ARRAY_LIST_GET, setter=ARRAY_LIST_SET) ArrayList tokens();// and just because is so easy to say thing a do not implement them...
@JsProperty(getter=ARRAY_LIST_GET, setter=ARRAY_LIST_SET)
@target(FIELD) @retention(RUNTIME) @JsDefpublic @interface JsList {}// so can be used as javax.validation annotation composition, just to clean it up the code
@jslist ArrayList tokens();


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#9327 (comment)

@gkdn
Copy link
Contributor Author

gkdn commented May 5, 2016

To answer your last comment;
If you think it could be null then you shouldn't be using Java primitive. The types that corresponds to JavaScript primitives are boxed Java types not Java primitives. You can only use a Java primitives for convenience if you don't have any concerns of nullability.

@ibaca
Copy link
Contributor

ibaca commented May 5, 2016

@rluble yep, I know this is not about primitives, probably not the best place to solve my doubts. But, if primitives can be safety defaulted to a java-primitive-default-value, this use case (double now()) will returns 0, so the second method to check if is null won't be necessary (maybe this isn't good). So I took the opportunity to comment my null-primitive concerns.

Looks like @gkdn are seeing this more in the side of JS, and I'm more in the side of Java. But you said; "you can only use java primitives for convenience if you don't have any concerns of nullability" so.. why are you using it in this now() method? 😞

My actual use case is for shared DTOs.

@JsType(isNative=true,namespace=GLOBAL,name="Object") class MyDto {
  public int foo;
  public int bar; 
}

And my concerns are that the client instance will have null foo/bar but server instances will not (need to be native and "Object" because this can come from a JSON). Just curious in the actual motivation on admit null-primitives. But as @gkdn said, v1 tries to be close to the original JavaScript. So I'm ok. Thanks!

@gkdn
Copy link
Contributor Author

gkdn commented May 5, 2016

window.performance.now can never return null. window.performance.now not being existent is different that now returning null when you 'call' it.

@rluble
Copy link
Contributor

rluble commented May 31, 2016

@rluble rluble closed this as completed Jun 14, 2016
hpehl pushed a commit to hpehl/gwt that referenced this issue Jun 16, 2016
A native property is allowed to have the same name as a native method.
A property is considered native if both setter and getter are native
if they exist.

Bug: gwtproject#9327
Bug-Link: http://github.com/gwtproject/gwt/issues/9327
Change-Id: I047fae52e344453eb1222c5a8e594f5f4cf53bff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants