@param type The Java class for which the native size is to be determined @param value an instance of said class (if available) @return the native size of the given class, in bytes. For use with arrays.
(Class<?> type, Object value)
| 1457 | * For use with arrays. |
| 1458 | */ |
| 1459 | public static int getNativeSize(Class<?> type, Object value) { |
| 1460 | if (type.isArray()) { |
| 1461 | int len = Array.getLength(value); |
| 1462 | if (len > 0) { |
| 1463 | Object o = Array.get(value, 0); |
| 1464 | return len * getNativeSize(type.getComponentType(), o); |
| 1465 | } |
| 1466 | // Don't process zero-length arrays |
| 1467 | throw new IllegalArgumentException("Arrays of length zero not allowed: " + type); |
| 1468 | } |
| 1469 | if (Structure.class.isAssignableFrom(type) |
| 1470 | && !Structure.ByReference.class.isAssignableFrom(type)) { |
| 1471 | return Structure.size((Class<Structure>) type, (Structure)value); |
| 1472 | } |
| 1473 | try { |
| 1474 | return getNativeSize(type); |
| 1475 | } |
| 1476 | catch(IllegalArgumentException e) { |
| 1477 | throw new IllegalArgumentException("The type \"" + type.getName() |
| 1478 | + "\" is not supported: " |
| 1479 | + e.getMessage()); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /** |
| 1484 | * Returns the native size for a given Java class. Structures are |