java 原始数据类型

发布于 2024-12-07 14:04:24 字数 748 浏览 3 评论 0原文

我正在使用反射从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬﹪忆之殇 2024-12-14 14:04:24

我所做的是始终使用兼容的字段类型并避免使用基元。

因此,对于布尔值,我会使用 Boolean 类型而不是原始类型 boolean;对于整数,我会使用 Integer 类型而不是 int。

假设你有:

class Person {
    int age;
}

你可以使用:

class Person {
    Integer age;
}

然后你可以测试 (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:

class Person {
    int age;
}

You could use:

class Person {
    Integer age;
}

Then you can test (age != null).

Hope this helps.

还不是爱你 2024-12-14 14:04:24

它不会失败。它将正常工作 - 字段值永远不会为 null,因此该字段将包含在条件中。 venue=0venue=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. And income=0 is as valid as income=10000. If you want the default values to mean "no value", then you can use wrapper types (Integer). Another special value may be Integer.MIN_VALUE. That, obviously, doesn't work for booleans.

爱的那么颓废 2024-12-14 14:04:24

您可以使用以下方法来检查对象的属性是否为空或具有默认值:

public static boolean isNullOrDefaultValue(Field field, Object obj) throws Exception {
    boolean result = false;
    if(!field.getType().isPrimitive()) {
        if (field.get(obj) == null) {
            result = true;
        }
    } 
    else{
        Class objClass =  field.getType();
        if (int.class.equals(objClass) ||  long.class.equals(objClass) ||
                short.class.equals(objClass) || byte.class.equals(objClass)) {
            if (field.getLong(obj) == 0) {
                result = true;
            }
        } else if(float.class.equals(objClass) || double.class.equals(objClass)) {
            if (field.getDouble(obj) == 0.0D) {
                result = true;
            }
        } else if(boolean.class.equals(objClass)) {
            if (field.getBoolean(obj) == false) {
                result = true;
            }
        } else if (char.class.equals(objClass)) {
            if (field.getChar(obj) == '\u0000') {
                result = true;
            }
        }
    }
    return result;
}

这是一个示例用法。如果我们有以下类:

class ClassA {
    public int intValue;
    public short shortValue;
    public byte byteValue;
    public long longValue;
    public float floatValue;
    public double doubleValue;
    public char charValue;
    public boolean booleanValue;
    public String stringValue;
}

那么我们可以在 main 方法中进行如下测试:

public static void main(String[] args) throws Exception {
    Class aClass = ClassA.class;
    Object aInst = new ClassA();
    Field[] fields = aClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        System.out.println("Name: " + field.getName() + " Eval:" + isNullOrDefaultValue(field, aInst));
    }
}

结果将是:

Name: intValue Eval:true
Name: shortValue Eval:true
Name: byteValue Eval:true
Name: longValue Eval:true
Name: floatValue Eval:true
Name: doubleValue Eval:true
Name: charValue Eval:true
Name: booleanValue Eval:true
Name: stringValue Eval:true

You can use the following method for check if the attribute of an Object is null or has default value:

public static boolean isNullOrDefaultValue(Field field, Object obj) throws Exception {
    boolean result = false;
    if(!field.getType().isPrimitive()) {
        if (field.get(obj) == null) {
            result = true;
        }
    } 
    else{
        Class objClass =  field.getType();
        if (int.class.equals(objClass) ||  long.class.equals(objClass) ||
                short.class.equals(objClass) || byte.class.equals(objClass)) {
            if (field.getLong(obj) == 0) {
                result = true;
            }
        } else if(float.class.equals(objClass) || double.class.equals(objClass)) {
            if (field.getDouble(obj) == 0.0D) {
                result = true;
            }
        } else if(boolean.class.equals(objClass)) {
            if (field.getBoolean(obj) == false) {
                result = true;
            }
        } else if (char.class.equals(objClass)) {
            if (field.getChar(obj) == '\u0000') {
                result = true;
            }
        }
    }
    return result;
}

Here is a sample usage. If we have the following class:

class ClassA {
    public int intValue;
    public short shortValue;
    public byte byteValue;
    public long longValue;
    public float floatValue;
    public double doubleValue;
    public char charValue;
    public boolean booleanValue;
    public String stringValue;
}

So we can test in a main method as follows:

public static void main(String[] args) throws Exception {
    Class aClass = ClassA.class;
    Object aInst = new ClassA();
    Field[] fields = aClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        System.out.println("Name: " + field.getName() + " Eval:" + isNullOrDefaultValue(field, aInst));
    }
}

The result will be:

Name: intValue Eval:true
Name: shortValue Eval:true
Name: byteValue Eval:true
Name: longValue Eval:true
Name: floatValue Eval:true
Name: doubleValue Eval:true
Name: charValue Eval:true
Name: booleanValue Eval:true
Name: stringValue Eval:true
那支青花 2024-12-14 14:04:24

为了确定类型是否不是对象而是原始类型,您可以使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文