eclipse-vertx / vertx-codegen

Vert.x code generator for asynchronous polyglot APIs
Apache License 2.0
105 stars 92 forks source link

Not compatible with lombok #250

Open hauter opened 5 years ago

hauter commented 5 years ago

This is a code example in the book (guide-for-java-devs.pdf), and I made a piece of changes.

  1. Wiki.java corresponds with the database table:

    @AllArgsConstructor
    public class Wiki {
    public Long id;
    public String name;
    }
  2. WikiDbService.java

@ProxyGen
public interface WikiDbService {

    @Fluent
    WikiDbService fetchAll(Handler<AsyncResult<JsonArray>> resultHandler);

    @GenIgnore
    static WikiDbService create(JDBCClient dbClient, Handler<AsyncResult<WikiDbService>> readyHandler) {
        return new WikiDbServiceImpl(dbClient, readyHandler);
    }

    @GenIgnore
    static WikiDbService createProxy(Vertx vertx, String address) {
        return new WikiDbServiceVertxEBProxy(vertx, address);
    }
}
  1. WikiDbServiceImpl.java
    @Override
    public WikiDbService fetchAll(Handler<AsyncResult<JsonArray>> resultHandler) {
        dbClient.query(FETCH_ALL_SQL, queryAr -> {
            if (queryAr.failed()) {
                resultHandler.handle(Future.failedFuture(queryAr.cause()));
                return;
            }

            ResultSet resultSet = queryAr.result();
            List<Wiki> wikiList = resultSet.getResults().stream()
                .map(r -> new Wiki(r.getLong(0), r.getString(1)))
                .collect(Collectors.toList());

            resultHandler.handle(Future.succeededFuture(new JsonArray(new Gson().toJson(wikiList))));
        });

        return this;
    }

Finally, I got a compilation error:

image

it means that there no suitable constructor for Wiki class with 2 arguments ....

But If I generate the constructor:

public class Wiki {
    public Long id;
    public String name;

    public Wiki(Long id, String name) {
        this.id = id;
        this.name = name;
    }
}

It works.

So I think it's not compatible with lombok ...

adityaLath18 commented 3 years ago

Did you get any solution for using codegen and lombok both?

vietj commented 3 years ago

does lombok works with code generators ?

hbayrousson commented 3 years ago

Hi,

With the step-3 in the book (guide-for-java-devs.pdf), and your changes, and some changes, I have an compilation success and the code is generated correctly.

My changes are :

Moreover, if I add in the wiki class :


package io.vertx.guides.wiki.database;
import io.vertx.codegen.annotations.DataObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@Setter
@AllArgsConstructor
@DataObject(generateConverter = true)
  public class Wiki {
    public Long id;
    public String name;
  }

After compiling, I have this file generated :

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package io.vertx.guides.wiki.database;

import io.vertx.core.json.JsonObject;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class WikiConverter {
    public WikiConverter() {
    }

    public static void fromJson(Iterable<Entry<String, Object>> json, Wiki obj) {
        Iterator var2 = json.iterator();

        while(var2.hasNext()) {
            Entry<String, Object> member = (Entry)var2.next();
            String var4 = (String)member.getKey();
            byte var5 = -1;
            switch(var4.hashCode()) {
            case 3355:
                if (var4.equals("id")) {
                    var5 = 0;
                }
                break;
            case 3373707:
                if (var4.equals("name")) {
                    var5 = 1;
                }
            }

            switch(var5) {
            case 0:
                if (member.getValue() instanceof Number) {
                    obj.setId(((Number)member.getValue()).longValue());
                }
                break;
            case 1:
                if (member.getValue() instanceof String) {
                    obj.setName((String)member.getValue());
                }
            }
        }

    }

    public static void toJson(Wiki obj, JsonObject json) {
        toJson(obj, json.getMap());
    }

    public static void toJson(Wiki obj, Map<String, Object> json) {
        if (obj.getId() != null) {
            json.put("id", obj.getId());
        }

        if (obj.getName() != null) {
            json.put("name", obj.getName());
        }

    }
}
jbaris commented 3 years ago

Thank you @hbayrousson. Adding: <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor> did the trick.