incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object

Question

I am trying to connect to run a query to fetch all the records in MongoDB and then convert the records into a list ofthe reference object type I am taking as a generic to my calling class. The code is running fine and achieving the desired result in Eclipse but gives a compilation error during a maven build , both maven and eclipse are referencing the same JDK(1.8). can someone please help me resolve this issue

public class MongoPersistenceImpl<T> {

MongoDatabase database=(MongoDatabase)MongoConnectImpl.getInstance().getConnection();

 public List<T> getAll(T modelObject){
        MongoCollection<Document> collection=database.getCollection(MongoConnectImpl.MONGO_COLLECTION);
        List<T> reportList=new ArrayList<>();
        Gson gson=new Gson();
        MongoCursor<Document> cursor = collection.find().iterator();
        try {
            while (cursor.hasNext()) {
               T report=gson.fromJson(cursor.next().toJson(),modelObject.getClass());
               reportList.add(report);
            }
            return reportList;
        }catch(Exception e){
            CatsLogger.printLogs(3, "30016", e, MongoPersistenceImpl.class,new Object[]{"get all"} );
            return null;
        } finally {
            cursor.close();
        }
    }

}  

Logs :-

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] incompatible types: inference variable T has incompatible bounds
    equality constraints: capture#1 of ? extends java.lang.Object
    upper bounds: T,java.lang.Object

The complete message on reproducing the same being :-

enter image description here

UPDATE: explicitly type casting the an Object variable worked , but I still need to understand how?

  public List<T> getAll(T modelObject){
        MongoCollection<Document> collection=database.getCollection(MongoConnectImpl.MONGO_COLLECTION);

        List<T> reportList=new ArrayList<T>();
        Gson gson=new Gson();
        MongoCursor<Document> cursor = collection.find().iterator();
        try {
            while (cursor.hasNext()) {
               Object rep=gson.fromJson(cursor.next().toJson(),modelObject.getClass());
               T report=(T)rep;//explicit type cast
               reportList.add(report);
            }
            return reportList;
        }catch(Exception e){
            CatsLogger.printLogs(3, "30016", e, MongoPersistenceImpl.class,new Object[]{"get all"} );
            return null;
        } finally {
            cursor.close();
        }
    }

Show source
| java   | eclipse   | maven   | gson   | mongodb   2017-09-28 06:09 1 Answers

Answers to incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object ( 1 )

  1. 2017-09-28 07:09

    While you are trying to cast an object to a specific Type of report, try changing

    T report = gson.fromJson(cursor.next().toJson(), modelObject.getClass());
    

    to

    T report = gson.fromJson(cursor.next().toJson(), (java.lang.reflect.Type) modelObject.getClass());
    

Leave a reply to - incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object

◀ Go back