google / jsinterop-generator

Generates Java annotated with JsInterop from JavaScript extern sources
Apache License 2.0
75 stars 24 forks source link

Generic closure `@interface` types emit create() factory methods with raw return type #50

Open niloc132 opened 2 years ago

niloc132 commented 2 years ago

A specific example of this is elemental2.dom.CustomEventInit, which at least as of elemental2 1.1.0 is generic on T, but the factory method is raw:

@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public interface CustomEventInit<T> extends EventInit {
  @JsOverlay
  static CustomEventInit create() {
    return Js.uncheckedCast(JsPropertyMap.of());
  }

The bug appears to stem from jsinterop.generator.visitor.DictionaryTypeVisitor#addFactoryMethod, which creates a wrapper JavaTypeReference around the provided Type. Instead, it should probably be a ParametrizedTypeReference in this case. Perhaps the addFactoryMethod can test if type has type params and if so create a ParameterizedTypeReference instance instead?

I didn't see a utility to generically create types like this, so perhaps this is the only use case for this, but maybe there are other synthetic methods that are created only to let Java do things that Js can natively do.

A few other examples in elemental2 - while making this list wI was looking for other places other than a create() method where this was a problem, but didn't spot any so far:

If the approach sounds reasonable, I can put together a sample patch to try fixing this?

gkdn commented 2 years ago

@jDramaix