Is there one method that returns true if the type is primitive
Class.isPrimitive :
Class<?> type = ...; if (type.isPrimitive()) { ... }
Note that void.class.isPrimitive() also true, which may or may not be what you want.
primitive shell?
No, but there are only eight of them, so you can check them explicitly:
if (type == Double.class || type == Float.class || type == Long.class || type == Integer.class || type == Short.class || type == Character.class || type == Byte.class || type == Boolean.class) { ... }
a string?
Just:
if (type == String.class) { ... }
This is not one method. I want to determine if this is one of the above or something else, one method.
Good. What about:
public static boolean isPrimitiveOrPrimitiveWrapperOrString(Class<?> type) { return (type.isPrimitive() && type != void.class) || type == Double.class || type == Float.class || type == Long.class || type == Integer.class || type == Short.class || type == Character.class || type == Byte.class || type == Boolean.class || type == String.class; }
Boann source share