Let's imagine one retrieves the declaring type of a Field
using reflection.
Which of the following tests will correctly indicate whether one is dealing with an int
or an Integer
?
Field f = ...
Class<?> c = f.getDeclaringClass();
boolean isInteger;
isInteger = c.equals(Integer.class);
isInteger = c.equals(Integer.TYPE);
isInteger = c.equals(int.class);
isInteger = ( c == Integer.class);
isInteger = ( c == Integer.TYPE);
isInteger = ( c == int.class);
Based on
Field.getType()
(instead off.getDeclaringClass()
), I get the following:Meaning the following is true:
Meaning if I want to find out whether I am dealing with an
int
or anInteger
, I can use any of the following tests:Is there a corner case I am missing? If yes, please comment.
int.class
compiles down toInteger.TYPE
. However, I think you are usingField.getDeclaringClass()
incorrectly, as this returns theClass
object representing the class that declares the field. What you would want to use isField.getType()
.