Author: fanningpj
Date: Sat Jun  6 15:12:32 2026
New Revision: 1935071

Log:
try to improve exception

Modified:
   
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java
==============================================================================
--- 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java 
    Sat Jun  6 15:09:47 2026        (r1935070)
+++ 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java 
    Sat Jun  6 15:12:32 2026        (r1935071)
@@ -3201,7 +3201,38 @@ public abstract class XmlObjectBase impl
 
     protected <T extends XmlObject> T[] getXmlObjectArray(QName elementName, 
T[] arrayCon) {
         synchronized (monitor()) {
-            return getBaseList(elementName).toArray(arrayCon);
+            List<XmlObjectBase> list = getBaseList(elementName);
+            try {
+                return list.toArray(arrayCon);
+            } catch (ArrayStoreException e) {
+                return reportArrayStoreException(list, arrayCon, e);
+            }
+        }
+    }
+
+    protected <T extends XmlObject> T[] getXmlObjectArray(QNameSet elementSet, 
T[] arrayCon) {
+        synchronized (monitor()) {
+            List<XmlObjectBase> list = getBaseList(elementSet);
+            try {
+                return list.toArray(arrayCon);
+            } catch (ArrayStoreException e) {
+                return reportArrayStoreException(list, arrayCon, e);
+            }
+        }
+    }
+
+    private <T extends XmlObject> T[] 
reportArrayStoreException(List<XmlObjectBase> list, T[] arrayCon,
+                                                                
ArrayStoreException e) {
+        if (list.isEmpty()) {
+            throw e;
+        }
+        String elementClass = list.get(0).getClass().getName();
+        Class<?> arrayClass = arrayCon.getClass().getComponentType();
+        if (arrayClass == null) {
+            throw e;
+        } else {
+            throw new IllegalStateException("The requested return type for the 
array (" + arrayClass.getName() +
+                    ") is not compatible with the type of underlying elements 
(" + elementClass + ")", e);
         }
     }
 
@@ -3291,12 +3322,6 @@ public abstract class XmlObjectBase impl
         }
     }
 
-    protected <T extends XmlObject> T[] getXmlObjectArray(QNameSet elementSet, 
T[] arrayCon) {
-        synchronized (monitor()) {
-            return getBaseList(elementSet).toArray(arrayCon);
-        }
-    }
-
     protected <T extends XmlObject> T[] xgetArray(QName elementName, 
IntFunction<T[]> arrayCon) {
         synchronized (monitor()) {
             check_orphaned();


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to