简体   繁体   English

不兼容的类型:推理变量T具有不兼容的边界等式约束:捕获#1的? 扩展java.lang.Object

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

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. 我正在尝试连接以运行查询以获取MongoDB中的所有记录,然后将记录转换为我作为我的调用类的通用参考对象类型的列表。 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). 代码运行正常并在Eclipse中实现了所需的结果,但在maven构建期间出现了编译错误,maven和eclipse都引用了相同的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 :- 关于复制同一个人的完整信息: -

在此输入图像描述

UPDATE : explicitly type casting the an Object variable worked , but I still need to understand how? 更新 :明确键入一个Object变量的类型,但我仍然需要了解如何?

  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();
        }
    }

While you are trying to cast an object to a specific Type of report , try changing 在尝试将对象强制转换为特定Typereport ,请尝试更改

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

to

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java:不兼容类型:推理变量T具有不兼容的边界等式约束:下限:java.util.List &lt;&gt; - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> java:不兼容的类型:推理变量 T 具有不兼容的边界等式约束:下限:java.util.List&lt;&gt; 用于添加字段 - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> for adding field 修复 java 中的错误:不兼容的类型:java.lang.Object 无法转换为 capture#1 of? - Fixing error in java: incompatible types: java.lang.Object cannot be converted to capture#1 of? 类型安全合并 Map 与多种类型的值; `不兼容的类型 java.lang.Object 无法转换为捕获 #1 的 ?` - Type safe merging of Map with multiple types for its value; `incompatible types java.lang.Object cannot be converted to capture#1 of ?` 无与伦比的类型:java.lang.Class<capture#1 of ? extends T[]> 和 java.lang.Class<java.lang.Object[]> - Incomparable types: java.lang.Class<capture#1 of ? extends T[]> and java.lang.Class<java.lang.Object[]> 编译生成以下错误:“错误:不兼容的类型:推理变量 T 具有不兼容的等式约束 Todo,TodoCouchDB” - Compiling generating the following error: “error: incompatible types: inference variable T has incompatible equality constraints Todo,TodoCouchDB” java EnumSet,不兼容的类型:推断变量E具有不兼容的范围 - java EnumSet, incompatible types: inference variable E has incompatible bounds java:不兼容的类型:推断变量 RR 的边界不兼容 - java: incompatible types: inference variable RR has incompatible bounds Java通用问题:“类型不兼容;推断的类型参数java.lang.Object不符合类型变量T的界限 - Java generic issue: "incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) T 不兼容的类型:java.lang.Class<capture#1 of ?> 无法转换为 - Incompatible types: java.lang.Class<capture#1 of ?> cannot be converted to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM