jeudi 15 octobre 2015

Java is there any way to tell if a class is a simple 'Java' class?

I'm using reflection to read through some class objects I've created and using recursion to trace down through the entire object tree.

The problem I have is that when I have a 'Field' I need to know if its a simple attribute or another complex object as the former can just be processed immediately, but the latter I need to dig into and call the recursive method again. My code is something like this :

recurse(Object o) {
    Class clazz = o.getClass();
    for (Field f : clazz.getDeclaredFields() ) {
        fieldClass = f.getType();

        // Heres the broken part
        if ( !ClassUtils.isPrimitiveOrWrapper(fieldClass) ) {
            recurse(f.get(o));
        }
        else {
            // Process simple attribute
        }
    }
}

I thought this would work, but then I found that String is not a 'PrimitiveOrWrapper'.... then I found that Date isn't either... then I found that BigDecimal isn't either...

Is there really NO class or utility package that can handle this request? All I want is to know if a Class is a internal / native / plain / simple / basic Java class or a complex object that I've written. This shouldnt be hard.

So I believe my choices are :

  1. Trial and error to list them all individually - hoping that 2 yrs in the future I dont add another attribute of a type I haven't used before (Yuck!)
  2. See if the class name begins with the string java. (double Yuck!)

Please tell me I missed a nice utility class/method somewhere.





Aucun commentaire:

Enregistrer un commentaire