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