Skip to content

Commit

Permalink
Support instance member overriding a static member with union types
Browse files Browse the repository at this point in the history
Fixes google#46
  • Loading branch information
niloc132 committed May 27, 2020
1 parent 3fb3be7 commit 65894de
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 7 deletions.
15 changes: 8 additions & 7 deletions java/jsinterop/generator/visitor/MembersClassCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

package jsinterop.generator.visitor;

import static jsinterop.generator.model.AnnotationType.JS_METHOD;
import static jsinterop.generator.model.AnnotationType.JS_PROPERTY;
import static jsinterop.generator.model.AnnotationType.*;
import static jsinterop.generator.model.EntityKind.METHOD;

import java.util.ArrayList;
Expand Down Expand Up @@ -163,12 +162,14 @@ private <T extends Entity> void renameSameEntities(
String name = entity.getName();
entity.setName(name + "_STATIC");

// TODO(dramaix): add a javadoc above the field explaining why it was renamed
AnnotationType annotationType =
entity.getKind() == METHOD ? JS_METHOD : JS_PROPERTY;
if (!entity.hasAnnotation(JS_OVERLAY)) {
// TODO(dramaix): add a javadoc above the field explaining why it was renamed
AnnotationType annotationType =
entity.getKind() == METHOD ? JS_METHOD : JS_PROPERTY;

ModelHelper.addAnnotationNameAttributeIfNotEmpty(
entity, name, annotationType, true);
ModelHelper.addAnnotationNameAttributeIfNotEmpty(
entity, name, annotationType, true);
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions java/jsinterop/generator/visitor/UnionTypeHelperTypeCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ public void applyTo(Program program) {

@Override
public boolean shouldProcessField(Field field) {
if (field.isStatic()) {
currentNameStack.push("Static");
}
currentNameStack.push(toCamelUpperCase(field.getName()));
return true;
}

@Override
public Field rewriteField(Field field) {
if (field.isStatic()) {
currentNameStack.pop();
}
currentNameStack.pop();
return field;
}
Expand All @@ -95,6 +101,9 @@ public boolean shouldProcessMethod(Method method) {
// JsFunction type only have one method named onInvoke, don't use the method name
// because it doesn't give us any much more information.
if (!isCurrentTypeJsFunction()) {
if (method.isStatic()) {
currentNameStack.push("Static");
}
currentNameStack.push(
method.getKind() == CONSTRUCTOR
? "Constructor"
Expand All @@ -106,6 +115,9 @@ public boolean shouldProcessMethod(Method method) {
@Override
public Method rewriteMethod(Method method) {
if (!isCurrentTypeJsFunction()) {
if (method.isStatic()) {
currentNameStack.pop();
}
currentNameStack.pop();
}

Expand Down
22 changes: 22 additions & 0 deletions javatests/jsinterop/generator/externs/overloads/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Description:
# Tests conversion of overridden methods and their args
#

load(
"//javatests/jsinterop/generator:jsinterop_generator_test.bzl",
"jsinterop_generator_test",
)

package(
default_visibility = ["//:__subpackages__"],
# Apache2
licenses = ["notice"],
)

jsinterop_generator_test(
name = "overloads",
srcs = ["overloads.js"],
expected_output = [
"Foo.java.txt",
],
)
157 changes: 157 additions & 0 deletions javatests/jsinterop/generator/externs/overloads/Foo.java.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package jsinterop.generator.externs.overloads;

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsOverlay;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsProperty;
import jsinterop.annotations.JsType;
import jsinterop.base.Js;

@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class Foo {
@JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL)
public interface StaticUnionArgMethodArgUnionType {
@JsOverlay
static Foo.StaticUnionArgMethodArgUnionType of(Object o) {
return Js.cast(o);
}

@JsOverlay
default double asDouble() {
return Js.asDouble(this);
}

@JsOverlay
default String asString() {
return Js.asString(this);
}

@JsOverlay
default boolean isDouble() {
return (Object) this instanceof Double;
}

@JsOverlay
default boolean isString() {
return (Object) this instanceof String;
}
}

@JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL)
public interface StaticUnionPropertyUnionType {
@JsOverlay
static Foo.StaticUnionPropertyUnionType of(Object o) {
return Js.cast(o);
}

@JsOverlay
default double asDouble() {
return Js.asDouble(this);
}

@JsOverlay
default String asString() {
return Js.asString(this);
}

@JsOverlay
default boolean isDouble() {
return (Object) this instanceof Double;
}

@JsOverlay
default boolean isString() {
return (Object) this instanceof String;
}
}

@JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL)
public interface UnionArgMethodArgUnionType {
@JsOverlay
static Foo.UnionArgMethodArgUnionType of(Object o) {
return Js.cast(o);
}

@JsOverlay
default double asDouble() {
return Js.asDouble(this);
}

@JsOverlay
default String asString() {
return Js.asString(this);
}

@JsOverlay
default boolean isDouble() {
return (Object) this instanceof Double;
}

@JsOverlay
default boolean isString() {
return (Object) this instanceof String;
}
}

@JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL)
public interface UnionPropertyUnionType {
@JsOverlay
static Foo.UnionPropertyUnionType of(Object o) {
return Js.cast(o);
}

@JsOverlay
default double asDouble() {
return Js.asDouble(this);
}

@JsOverlay
default String asString() {
return Js.asString(this);
}

@JsOverlay
default boolean isDouble() {
return (Object) this instanceof Double;
}

@JsOverlay
default boolean isString() {
return (Object) this instanceof String;
}
}

@JsProperty(name = "unionProperty")
public static Foo.StaticUnionPropertyUnionType unionProperty_STATIC;

@JsMethod(name = "noArgMethod")
public static native Object noArgMethod_STATIC();

public static native Object unionArgMethod(Foo.StaticUnionArgMethodArgUnionType arg);

@JsOverlay
public static final Object unionArgMethod_STATIC(String arg) {
return unionArgMethod(Js.<Foo.StaticUnionArgMethodArgUnionType>uncheckedCast(arg));
}

@JsOverlay
public static final Object unionArgMethod_STATIC(double arg) {
return unionArgMethod(Js.<Foo.StaticUnionArgMethodArgUnionType>uncheckedCast(arg));
}

public Foo.UnionPropertyUnionType unionProperty;

public native Object noArgMethod();

@JsOverlay
public final Object unionArgMethod(String arg) {
return unionArgMethod(Js.<Foo.UnionArgMethodArgUnionType>uncheckedCast(arg));
}

public native Object unionArgMethod(Foo.UnionArgMethodArgUnionType arg);

@JsOverlay
public final Object unionArgMethod(double arg) {
return unionArgMethod(Js.<Foo.UnionArgMethodArgUnionType>uncheckedCast(arg));
}
}
31 changes: 31 additions & 0 deletions javatests/jsinterop/generator/externs/overloads/overloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

/**
* @fileoverview Test "overloaded" method conventions
* @externs
*/

/**
* @constructor
*/
function Foo() {}

Foo.noArgMethod = function() {};
Foo.prototype.noArgMethod = function() {};

/**
* @param {string|number} arg
*/
Foo.unionArgMethod = function(arg) {};
/**
* @param {string|number} arg
*/
Foo.prototype.unionArgMethod = function(arg) {};

/**
* @type {string|number}
*/
Foo.unionProperty;
/**
* @type {string|number}
*/
Foo.prototype.unionProperty;

0 comments on commit 65894de

Please sign in to comment.