I have an Object instance obj and a Method object method. How can I check to see if obj would be a valid return for the method?
Assume method has no parameters and it's not a return type of void (confirmed as a getter).
For example, if obj is null then the return type for method cannot be a primitive. Alright, got that figured out. However, Class.isAssignableFrom does not take into account unboxing and boxing from primitives. For example int.class.isAssignableFrom(Integer.class) returns false, but that would be a valid return for the method.
An option I could take is use Class.cast but using exceptions for programming flow is rather nasty and generating exceptions is also costly. But it is something I am aware of.
I'm kind of stumped on exactly how to figure this all out elegantly. Help would be much appreciated.
Aucun commentaire:
Enregistrer un commentaire