【问题标题】:Type mismatch: cannot convert from ArrayList<X509Certificate> to Collection<Certificate>类型不匹配:无法从 ArrayList<X509Certificate> 转换为 Collection<Certificate>
【发布时间】:2021-06-23 06:53:45
【问题描述】:

我正在尝试实现 Tomcat-9 的源代码。

我有一行代码如下:

Collection<Certificate> chain = new ArrayList<>(certificateFile.getCertificates());
        if (certificate.getCertificateChainFile() != null) {
            PEMFile certificateChainFile = new PEMFile(certificate.getCertificateChainFile());
            chain.addAll(certificateChainFile.getCertificates());
        }

代码在 Eclipse 中编译得很好,但是当我尝试使用 ant builder 通过 build.xml 构建它时。我收到一条错误消息:

类型不匹配:无法从 ArrayList 转换为 Collection

为什么会观察到这种行为?有什么想法吗?

【问题讨论】:

  • 有很多类叫做Collection。你用import java.util.Collection;吗?其他类型同上:您使用的是哪个 Certficate
  • 为什么不用List而不是Collection,List接口扩展了Collection。所以无论如何你都会没事的。例如List&lt;Certificate&gt;
  • 抱歉,我没有意识到您指的是this code“实施” 是什么意思?如果您将这些行复制到自己的文件中,则可能使用了错误的导入语句。
  • 实施我的意思是我获取他们的源代码并实施我自己的 mwthods 来获取密码和协议等
  • 虽然错误信息被破坏了,但这是容器协方差的 YA 案例;参见例如stackoverflow.com/questions/5082044/… stackoverflow.com/questions/3246137/… stackoverflow.com/questions/2745265/… 等等。

标签: java tomcat9 java-security


【解决方案1】:

导入,一切正常。我将Collection&lt;Certificate&gt; 修改为Collection&lt;X509Certificate&gt;,一切看起来都不错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多