【问题标题】:incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object不兼容的类型:推理变量 T 具有不兼容的边界等式约束:capture#1 of ?扩展 java.lang.Object
【发布时间】:2023-03-14 22:30:02
【问题描述】:

我正在尝试连接以运行查询以获取 MongoDB 中的所有记录,然后将记录转换为引用对象类型的列表,我将其作为调用类的泛型。代码运行良好并在 Eclipse 中实现了预期的结果,但在 maven build 期间出现编译错误,maven 和 eclipse 都引用相同的 JDK(1.8)。有人可以帮我解决这个问题吗

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

}  

日志:-

[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

关于复制同一存在的完整消息:-

更新:显式类型转换对象变量有效,但我仍然需要了解如何?

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

【问题讨论】:

  • 在 Mave 构建中使用 javac,而 Eclipse IDE 使用自己的编译器来增量编译 Java 代码。 javac 的错误消息不是很丰富(例如,您的代码中没有capture#1)。你确定代码是由代码sn-p引起的吗?您能否还显示使用MongoPersistenceImpl&lt;T&gt; 的代码?
  • @howlger 错误中包含行号,指向 T report=gson.fromJson(cursor.next().toJson(),modelObject.getClass()); 作为错误行

标签: java eclipse mongodb maven gson


【解决方案1】:

当您尝试将对象强制转换为特定的 Typereport 时,请尝试更改

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

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

【讨论】:

  • 感谢您的回答..它有效..您能详细说明一下具体问题是什么吗?
  • 问题是你试图转换一个类的类型是未知的。当您将其转换为 Type 时,您将为 Java 中的任何类型提供超接口,其中 T 可以在您的泛型定义中。
  • aah.. 好的.. 这就是为什么显式类型转换对象之后工作.. 谢谢:)
  • @SakshamB 希望有所帮助:)
  • 这里不涉及bounded type parameters。错误消息inference variable T has incompatible bounds 对我来说没有任何意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多