ebean-orm-tools / finder-generator

Code generator for avaje-ebeanorm-typequery
Apache License 2.0
2 stars 3 forks source link

Kotlin imports #28

Closed Ryszard-Trojnacki closed 5 years ago

Ryszard-Trojnacki commented 5 years ago

I'm using io.ebean.tools:finder-generator:11.34.1 to generate QueryBeans, and evething works OK, but for one class generator started at some point to generate Kotlin imports. Generated class:

package pl.rtprog.bookme.database.tables.query;

import io.ebean.Database;
import io.ebean.typequery.PArray;
import io.ebean.typequery.PJson;
import io.ebean.typequery.PLocalDate;
import io.ebean.typequery.PLong;
import io.ebean.typequery.PString;
import io.ebean.typequery.TQRootBean;
import io.ebean.typequery.TypeQueryBean;
import kotlin.Int;
import kotlin.Long;
import pl.rtprog.bookme.database.tables.Apartment;
import pl.rtprog.bookme.database.tables.query.assoc.QAssocBaseFile;
import pl.rtprog.bookme.database.tables.query.assoc.QAssocProperty;
import pl.rtprog.bookme.database.tables.query.assoc.QAssocUser;

/**
 * Query bean for Apartment.
 * 
 * THIS IS A GENERATED OBJECT, DO NOT MODIFY THIS CLASS.
 */
@TypeQueryBean
public class QApartment extends TQRootBean<Apartment,QApartment> {

  private static final QApartment _alias = new QApartment(true);

  /**
   * Return the shared 'Alias' instance used to provide properties to 
   * <code>select()</code> and <code>fetch()</code> 
   */
  public static QApartment alias() {
    return _alias;
  }

  public PLong<QApartment> id;
  public PString<QApartment> name;
  public PJson<QApartment> names;
  public QAssocUser<QApartment> owner;
  public QAssocProperty<QApartment> property;
  public PJson<QApartment> descriptions;
  public PArray<QApartment,Integer> rooms;
  public PLocalDate<QApartment> dataDay;
  public PArray<QApartment,Long> prices;
  public QAssocBaseFile<QApartment> photos;

  /**
   * Construct with a given Database.
   */
  public QApartment(Database database) {
    super(Apartment.class, database);
  }

  /**
   * Construct using the default Database.
   */
  public QApartment() {
    super(Apartment.class);
  }

  /**
   * Construct for Alias.
   */
  private QApartment(boolean dummy) {
    super(dummy);
  }
}

Same for Assoc class for this entity:

package pl.rtprog.bookme.database.tables.query.assoc;

import io.ebean.typequery.PArray;
import io.ebean.typequery.PJson;
import io.ebean.typequery.PLocalDate;
import io.ebean.typequery.PLong;
import io.ebean.typequery.PString;
import io.ebean.typequery.TQAssocBean;
import io.ebean.typequery.TQProperty;
import io.ebean.typequery.TypeQueryBean;
import kotlin.Int;
import kotlin.Long;
import pl.rtprog.bookme.database.tables.Apartment;
import pl.rtprog.bookme.database.tables.query.QApartment;

/**
 * Association query bean for AssocApartment.
 * 
 * THIS IS A GENERATED OBJECT, DO NOT MODIFY THIS CLASS.
 */
@TypeQueryBean
public class QAssocApartment<R> extends TQAssocBean<Apartment,R> {

  public PLong<R> id;
  public PString<R> name;
  public PJson<R> names;
  public QAssocUser<R> owner;
  public QAssocProperty<R> property;
  public PJson<R> descriptions;
  public PArray<R,Integer> rooms;
  public PLocalDate<R> dataDay;
  public PArray<R,Long> prices;
  public QAssocBaseFile<R> photos;

  /**
   * Eagerly fetch this association loading the specified properties.
   */
  @SafeVarargs
  public final R fetch(TQProperty<QApartment>... properties) {
    return fetchProperties(properties);
  }

  public QAssocApartment(String name, R root) {
    super(name, root);
  }
}

Generated code is Java, but imports for Int and Long is from Kotlin. In my project I have other 12 entity classes, but only for this one I have Kotlin imports:

2019-08-14 12:28:16,283 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/User
2019-08-14 12:28:16,292 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QUser.java
2019-08-14 12:28:16,293 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocUser.java
2019-08-14 12:28:16,294 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/ApartmentInstance
2019-08-14 12:28:16,294 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QApartmentInstance.java
2019-08-14 12:28:16,295 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocApartmentInstance.java
2019-08-14 12:28:16,295 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/Foundation
2019-08-14 12:28:16,296 [main] WARN |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: No support for field [logo] desc[[B] signature [null]
2019-08-14 12:28:16,296 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QFoundation.java
2019-08-14 12:28:16,297 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocFoundation.java
2019-08-14 12:28:16,297 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/Registration
2019-08-14 12:28:16,297 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QRegistration.java
2019-08-14 12:28:16,298 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocRegistration.java
2019-08-14 12:28:16,298 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/ActivationCode
2019-08-14 12:28:16,298 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QActivationCode.java
2019-08-14 12:28:16,299 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocActivationCode.java
2019-08-14 12:28:16,299 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/RememberMe
2019-08-14 12:28:16,299 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QRememberMe.java
2019-08-14 12:28:16,300 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocRememberMe.java
2019-08-14 12:28:16,300 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/Payment
2019-08-14 12:28:16,300 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QPayment.java
2019-08-14 12:28:16,300 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocPayment.java
2019-08-14 12:28:16,301 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/MailQueue
2019-08-14 12:28:16,301 [main] WARN |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: No support for field [content] desc[[B] signature [null]
2019-08-14 12:28:16,301 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QMailQueue.java
2019-08-14 12:28:16,301 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocMailQueue.java
2019-08-14 12:28:16,302 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/Apartment
2019-08-14 12:28:16,302 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QApartment.java
2019-08-14 12:28:16,302 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocApartment.java
2019-08-14 12:28:16,303 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/Reservation
2019-08-14 12:28:16,303 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QReservation.java
2019-08-14 12:28:16,303 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocReservation.java
2019-08-14 12:28:16,303 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/Property
2019-08-14 12:28:16,304 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QProperty.java
2019-08-14 12:28:16,304 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocProperty.java
2019-08-14 12:28:16,304 [main] INFO |io.ebean.typequery.generator.Generator: generate for pl/rtprog/bookme/database/tables/BaseFile
2019-08-14 12:28:16,304 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/QBaseFile.java
2019-08-14 12:28:16,305 [main] INFO |io.ebean.typequery.generator.write.SimpleQueryBeanWriter: writing /home/rysiek/workspace/bookme/server/src/main/java/pl/rtprog/bookme/database/tables/query/assoc/QAssocBaseFile.java
rbygrave commented 5 years ago

Hmmm. I'm not that keen on this approach to generate query beans and much prefer using annotation processors. I've got a lot on my plate so can you look into this yourself, debug it and prepare a PR?

Ryszard-Trojnacki commented 5 years ago

OK, I will check this.

Ryszard-Trojnacki commented 5 years ago

OK. I have found the cause of this problem.

This is because of fields:

    /** Informacja o liczbie miejsc w pokojach */
    @DbArray(length = 32)
    @Column(name = "rooms", nullable = false)
    private List<Integer> rooms=new ArrayList<>();

    @DbArray(length = 9*400)
    @Column(name = "prices", nullable = false)
    private List<Long> prices=new ArrayList<>();

For those fields SimpleQueryBeanWriter.addClassProperties -> GenerationMetaData.getPropertyType is returning PropertyTypeArray:

    if (EntityBeanPropertyReader.dbArrayField(field)) {
      String collectParamType = deriveCollectionParameterType(field.signature);
      String shortType = langShortType(deriveShortName(collectParamType));
      return new PropertyTypeArray(collectParamType, shortType);
    }

And PropertyTypeArray.addImports:

  @Override
  public void addImports(Set<String> allImports) {
    super.addImports(allImports);
    allImports.add(elementClass);
  }

Just adds this type to imports - elementClass which is java.lang.Integer.

Then it is replaced by/in SimpleQueryBeanWriter.translateKotlinImportTypes, which is called no matter what.

Not sure hot to fix this:

  1. One option is to check in SimpleQueryBeanWriter if is Kotlin and only then call translateKotlinImportTypes.
  2. Second is to ignore import for java.lang.Integer in PropertyTypeArray.addImports.

First thing I can make without any problems - it is just one if. Second thing I'm not sure how to make. Should I only do the first thing?

And i think same thing is with QAssoc....

rbygrave commented 5 years ago

Good work!!

One option is to check in SimpleQueryBeanWriter if is Kotlin and only then call translateKotlinImportTypes.

Yes. Lets definitely do option 1.

Second is to ignore import for java.lang.Integer in PropertyTypeArray.addImports.

I suspect we want to ignore that import for Java or conversely it might be a better approach when using Java to remove all imports for java.lang.* (that might be simpler/cleaner).

Yeah. I'll need to have a look on this second point.

rbygrave commented 5 years ago

Fixed by https://github.com/ebean-orm-tools/finder-generator/pull/29