Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use the following method for check if the attribute of an Object is null or has default value:</p> <pre><code>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; } </code></pre> <p>Here is a sample usage. If we have the following class:</p> <pre><code>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; } </code></pre> <p>So we can test in a main method as follows:</p> <pre><code>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 &lt; fields.length; i++) { Field field = fields[i]; System.out.println("Name: " + field.getName() + " Eval:" + isNullOrDefaultValue(field, aInst)); } } </code></pre> <p>The result will be:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload