Is there a single method which returns true if a type is a primitive
Class.isPrimitive:
Class<?> type = ...;
if (type.isPrimitive()) { ... }
Note that void.class.isPrimitive()
is true too, which may or may not be what you want.
a primitive wrapper?
No, but there are only eight of them, so you can check for 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?
Simply:
if (type == String.class) { ... }
That's not one method. I want to determine whether it's one of those named or something else, in one method.
Okay. How 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;
}