Recursive Field Check by Reflection

D

David

Hi,

I am looking for a piece of code to do the following:

Getting an object and a field name as a parameter, the code will check
the object fields (including the object's ancestors fields)
recursively to find out if the fields named "X" are null (or any other
given value).

Anybody has an idea where to look for?

10x a lot,

David
 
T

Tom Anderson

Just typed in and not tested:

static boolean isFieldNull(Object obj, String name)
throws Throwable // you could be more specific here
{
Class clown = obj.getClass();
Field field = clown.getDeclaredField(name);
Object value = field.get(obj);
return value == null;
}

Checking for "any other given value" could be a bit more laborious,
depending on how much you can assume about the type of that value.

Can't you just pass in an Object value, and do
value.equals(field.get(obj))? I believe primitive values get boxed, so
this should work for them too.

Obviously, that won't for for checking for null. So write

(value != null) ? value.equals(field.get(obj)) : field.get(obj) == null
Access restrictions could bollix things, as could security managers
and/or multiple class loaders.

You will need to call field.setAccessible(true) to read private fields.
And yes, any kind of security manager or classloader situation could make
life more complicated.
Off-hand I'm not sure whether getDeclaredField() will find fields
that are inherited from superclasses; I think it will but you should
check.

I think it won't. I think the 'declared' means that it only finds fields
declared in that class. getField() will find fields regardless of where
they're declared. The javadoc for getDeclaredField doesn't make this
clear, but that for getField implies it.
If you're worried about a subclass' field "hiding" a superclass' field
of the same name, I guess you could use getSuperclass() on the Class
object to walk up the inheritance tree.

Yes, i think that will be necessary.

If you're not worried about hiding, though, getField is probably the way
to go.

tom
 
D

David

Annie said:
10x a lot,

That's a dekalot.

--
Lew

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[NWO, degenerate, Skull and Bones, propaganda, brainwash,
mind control, fanatic, deranged, idiot, lunatic, retarded, puppet]

"You're free. And freedom is beautiful. And, you know,
it'll take time to restore chaos and order -- order
out of chaos. But we will."

--- Adolph Bush,
Washington, D.C.,
April 13, 2003

Thanks,

equals will not work in my scenario as I need the fields to be of
equal content but different references.

I also need to check all super classes fields and every field's fields
too.

What I need is to make sure that a complex hibernate mapped object (a
Long id for each mapped field) copy does not contain id values.

This means that I have a deep copy and hibernate will not treat the
object as the original object in all levels.

10x again,
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top