So you want to get the wrapper class type, ok.
No need to query the types or reference look up tables because java already does it anyway. Let's walk through the problem together...
Synopsis
We are retrieving a field and then find it contains a primitive type.
Field f = getTheField(); // Dummy method that returns my Field
Class<?> c = f.getType(); // Gets the declared primitive type
But instead we want the wrapper type.
Primitive types in Java
Now as you already found out the only thing a primitive class is good for is to return true for c.isPrimitive();
.
From wiki books - java programming:
Primitive types are the most basic data types available within the Java language. There are 8: boolean , byte , char , short , int , long , float and double . These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose — containing pure, simple values of a kind.
So why do we want to know the wrapper type?
Attempting to use primitives in any other way and you are in for a lot of hurt.
Cannot make a new instance of a primitive.
Field f = getTheField();
Class<?> c = f.getType();
Object wrapper = c.newInstance();
// java.lang.InstantiationException thrown: int
// at Class.newInstance (Class.java:545)
Cannot cast to a primitive type.
Field f = getTheField();
Class<?> c = f.getType();
Object wrapper = c.cast(0);
// java.lang.ClassCastException thrown: Cannot cast java.lang.Integer to int
// at Class.cast (Class.java:3578)
Can cast to a null wrapper type. Yeah! \o/
Field f = getTheField();
Class<?> c = f.getType();
Object wrapper = c.cast(null);
No exceptions and the variable wrapper
is of type class java.lang.Integer
but with a value of null
, a whole lot of good that will do us.
Primitives are not even inherited from wrappers.
boolean isSuperClass = Integer.class.isAssignableFrom(int.class); // false
This is obviously not getting us anywhere so lets rather take a step back from the problem and have a look at the bigger picture.
When at first you don't succeed...
Lets recap: We are retrieving a field which has to come from somewhere so if we were to fill in the gaps left out in the question it might look something like this.
public class Simple {
public static int field;
public static Field getTheField() {
return Simple.class.getField("field"); // Actual method that returns our Field
}
public static void main(String[] args) {
Field f = getTheField();
Class<?> c = f.getType();
}
}
Instead of fighting against the machine lets rather work with it. One of the perks of primitives are that they will initialise to a default value 0
instead of null
. Lets see if we can use that.
Get wrapper class from wrapped instance.
public class Simple {
public static int field;
public static Field getTheField() {
return Simple.class.getField("field"); // Actual method that returns our Field
}
public static void main(String[] args) {
Field f = getTheField();
Object wrapped = f.get(null); // Integer value 0
Class<?> c = wrapped.getClass(); // class java.lang.Integer
}
}
That was much easier than before and we didn't even have to do anything, auto boxing, everything was done for us. Yet another perk for not trying to go against the stream.
Lets improve on that, refactor and make it a little more reusable by extracting a method.
Implement a manual boxing method.
We can do the same auto boxing with generics:
public class Simple {
public static int field;
public static Field getTheField() {
return Simple.class.getField("field"); // Actual method that returns our Field
}
public static <T> T wrap(T t) {
return t;
}
public static void main(String[] args) {
Field f = getTheField();
Class<?> c = Simple.wrap(f.get(null)).getClass(); // class java.lang.Integer
}
}
A simple primitive wrap without ever having to look at the types or use look up tables because java already does it anyway.
Conclusion
The simple solution using pure java to get the wrapper class from a primitive field:
Field f = getTheField(); // Dummy method that returns my Field
Class<?> c = f.get(null).getClass();
Or you can replace null with an instance if the field is not static.
nJoy!