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 pr开发者_开发知识库oblem 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?
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.
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.
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
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
精彩评论