[英]Java: How to determine if type is any of primitive/wrapper/String, or something else
Is there a single method in JDK or common basic libraries which returns true if a type is a primitive, a primitive wrapper, or a String? JDK或公共基本库中是否有单个方法,如果类型是基元,基元包装器或字符串,则返回true?
Ie 即
Class<?> type = ...
boolean isSimple = SomeUtil.isSimple( type );
The need for such information can be eg to check whether some data can be represented in formats like JSON. 对此类信息的需求可以是例如检查某些数据是否可以以诸如JSON的格式表示。 The reason for a single method is to be able to use it in expression language or templates.
单个方法的原因是能够在表达式语言或模板中使用它。
I found something: 我发现了一些东西
Commons Lang : (would have to combine with check for String) Commons Lang :(必须结合检查字符串)
ClassUtils.isPrimitiveOrWrapper()
BeanUtils.isSimpleValueType()
This is what I want, but would like to have it in Commons. 这就是我想要的,但是想在Commons中拥有它。
Is there a single method which returns true if a type is a primitive
是否有一个方法,如果类型是基元,则返回true
Class.isPrimitive : Class.isPrimitive :
Class<?> type = ...;
if (type.isPrimitive()) { ... }
Note that void.class.isPrimitive()
is true too, which may or may not be what you want. 请注意,
void.class.isPrimitive()
也是true,可能是您想要的也可能不是。
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;
}
The java.util.Class
type has the proper methods: java.util.Class
类型具有正确的方法:
Class<?> type = ...
boolean primitive = type.isPrimitive();
boolean string_ = type == String.class;
boolean array = type.isArray();
boolean enum_ = type.isEnum();
boolean interf_ = type.isInterface();
Integer, Float, Character, etc are not primitives; 整数,浮点数,字符等不是原始的; they are wrapper classes which serve as containers for primitives.
它们是包装类,用作基元的容器。 They are reference objects.
它们是参考对象。 True primitives are types like int, float, double, long, byte, char, and boolean -- non-object types.
真正的基元是类型,如int,float,double,long,byte,char和boolean - 非对象类型。 There's a big difference, since
从那以后,有一个很大的不同
value instanceof Float 值instanceof浮动
won't even compile if "value" is a primitive. 如果“value”是原语,则甚至不会编译。 "String" is also not a primitive -- it's a type of object.
“String”也不是原始的 - 它是一种对象。 'null' is also not a primitive -- it's a literal value.
'null'也不是原始的 - 它是一个字面值。
No there is not. 不,那里没有。 And should not be.
不应该。 For tree difference question you should provide tree different answers.
对于树差异问题,您应该提供树不同的答案。
public static <T> boolean isPrimitive(Class<T> klass) {
return klass.isPrimitive();
}
public static <T> boolean isString(Class<T> klass) {
return String.class == klass; //String is final therefor you can not extend it.
}
public static <T> boolean isPrimitiveWrapper(Class<T> klass) {
return Character.class == klass || Boolean.class == klass || klass.isAssignableFrom(Number.class);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.