Troubleshooting
Problem
The problem here has been found that we do not like casting a full array of a cross-compiled Java interface into an array of generic Java Objects.
We are casting an array of Java Action[] objects into Object[]. Although we can cast arrays of implementations, we cannot cast without exception when the Array type is of a Java interface. This is why making a new Object[] array and assigning each index to match with the Action[] array, with the types cast individually as Java Object works - because we are dealing with specific implementations in each case. Similarly, an array of an implementation of Action (e.g. DefaultAction?[]) would cast fine to Object[] however casting an array of Java interfaces to Object[] will cause the C# system to throw a ClassCastException, presumably because the interface itself does not explicitly extend Java Object and thus cannot be cast.
Example code:
Java:
public interface Thing {
}
public class DefaultThing implements Thing {
}
C# referencing the cross compiled Java:
Thing[] collection = new Thing[] { new DefaultThing(), new DefaultThing() };
DefaultThing[] collectionImpl = new DefaultThing[] { new DefaultThing(), new DefaultThing() };
JObject[] castSuccess = (JObject[])collectionImpl; // This should be fine
JObject[] castFail = (JObject[])collection; // This should throw a System.ClassCastException
Document Location
Worldwide
Log InLog in to view more of this document
Was this topic helpful?
Document Information
Modified date:
19 March 2025
UID
ibm17191229