How to return the Class of a generic type

user1604294 :

I have this compilation problem:

enter image description here

Here is the class in question:

package huru.entity;

import io.vertx.core.json.JsonObject;
import java.util.Date;

public class BaseEntity <T extends BaseModel> extends JsonObject {

  private T model;

  public BaseEntity(T m){
    this.model = m;
  }

  public void setUpdateInfo(String user){
    this.model.updatedBy = user;
    this.model.updatedAt = new Date();
  }

  public JsonObject toJsonObject(){
    return JsonObject.mapFrom(this.model);
  }

  public T getEntityType (){
    return this.model.getClass();  // doesn't compile
  }

}

I also tried using

 public T getEntityType (){
    return T;  // doesn't compile
 }

but that clearly doesn't work either. Anybody know how I can return the class instance of that generic type?

I also tried this:

  public Class<T> getEntityType (){
    return this.model.getClass();
  }

and I get:

enter image description here

and then I tried this:

  public Class<? extends T> getEntityType (){
    return this.model.getClass();
  }

and I have:

enter image description here

Vinay Avasthi :

What about the following code. I think it works.

 public Class<? extends BaseModel> getEntityType (){
    return model.getClass();  
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=73289&siteId=1