Skip to content

Commit

Permalink
Emit create() factory methods with generic return type
Browse files Browse the repository at this point in the history
Fixes #50

PiperOrigin-RevId: 685752611
  • Loading branch information
mollyibot authored and copybara-github committed Oct 14, 2024
1 parent 4fa3bff commit 04c009f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion java/jsinterop/generator/visitor/DictionaryTypeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
import static jsinterop.generator.model.PredefinedTypeReference.JS_PROPERTY_MAP;
import static jsinterop.generator.model.PredefinedTypeReference.OBJECT;

import com.google.common.collect.ImmutableList;
import jsinterop.generator.model.AbstractVisitor;
import jsinterop.generator.model.Annotation;
import jsinterop.generator.model.JavaTypeReference;
import jsinterop.generator.model.Method;
import jsinterop.generator.model.MethodInvocation;
import jsinterop.generator.model.ModelVisitor;
import jsinterop.generator.model.ParametrizedTypeReference;
import jsinterop.generator.model.Program;
import jsinterop.generator.model.ReturnStatement;
import jsinterop.generator.model.Type;
import jsinterop.generator.model.TypeQualifier;
import jsinterop.generator.model.TypeReference;

/**
* A dictionary type is an interface that contains only JsProperty methods.
Expand All @@ -57,8 +60,13 @@ private static void addFactoryMethod(Type type) {
Method factory = new Method();
factory.setStatic(true);
factory.setName("create");
factory.setReturnType(new JavaTypeReference(type));
factory.addAnnotation(Annotation.builder().type(JS_OVERLAY).build());
TypeReference returnType = new JavaTypeReference(type);
if (!type.getTypeParameters().isEmpty()) {
factory.setTypeParameters(ImmutableList.copyOf(type.getTypeParameters()));
returnType = new ParametrizedTypeReference(returnType, type.getTypeParameters());
}
factory.setReturnType(returnType);
// body statement: return Js.uncheckedCast(JsPropertyMap.of());
factory.setBody(
new ReturnStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import jsinterop.base.JsPropertyMap;
@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public interface RecordWithGeneric<T> {
@JsOverlay
static RecordWithGeneric create() {
static <T> RecordWithGeneric<T> create() {
return Js.uncheckedCast(JsPropertyMap.of());
}

Expand Down

0 comments on commit 04c009f

Please sign in to comment.