Skip to content

Instantly share code, notes, and snippets.

@lwr
Last active April 27, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwr/666d1a4ce985810ce3318cbe1ad76fb0 to your computer and use it in GitHub Desktop.
Save lwr/666d1a4ce985810ce3318cbe1ad76fb0 to your computer and use it in GitHub Desktop.
a simple code demonstrating what java primitive type is
// the implemention of getPrimitiveWrapperClass & getPrimitiveClass
public static <T> Class<T> getPrimitiveWrapperClass(Class<T> primitiveClass) {
if ((primitiveClass == null) || !primitiveClass.isPrimitive()) {
throw new IllegalArgumentException("Not a primitive class: " + primitiveClass);
}
String name = primitiveClass.getName();
if (primitiveClass == int.class) {
name = "Integer";
} else if (primitiveClass == char.class) {
name = "Character";
} else {
name = Character.toUpperCase(name.charAt(0)) + name.substring(1);
}
try {
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
Class<T> wrapperClass = (Class<T>) Class.forName("java.lang." + name, true, null);
return wrapperClass;
} catch (ClassNotFoundException e) {
// never happened
throw new AssertionError();
}
}
public static <T> Class<T> getPrimitiveClass(Class<T> wrapperClass) {
// Integer.class
// public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
try {
if (wrapperClass.getName().startsWith("java.lang.")) {
@SuppressWarnings({"unchecked"})
Class<T> primitiveClass = (Class<T>) wrapperClass.getField("TYPE").get(null);
if ((primitiveClass != null) && primitiveClass.isPrimitive()) {
return primitiveClass;
}
}
} catch (Exception ignore) {}
throw new IllegalArgumentException("Not a primitive wrapper class: " + wrapperClass);
}
@Test
public void testReflectForPrimitiveType() throws Exception {
for (Class<?> type : new Class<?>[]{int.class,
long.class,
char.class,
short.class,
byte.class,
double.class,
float.class,
boolean.class,
void.class}) {
assertTrue(type.isPrimitive());
assertEquals(type + " has no any public method", 0, type.getMethods().length);
assertEquals(type + " has no any public field", 0, type.getFields().length);
assertEquals(type + " has no any public constructor", 0, type.getConstructors().length);
assertEquals(type + " declare no any method", 0, type.getDeclaredMethods().length);
assertEquals(type + " declare no any field", 0, type.getDeclaredFields().length);
assertEquals(type + " declare no any constructor", 0, type.getDeclaredConstructors().length);
assertSame(type + " has no super class", null, type.getSuperclass());
assertSame(type + " implements no interface", 0, type.getInterfaces().length);
Class<?> wrapperType = getPrimitiveWrapperClass(type);
println(type + " <-> " + wrapperType);
assertFalse(wrapperType.isPrimitive());
assertFalse(type.equals(wrapperType));
// Integer.class
// public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
assertSame(type, wrapperType.getField("TYPE").get(null));
assertSame(type, getPrimitiveClass(wrapperType));
}
}
@lwr
Copy link
Author

lwr commented Apr 27, 2017

You must implement two methods to make this simple test run

  • getPrimitiveWrapperClass
    giving a primitive type, return the wrapper type, for instance, returning Integer.class for int.class
  • getPrimitiveClass
    giving a wrapper type, return the primitive type, for instance, returning int.class for Integer.class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment