java - If A extends B extends C, why can I cast to A but get a ClassCastException casting to C? -
i trying read asn1 object using bouncycastle on android. expect dersequence, in bouncycastle subclass of asn1sequence, subclass of asn1object.
import org.bouncycastle.asn1.asn1inputstream; import org.bouncycastle.asn1.asn1object; import org.bouncycastle.asn1.asn1sequence; import org.bouncycastle.asn1.dersequence; ... asn1inputstream ais = ...; object o = ais.readobject(); // eclipse's debugger says o dersequence, expected. dersequence o2 = (dersequence)o; asn1sequence o3 = o2; asn1object o4 = o3; // , o4 want. asn1object o5 = (asn1object)o; // throws: /// java.lang.classcastexception: org.bouncycastle.asn1.dersequence based on feedback answers, have constructed another, shorter example:
object o = new dersequence(); asn1object o1 = new dersequence(); // behaves fine. asn1object o2 = (asn1object)o; // throws classcastexception. what causes cast fail?
android has modified class hierarchy here, see comment in http://www.netmite.com/android/mydroid/1.5/dalvik/libcore/security/src/main/java/org/bouncycastle/asn1/asn1sequence.java absolutely sure version using dersequence subtype of asn1object?
e.g here http://www.eecs.berkeley.edu/~jonah/bc/org/bouncycastle/asn1/dersequence.html
but not here http://www.androidjavadoc.com/m3-rc37a/org/bouncycastle/asn1/dersequence.html
Comments
Post a Comment