java-native-access/jna · error · IllegalArgumentException
Unsupported size
Error message
Unsupported size: <size>
What it means
IntegerType.setValue throws this IllegalArgumentException when the constructor/fromNative path is given a size (in bytes) that is not 1, 2, 4, or 8. IntegerType backs C integer types (uint8_t, int16_t, etc.) and only supports those native widths.
Solutions
- Use size 1, 2, 4, or 8 matching the actual C type (int8/uint8=1, int16=2, int32=4, int64=8).
- Fix the subclass constructor super(size) call to a supported width.
- For sizes not representable, model with a byte array in a Structure or use Memory directly.
Example fix
// before
class UInt24 extends IntegerType { UInt24(int v){ super(3, v); } }
// after
class UInt32 extends IntegerType { UInt32(int v){ super(4, v); } } Defensive patterns
Strategy: validation
Validate before calling
static void checkIntegerTypeSize(int size) {
if (size != 1 && size != 2 && size != 4 && size != 8)
throw new IllegalArgumentException("IntegerType size must be 1,2,4, or 8, got " + size);
} Try / catch
try {
IntegerType v = new MyInt(raw, size);
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Unsupported size:")) {
throw new IllegalStateException("Size " + size + " has no IntegerType backing; use 1/2/4/8", e);
}
throw e;
} Prevention
- Only subclass IntegerType with sizes 1, 2, 4, or 8.
- Match size to the exact C type width (stdint mapping).
- Reuse built-in types (BYTE, WORD, DWORD_WORD equivalents, NativeLong) instead of custom subclasses.
- Test constructors of custom IntegerType subclasses.
When it happens
Trigger: Constructing a custom IntegerType subclass with new MyIntegerType(size, value) where size is e.g. 3, 16, or 0; calling fromNative with a FromNativeContext specifying an invalid size; a library returning a size outside {1,2,4,8}.
Common situations: Hand-written IntegerType subclasses for exotic C types (e.g. 3-byte or 128-bit integers); copy-paste errors in the size argument; custom type mappers supplying wrong sizes.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Argument value 0x exceeds native capacity ( bytes) mask=0x
- Allocation size must be greater than zero
- Area is not polygonal: " + area
- Array fields must be initialized
- Arrays of length zero not allowed: type
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/21ecd7c703923853.
Report an issue: GitHub.
Appendix: source
Thrown at src/com/sun/jna/IntegerType.java:104
case 2:
if (unsigned) {
this.value = value & 0xFFFFL;
}
truncated = (short) value;
this.number = Short.valueOf((short) value);
break;
case 4:
if (unsigned) {
this.value = value & 0xFFFFFFFFL;
}
truncated = (int) value;
this.number = Integer.valueOf((int) value);
break;
case 8:
this.number = Long.valueOf(value);
break;
default:
throw new IllegalArgumentException("Unsupported size: " + size);
}
if (size < 8) {
long mask = ~((1L << (size * 8)) - 1);
if ((value < 0 && truncated != value)
|| (value >= 0 && (mask & value) != 0)) {
throw new IllegalArgumentException("Argument value 0x"
+ Long.toHexString(value) + " exceeds native capacity ("
+ size + " bytes) mask=0x" + Long.toHexString(mask));
}
}
}
@Override
public Object toNative() {
return number;
}
@OverrideView on GitHub (pinned to d036ad9781)