简体   繁体   English

在java中向下转换数组

[英]Downcasting of arrays in java

I am not sure how downcasting of arrays works. 我不确定数组的向下转换是如何工作的。
Example this works: 这个例子有效:

String[] sArray = {"a", "b"};  
Object[] o = sArray;  
f((String[]) o);  

static void f(String[] s){  
  System.out.println("Ok");   
}  

But for the following: 但对于以下内容:
new F(cert, (X509Certificate[]) ks.getCertificateChain("ALIAS"), key));
where F is 其中F

public F(X509Certificate certificate, X509Certificate[] certificateChain, PrivateKey privateKey) {      
}  

I get ClassCastException 我得到ClassCastException

java.lang.ClassCastException: [Ljava.security.cert.Certificate; java.lang.ClassCastException:[Ljava.security.cert.Certificate; incompatible with [Ljava.security.cert.X509Certificate; 与[Ljava.security.cert.X509Certificate;不兼容;

Why? 为什么?

Arrays in Java are objects, and as such, have a class. Java中的数组是对象,因此有一个类。 They aren't simply identityless containers. 它们不仅仅是无身份的容器。 When you say: 当你说:

String[] sArray = {"a", "b"};

You are creating an object of class String[] , and putting two strings in it. 您正在创建String[]类的对象,并在其中放入两个字符串。 There is an alternative way of writing it which makes that clearer: 有一种替代的写作方式,这使得更清楚:

String[] sArray = new String[] {"a", "b"};

You can cast references to arrays in much the same way as you can cast references to any other objects; 您可以使用与向任何其他对象转换引用相同的方式转换对数组的引用; you can always make a widening conversion (eg assigning an expression of type String[] to a variable of type Object[] ), and you can make a narrowing conversion using an explicit cast (eg assigning an expression of type Object[] to a variable of type String[] ). 您总是可以进行扩展转换(例如,将String[]类型的表达式赋值给Object[]类型的变量,并且可以使用显式转换进行缩小转换(例如,将Object[]类型的表达式赋值给a String[]类型的变量。 As with other objects, when you do an explicit case, the actual class of the object is checked at runtime, and if it doesn't fit the desired type, a ClassCastException is thrown. 与其他对象一样,当您执行显式情况时,将在运行时检查对象的实际类,如果它不符合所需类型,则抛出ClassCastException As with other objects, the actual class of the object does not change when you cast. 与其他对象一样,对象的实际类在转换时不会更改。 Only the type of the variable does. 只有变量类型才有。

In your case, it looks like ks.getCertificateChain("ALIAS") returns a Certificate[] . 在您的情况下,看起来ks.getCertificateChain("ALIAS")返回Certificate[] It's possible that the elements in it are instances of X509Certificate , but the array itself is still a Certificate[] . 它中的元素可能是X509Certificate实例,但数组本身仍然是Certificate[] When you try to cast it to X509Certificate[] , that fails. 当您尝试将其X509Certificate[]X509Certificate[]时,失败。

If you need a X509Certificate[] , then what you will have to do is copy the contents of the array into a new array which is a X509Certificate[] . 如果需要X509Certificate[] ,那么您需要做的是将数组的内容复制到一个新的数组中,该数组是X509Certificate[] You can do that as tbk suggests, or you can call Arrays.copyOf , looking something like: 你可以像tbk建议那样做,或者你可以调用Arrays.copyOf ,看起来像:

Certificate[] Certificates = ks.getCertificateChain("ALIAS");
X509Certificate[] x509Certificates = Arrays.copyOf(certificates, certificates.length, X509Certificate[].class);

You can not cast down an array in java, but you can work around it by doing the following: 你不能在java中强制转换数组,但你可以通过执行以下操作来解决它:

Certificate[] certs = ...;
X509Certificate[] x509certs = new X509Certificate[certs.length];
for(int i = 0; i < certs.length; i++) {
  x509certs[i] = (X509Certificate)certs[i];
}

Edit: An alternative to this is shown in this post . 编辑: 这篇文章中显示替代方案。 However, it involves System.arraycopy() which will likely perform the same when it comes to runtime. 但是,它涉及System.arraycopy() ,它可能在运行时执行相同的操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM