/** * <p>Converts the specified array of wrapper Class objects to an array of * its corresponding primitive Class objects.</p> * * <p>This method invokes {@code wrapperToPrimitive()} for each element * of the passed in array.</p> * * @param classes the class array to convert, may be null or empty * @return an array which contains for each given class, the primitive class or * <b>null</b> if the original class is not a wrapper class. {@code null} if null input. * Empty array if an empty array passed in. * @see #wrapperToPrimitive(Class) * @since 2.4 */ public static Class<?>[] wrappersToPrimitives(final Class<?>... classes) { if (classes == null) { return null; } if (classes.length == 0) { return classes; } final Class<?>[] convertedClasses = new Class[classes.length]; for (int i = 0; i < classes.length; i++) { convertedClasses[i] = wrapperToPrimitive(classes[i]); } return convertedClasses; }
/** * Gets the number of steps required to promote a primitive number to another * type. * @param srcClass the (primitive) source class * @param destClass the (primitive) destination class * @return The cost of promoting the primitive */ private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) { float cost = 0.0f; Class<?> cls = srcClass; if (!cls.isPrimitive()) { // slight unwrapping penalty cost += 0.1f; cls = ClassUtils.wrapperToPrimitive(cls); } for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) { if (cls == ORDERED_PRIMITIVE_TYPES[i]) { cost += 0.1f; if (i < ORDERED_PRIMITIVE_TYPES.length - 1) { cls = ORDERED_PRIMITIVE_TYPES[i + 1]; } } } return cost; }
private static Object getProperValue(Number number, Class<?> expectedClass) { Class<?> klazz = ClassUtils.wrapperToPrimitive(number.getClass()); Object value = null; // Dexlib will only ever make byte (t), int, long (l), or short (s) if (klazz == byte.class) { value = number.byteValue(); } else if (klazz == short.class) { value = number.shortValue(); } else if (klazz == int.class) { value = number.intValue(); } else if (klazz == long.class) { value = number.longValue(); } if (expectedClass == boolean.class) { value = (byte) value == 1; } else if (expectedClass == char.class) { value = (char) number.shortValue(); } else if (expectedClass == short.class) { value = number.shortValue(); } else if (expectedClass == int.class) { value = number.intValue(); } else if (expectedClass == long.class) { value = number.longValue(); } else if (expectedClass == float.class) { value = Float.intBitsToFloat(number.intValue()); } else if (expectedClass == double.class) { value = Double.longBitsToDouble(number.longValue()); } return value; }
cls = wrapperToPrimitive(cls); if (cls == null) { return false;
final Class<?> pt = ClassUtils.wrapperToPrimitive(ct); if(Integer.TYPE.equals(pt)) { return toPrimitive((Integer[]) array);
@Test public void testWrapperToPrimitiveNull() { assertNull("Wrong result for null class", ClassUtils.wrapperToPrimitive(null)); } }
@Test public void testWrapperToPrimitiveNoWrapper() { assertNull("Wrong result for non wrapper class", ClassUtils.wrapperToPrimitive(String.class)); }
@Test public void testDistance() throws Exception { final Method distanceMethod = MethodUtils.getMatchingMethod(MethodUtils.class, "distance", Class[].class, Class[].class); distanceMethod.setAccessible(true); assertEquals(-1, distanceMethod.invoke(null, new Class[]{String.class}, new Class[]{Date.class})); assertEquals(0, distanceMethod.invoke(null, new Class[]{Date.class}, new Class[]{Date.class})); assertEquals(1, distanceMethod.invoke(null, new Class[]{Integer.class}, new Class[]{ClassUtils.wrapperToPrimitive(Integer.class)})); assertEquals(2, distanceMethod.invoke(null, new Class[]{Integer.class}, new Class[]{Object.class})); distanceMethod.setAccessible(false); } }
@Test public void testWrapperToPrimitive() { // an array with classes to convert final Class<?>[] primitives = { Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE }; for (final Class<?> primitive : primitives) { final Class<?> wrapperCls = ClassUtils.primitiveToWrapper(primitive); assertFalse("Still primitive", wrapperCls.isPrimitive()); assertEquals(wrapperCls + " -> " + primitive, primitive, ClassUtils.wrapperToPrimitive(wrapperCls)); } }
@Test public void testWrappersToPrimitives() { // an array with classes to test final Class<?>[] classes = { Boolean.class, Byte.class, Character.class, Short.class, Integer.class, Long.class, Float.class, Double.class, String.class, ClassUtils.class, null }; final Class<?>[] primitives = ClassUtils.wrappersToPrimitives(classes); // now test the result assertEquals("Wrong length of result array", classes.length, primitives.length); for (int i = 0; i < classes.length; i++) { final Class<?> expectedPrimitive = ClassUtils.wrapperToPrimitive(classes[i]); assertEquals(classes[i] + " -> " + expectedPrimitive, expectedPrimitive, primitives[i]); } }
/** * Checks whether the specified object is a scalar value. This method is * called by {@code getList()} and {@code getStringArray()} if the * property requested is not a string, a list, or an array. If it returns * <b>true</b>, the calling method transforms the value to a string and * returns a list or an array with this single element. This implementation * returns <b>true</b> if the value is of a wrapper type for a primitive * type. * * @param value the value to be checked * @return a flag whether the value is a scalar * @since 1.7 */ protected boolean isScalarValue(final Object value) { return ClassUtils.wrapperToPrimitive(value.getClass()) != null; }
static String shortClassName(Class<?> type) { Package pkg = type.getPackage(); if (ClassUtils.isPrimitiveOrWrapper(type)) { if (!type.isPrimitive()) { type = ClassUtils.wrapperToPrimitive(type); } return type.getName(); } else if (pkg == null || pkg.equals(Package.getPackage("java.lang"))) { return type.getSimpleName(); } else { String[] words = type.getName().split(" "); String fullClassName = words[words.length - 1]; String[] path = fullClassName.split("\\."); int i = 0; while (i + 1 < path.length && !Character.isUpperCase(path[i + 1].charAt(0))) { path[i] = path[i].substring(0, 1); i++; } return StringUtils.join(path, "."); } }
@Override public String toString() { String tname; if (ClassUtils.isPrimitiveWrapper(getRawType())) { tname = ClassUtils.wrapperToPrimitive(getRawType()).getName(); } else { tname = type.toString(); } return "TypedName[" + name + ": " + tname + "]"; }
/** * Whether a property cannot be represented by a resource but must stem * from a value map representing the properties of a resource. */ private static boolean isPropertyType(Class<?> type) { return type.isPrimitive() || type == String.class || type == Date.class || type == Calendar.class || ClassUtils.wrapperToPrimitive(type) != null; }
boolean isPrimitive(Object value) { if (value instanceof String) { return true; } if (value instanceof Number) { return true; } Class clazz = value.getClass(); return clazz.isPrimitive() || ClassUtils.wrapperToPrimitive(clazz) != null; }
/** * Gets the number of steps required to promote a primitive number to another * type. * @param srcClass the (primitive) source class * @param destClass the (primitive) destination class * @return The cost of promoting the primitive */ private static float getPrimitivePromotionCost(final Class<?> srcClass, final Class<?> destClass) { float cost = 0.0f; Class<?> cls = srcClass; if (!cls.isPrimitive()) { // slight unwrapping penalty cost += 0.1f; cls = ClassUtils.wrapperToPrimitive(cls); } for (int i = 0; cls != destClass && i < ORDERED_PRIMITIVE_TYPES.length; i++) { if (cls == ORDERED_PRIMITIVE_TYPES[i]) { cost += 0.1f; if (i < ORDERED_PRIMITIVE_TYPES.length - 1) { cls = ORDERED_PRIMITIVE_TYPES[i + 1]; } } } return cost; }
static String shortClassName(Class<?> type) { if (ClassUtils.isPrimitiveOrWrapper(type)) { if (!type.isPrimitive()) { type = ClassUtils.wrapperToPrimitive(type); } return type.getName(); } else if (type.getPackage().equals(Package.getPackage("java.lang"))) { return type.getSimpleName(); } else { String[] words = type.getName().split(" "); String fullClassName = words[words.length - 1]; String[] path = fullClassName.split("\\."); int i = 0; while (!Character.isUpperCase(path[i + 1].charAt(0))) { path[i] = path[i].substring(0, 1); i++; } return StringUtils.join(path, "."); } }
private static String toJson(Object value) { if (value == null) { return "\"\""; } if (!value.getClass().isArray() && (value.getClass().isPrimitive() || wrapperToPrimitive(value.getClass()) != null)) { return Objects.toString(value); } if (value instanceof String) { return '"' + ((String) value).replaceAll("\"", "\\\\\"") + '"'; } if (value instanceof Collection) { return toJson((Collection) value); } if (value instanceof Map) { return toJson((Map<?, ?>) value); } throw new IllegalArgumentException("Cannot convert value " + value + " to JSON."); }
private String getRepresentationAsText(Object value) { Class<? extends Object> clazz = value.getClass(); boolean isPrimitiveOrWrapped = clazz.isPrimitive() || ClassUtils.wrapperToPrimitive(clazz) != null; if (isPrimitiveOrWrapped || clazz == String.class) { return String.valueOf(value); } String representationAsText = null; try { Representation representation = clientResource.getApplication() .getConverterService().toRepresentation(value); representationAsText = representation.getText(); } catch (IOException e) { throw new WebApplicationException(e); } return representationAsText; }
@Override public String toString() { String tname; if (ClassUtils.isPrimitiveWrapper(getRawType())) { tname = ClassUtils.wrapperToPrimitive(getRawType()).getName(); } else { tname = type.toString(); } return "TypedName[" + name + ": " + tname + "]"; }