java 原始数据类型
我正在使用反射从 java 对象创建条件查询。该功能如下,
private void createCriteria(Class searchClass, Object object, Criteria criteria, Field field, ClassMetadata classMetadata)
throws Exception, DAOSystemException
{
String fieldName = field.getName();
Object fieldValue = invokeGetterMethod(object.getClass(), getRoleNameForMethodInvocation(field.getName()), object);
if (fieldValue != null)
{
Class fieldTypeClass = field.getType();
addCriteria(criteria, fieldName, fieldValue, fieldTypeClass, classMetadata);
}
}
当“字段”是原始数据类型时,我遇到问题。在这种情况下,以下检查将失败。
if (fieldValue != null)
是否有任何 API 可用于检查原始数据类型及其默认值?
I am creating a criteria query from a java object using reflection. The function is as follows
private void createCriteria(Class searchClass, Object object, Criteria criteria, Field field, ClassMetadata classMetadata)
throws Exception, DAOSystemException
{
String fieldName = field.getName();
Object fieldValue = invokeGetterMethod(object.getClass(), getRoleNameForMethodInvocation(field.getName()), object);
if (fieldValue != null)
{
Class fieldTypeClass = field.getType();
addCriteria(criteria, fieldName, fieldValue, fieldTypeClass, classMetadata);
}
}
i am having a problem when "field" is a primitive datatype. In this case following check would fail.
if (fieldValue != null)
Is there any API available to check the primitive data type and its default value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我所做的是始终使用兼容的字段类型并避免使用基元。
因此,对于布尔值,我会使用 Boolean 类型而不是原始类型 boolean;对于整数,我会使用 Integer 类型而不是 int。
假设你有:
你可以使用:
然后你可以测试 (age != null)。
希望这有帮助。
What I do is always use a compatible type for field and avoid using primitives.
So, for booleans, I'd use the type Boolean over the primitive type boolean, for integers, I'd use the type Integer over int.
Suppose you have:
You could use:
Then you can test (age != null).
Hope this helps.
它不会失败。它将正常工作 - 字段值永远不会为
null
,因此该字段将包含在条件中。venue=0
与venue=10000
一样有效。如果您希望默认值表示“无值”,则可以使用包装类型 (Integer
)。另一个特殊值可能是Integer.MIN_VALUE
。显然,这不适用于布尔值。It won't fail. It will work properly - the field value will never be
null
, so the field will be included in the criteria. Andincome=0
is as valid asincome=10000
. If you want the default values to mean "no value", then you can use wrapper types (Integer
). Another special value may beInteger.MIN_VALUE
. That, obviously, doesn't work for booleans.您可以使用以下方法来检查对象的属性是否为空或具有默认值:
这是一个示例用法。如果我们有以下类:
那么我们可以在 main 方法中进行如下测试:
结果将是:
You can use the following method for check if the attribute of an Object is null or has default value:
Here is a sample usage. If we have the following class:
So we can test in a main method as follows:
The result will be:
为了确定类型是否不是对象而是原始类型,您可以使用 getClass().isPrimitive()
http://download.oracle.com/javase/1,5,0/docs/api/java/lang/Class.html#isPrimitive%28%29
for determining if the type is not an object but primitive you could use getClass().isPrimitive()
http://download.oracle.com/javase/1,5,0/docs/api/java/lang/Class.html#isPrimitive%28%29