cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 160 forks source link

Invalid equals method #573

Closed mueller-jens closed 4 years ago

mueller-jens commented 4 years ago

Hi, I try to convert a pojo wich uses the @Data annotation.

package x;
import java.util.List;

import lombok.Data;

@Data
public class Pojo {

    private String property;
    private int intPproperty;
    private List<Subelement> listPproperty;

}

When i try to convert this to javascrpt using the jsweet-maven-plugin the folloving typescript code will be generated:

/* Generated from Java with JSweet 3.0.0-RC1 - http://www.jsweet.org */
var x;
(function (x) {
    class Pojo {
        constructor() {
            if (this.property === undefined)
                this.property = null;
            if (this.intPproperty === undefined)
                this.intPproperty = 0;
            if (this.listPproperty === undefined)
                this.listPproperty = null;
        }
        getProperty() {
            return this.property;
        }
        getIntPproperty() {
            return this.intPproperty;
        }
        getListPproperty() {
            return this.listPproperty;
        }
        setProperty(property) {
            this.property = property;
        }
        setIntPproperty(intPproperty) {
            this.intPproperty = intPproperty;
        }
        setListPproperty(listPproperty) {
            this.listPproperty = listPproperty;
        }
        /**
         *
         * @param {*} o
         * @return {boolean}
         */
        equals(o) {
            ifo === this;
            return true;
            if (!(o != null && o instanceof x.Pojo))
                return false;
            let other = o;
            if (!other.canEqual(this))
                return false;
            let this$property = this.getProperty();
            let other$property = other.getProperty();
            ifthis$property == null ? other$property != null : !((o1, o2) => { if (o1 && o1.equals) {
                return o1.equals(o2);
            }
            else {
                return o1 === o2;
            } })(this$property, other$property);
            return false;
            ifthis.getIntPproperty() !== other.getIntPproperty();
            return false;
            let this$listPproperty = this.getListPproperty();
            let other$listPproperty = other.getListPproperty();
            ifthis$listPproperty == null ? other$listPproperty != null : !((o1, o2) => { if (o1 && o1.equals) {
                return o1.equals(o2);
            }
            else {
                return o1 === o2;
            } })(this$listPproperty, other$listPproperty);
            return false;
            return true;
        }
        canEqual(other) {
            return (other != null && other instanceof x.Pojo);
        }
        /**
         *
         * @return {number}
         */
        hashCode() {
            let PRIME = 59;
            let result = 1;
            let $property = this.getProperty();
            result = result * PRIME + ($property == null ? 43 : ((o) => { if (o.hashCode) {
                return o.hashCode();
            }
            else {
                return o.toString().split('').reduce((prevHash, currVal) => (((prevHash << 5) - prevHash) + currVal.charCodeAt(0)) | 0, 0);
            } })($property));
            result = result * PRIME + this.getIntPproperty();
            let $listPproperty = this.getListPproperty();
            result = result * PRIME + ($listPproperty == null ? 43 : ((o) => { if (o.hashCode) {
                return o.hashCode();
            }
            else {
                return o.toString().split('').reduce((prevHash, currVal) => (((prevHash << 5) - prevHash) + currVal.charCodeAt(0)) | 0, 0);
            } })($listPproperty));
            return result;
        }
        /**
         *
         * @return {string}
         */
        toString() {
            return "Pojo(property=" + this.getProperty() + ", intPproperty=" + this.getIntPproperty() + ", listPproperty=" + (a => a ? '[' + a.join(', ') + ']' : 'null')(this.getListPproperty()) + ")";
        }
    }
    x.Pojo = Pojo;
    Pojo["__class"] = "x.Pojo";
})(x || (x = {}));

As you can see there are some blanks missing in the typescript code for example in this line:

ifthis.getIntPproperty() !== other.getIntPproperty();

which leads to an exeption in the next step the js generation:

2020-04-07 20:02:45.045 ERROR output:55 - ';' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
2020-04-07 20:02:46.046 ERROR output:55 - '(' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
2020-04-07 20:02:46.046 ERROR output:55 - ')' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
2020-04-07 20:02:46.046 ERROR output:55 - '(' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
2020-04-07 20:02:46.046 ERROR output:55 - ')' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
2020-04-07 20:02:46.046 ERROR output:55 - ';' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
2020-04-07 20:02:46.046 ERROR output:55 - ';' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
2020-04-07 20:02:46.046 ERROR output:55 - ';' expected at D:\tools\eclipse-workspace\jsweet-test\src\main\java\x\Pojo.java(12)
[ERROR] transpilation failed
org.apache.maven.plugin.MojoFailureException: transpilation failed with 8 error(s) and 0 warning(s)
    at org.jsweet.AbstractJSweetMojo.transpile(AbstractJSweetMojo.java:618)
    at org.jsweet.JSweetMojo.execute(JSweetMojo.java:43)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)

Is the use of @Data not supported?

mueller-jens commented 4 years ago

Looks like there is a bug in line 4956 of class Java2TypeScriptTranslator

print("if").print(ifStatement.getCondition()).print(" ");

should be

print("if (").print(ifStatement.getCondition()).print(") ");