views:

77

answers:

1

I have following code and now I have type, but I need to have some kind of switch to know if type is for example of String and then do handling for strings. So how can I check if Type t is type of String?

Type t = bean.getClass().getDeclaredField(fieldName).getType();
+4  A: 

Do you want to do a name check or an object-based check?

Since Class implements Type, you can actually go directly and do the equals against String.class For instance: if (t.equals(String.class))

If, however, you want to do a check based on name, then first check if t is an instance of Class, and then cast it to Class, obtain the full type name, and compare.

Uri
Actually `Field.getType()` returns `Class<?>` so he's safe to go this way.
laura
Huh... That's interesting. I should have looked at the JavaDocs. What does it do in the cause of primitives?
Uri
Primitives, though not actually classes, have a Class object associated with them. `Class<Integer> i = int.class;`
ILMTitan