1

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?

1
  • 1
    the Method.invoke should return wrapper for primitive type. Commented Oct 3, 2011 at 20:57

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Honestly, I'd probably use something like -1. Then test for the value being -1 instead of null. This works as well, though.
@Chris With integers it works fine. The problem is when you need to test whether a boolean field was set or not, as the primitive will always return only true or false, but will never be null. Because of that, I'd rather adopt a singular standard for all fields that should be primitives.
2

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.

Comments

2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.