Skip to content

Commit

Permalink
refactor & getObjectClass
Browse files Browse the repository at this point in the history
  • Loading branch information
n9Mtq4 committed Jun 19, 2015
1 parent ba8afaa commit 1ba6021
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/com/n9mtq4/reflection/ReflectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2427,17 +2427,35 @@ public static Class[] getClassParams(Object[] params) {
Class[] classParams = new Class[params.length];
for (int i = 0; i < classParams.length; i++) {
Class clazz = params[i].getClass();
if (isPrimitive(clazz)) {
classParams[i] = getPrimitive(clazz);
if (canBePrimitive(clazz)) {
classParams[i] = getPrimitiveClass(clazz);
}else {
classParams[i] = clazz;
}
}
return classParams;
}

/**
* Checks if the class has a primitive version.
*
* @param clazz The class to check
* @return If the class given has a primitive version
* @see ReflectionHelper#isPrimitive(Class)
* */
public static boolean canBePrimitive(Class clazz) {
return getPrimitiveClass(clazz) != null;
}

/**
* Checks if the given class is primitive.
*
* @param clazz The class to check
* @return If the class is primitive
* @see ReflectionHelper#canBePrimitive(Class)
* */
public static boolean isPrimitive(Class clazz) {
return getPrimitive(clazz) != null;
return getObjectClass(clazz) != null;
}

/**
Expand All @@ -2448,9 +2466,10 @@ public static boolean isPrimitive(Class clazz) {
* and returns a primitive one (ie. int.class)
*
* @param clazz The Object class to get the primitive version
* @return The Primitive version, or null if there isn't one
* @return The Primitive version, or null if there isn't one
* @see ReflectionHelper#getObjectClass(Class)
* */
public static Class getPrimitive(Class clazz) {
public static Class getPrimitiveClass(Class clazz) {
if (clazz == Boolean.class) return boolean.class;
if (clazz == Byte.class) return byte.class;
if (clazz == Character.class) return char.class;
Expand All @@ -2462,4 +2481,27 @@ public static Class getPrimitive(Class clazz) {
return null;
}

/**
* Primitive types have two classes in java.
* The primitive one (ie. int.class) and the
* Object one (ie. Integer.class).
* This takes the Object one (ie. int.class)
* and returns the primitive one (ie. Integer.class)
*
* @param clazz The primitive class to get the Object version
* @return The Object version, or null if there isn't one
* @see ReflectionHelper#getPrimitiveClass(Class)
* */
public static Class getObjectClass(Class clazz) {
if (clazz == boolean.class) return Boolean.class;
if (clazz == byte.class) return Byte.class;
if (clazz == char.class) return Character.class;
if (clazz == double.class) return Double.class;
if (clazz == float.class) return Float.class;
if (clazz == long.class) return Long.class;
if (clazz == short.class) return Short.class;
if (clazz == int.class) return Integer.class;
return null;
}

}

0 comments on commit 1ba6021

Please sign in to comment.