/** * <p>Converts the specified array of primitive Class objects to an array of * its corresponding wrapper Class objects.</p> * * @param classes the class array to convert, may be null or empty * @return an array which contains for each given class, the wrapper class or * the original class if class is not a primitive. {@code null} if null input. * Empty array if an empty array passed in. * @since 2.1 */ public static Class<?>[] primitivesToWrappers(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] = primitiveToWrapper(classes[i]); } return convertedClasses; }
final int varArgLength = args.length - methodParameterTypes.length + 1; Object varArgsArray = Array.newInstance(ClassUtils.primitiveToWrapper(varArgComponentType), varArgLength);
/** * Checks if classA or classB can be auto boxed by the JVM * * @return {@code true}, if both classes are either primitive or wrapper classes and * autoboxing is possible between {@code classA} and {@code classB} */ private static boolean canBeAutoboxed(Class<?> classA, Class<?> classB) { return ClassUtils.isPrimitiveOrWrapper(classA) && ClassUtils.isPrimitiveOrWrapper(classB) && (classB.equals(classA) // Same types || ClassUtils.primitiveToWrapper(classB).equals(classA) // Matching primitive-wrapper pair, e.g. double - Double || ClassUtils.primitiveToWrapper(classA).equals(classB)); // Matching wrapper-primitive pair, e.g. Long - long }
/** * Coerce arguments in <em>args</em> into the appropriate types (if possible) for the parameter arguments * to <em>accessor</em>. * @return array of coerced values if successful, null otherwise * @throws Exception if unable to coerce */ private List<Object> coerceArguments(Method accessor, List<String> args) { final List<Object> coerced = new ArrayList<Object>(); for (int i = 0; i < accessor.getParameterTypes().length; i++) { Class<?> toType = ClassUtils.primitiveToWrapper(accessor.getParameterTypes()[i]); try { final Constructor<?> ctor = toType.getConstructor(String.class); final Object coercedArgument = ctor.newInstance(args.get(i)); coerced.add(coercedArgument); } catch (Exception e) { log.fine("Unable to coerce " + args.get(i) + " to " + toType.getSimpleName() + " via " + toType.getSimpleName() + "(String) constructor."); return null; } } return coerced; }
Class<?> fieldTypeWrapper = ClassUtils.primitiveToWrapper(f.getType());
cls = primitiveToWrapper(cls); if (cls == null) { return false;
cls = ClassUtils.primitiveToWrapper(cls);
final Class<?>[] methodParameterTypes = bestMatch.getParameterTypes(); final Class<?> methodParameterComponentType = methodParameterTypes[methodParameterTypes.length - 1].getComponentType(); final String methodParameterComponentTypeName = ClassUtils.primitiveToWrapper(methodParameterComponentType).getName(); final String parameterTypeName = parameterTypes[parameterTypes.length - 1].getName(); final String parameterTypeSuperClassName = parameterTypes[parameterTypes.length - 1].getSuperclass().getName();
public Class getCustomConverter(Class<?> srcClass, Class<?> destClass, Cache converterTypeCache) { if (converters.isEmpty()) { return null; } // Check cache first final Object cacheKey = CacheKeyFactory.createKey(destClass, srcClass); if (converterTypeCache.containsKey(cacheKey)) { // even null return (Class)converterTypeCache.get(cacheKey); } // Let's see if the incoming class is a primitive: final Class src = ClassUtils.primitiveToWrapper(srcClass); final Class dest = ClassUtils.primitiveToWrapper(destClass); Class appropriateConverter = findConverter(src, dest); converterTypeCache.put(cacheKey, appropriateConverter); return appropriateConverter; }
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) { Class<?> wrappedDestinationClass = ClassUtils.primitiveToWrapper(destinationClass); Class<?> wrappedSourceClass = ClassUtils.primitiveToWrapper(sourceClass); if (prototypeA.equals(wrappedDestinationClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else if (prototypeB.equals(wrappedDestinationClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeA.equals(wrappedSourceClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeB.equals(wrappedSourceClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else if (prototypeA.isAssignableFrom(wrappedDestinationClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else if (prototypeB.isAssignableFrom(wrappedDestinationClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeA.isAssignableFrom(wrappedSourceClass)) { return convertTo((A)sourceFieldValue, (B)existingDestinationFieldValue); } else if (prototypeB.isAssignableFrom(wrappedSourceClass)) { return convertFrom((B)sourceFieldValue, (A)existingDestinationFieldValue); } else { throw new MappingException("Destination Type (" + wrappedDestinationClass.getName() + ") is not accepted by this Custom Converter (" + this.getClass().getName() + ")!"); } }
Boolean.class, ClassUtils.primitiveToWrapper(Boolean.TYPE)); assertEquals("byte -> Byte.class", Byte.class, ClassUtils.primitiveToWrapper(Byte.TYPE)); assertEquals("char -> Character.class", Character.class, ClassUtils.primitiveToWrapper(Character.TYPE)); assertEquals("short -> Short.class", Short.class, ClassUtils.primitiveToWrapper(Short.TYPE)); assertEquals("int -> Integer.class", Integer.class, ClassUtils.primitiveToWrapper(Integer.TYPE)); assertEquals("long -> Long.class", Long.class, ClassUtils.primitiveToWrapper(Long.TYPE)); assertEquals("double -> Double.class", Double.class, ClassUtils.primitiveToWrapper(Double.TYPE)); assertEquals("float -> Float.class", Float.class, ClassUtils.primitiveToWrapper(Float.TYPE)); String.class, ClassUtils.primitiveToWrapper(String.class)); assertEquals("ClassUtils.class -> ClassUtils.class", org.apache.commons.lang3.ClassUtils.class, ClassUtils.primitiveToWrapper(org.apache.commons.lang3.ClassUtils.class)); assertEquals("Void.TYPE -> Void.TYPE", Void.TYPE, ClassUtils.primitiveToWrapper(Void.TYPE)); ClassUtils.primitiveToWrapper(null));
@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)); } }
private Converter getPrimitiveOrWrapperConverter(Class destClass, DateFormatContainer dateFormatContainer, String destFieldName, Object destObj) { if (String.class.equals(destClass)) { return new StringConverter(dateFormatContainer); } Converter result = CONVERTER_MAP.get(ClassUtils.primitiveToWrapper(destClass)); if (result == null) { if (java.util.Date.class.isAssignableFrom(destClass)) { result = new DateConverter(dateFormatContainer.getDateFormat()); } else if (Calendar.class.isAssignableFrom(destClass)) { result = new CalendarConverter(dateFormatContainer.getDateFormat()); } else if (XMLGregorianCalendar.class.isAssignableFrom(destClass)) { result = new XMLGregorianCalendarConverter(dateFormatContainer.getDateFormat()); } else if (MappingUtils.isEnumType(destClass)) { result = new EnumConverter(); } else if (JAXBElement.class.isAssignableFrom(destClass) && destFieldName != null) { result = new JAXBElementConverter(destObj.getClass().getCanonicalName(), destFieldName, dateFormatContainer.getDateFormat(), beanContainer); } else if (isLocalTime(destClass)) { result = new LocalDateTimeConverter(dateFormatContainer.getDateTimeFormatter()); } else if (isOffsetTime(destClass)) { result = new OffsetDateTimeConverter(dateFormatContainer.getDateTimeFormatter()); } else if (ZonedDateTime.class.isAssignableFrom(destClass)) { result = new ZonedDateTimeConverter(dateFormatContainer.getDateTimeFormatter()); } } return result == null ? new StringConstructorConverter(dateFormatContainer) : result; }
final Class<?> expectedWrapper = ClassUtils.primitiveToWrapper(primitive);
@Override public <T> T get(Class<T> type, String name) { if (type.isPrimitive()) { type = (Class<T>) ClassUtils.primitiveToWrapper(type); } T result = (T) _properties.get(name); if (result == null) { return null; } else { return (type.isAssignableFrom(result.getClass())) ? result : null; } }
/** * Create a typed name object. * * @param name The name. * @param type The type. * @return An object encapsulating the specified name and type. */ @SuppressWarnings("unchecked") @Nonnull public static <T> TypedName<T> create(String name, Class<T> type) { Preconditions.checkNotNull(name, "name"); Preconditions.checkNotNull(type, "type"); if (type.isPrimitive()) { type = (Class<T>) ClassUtils.primitiveToWrapper(type); } TypedName<T> attribute = new TypedName<>(name.intern(), TypeToken.of(type)); return (TypedName<T>) FIELD_CACHE.intern(NAME_EQUIVALENCE.wrap(attribute)).get(); }
.equals(ClassUtils.primitiveToWrapper(elemClass))) final Class<?> targetClass = ClassUtils.primitiveToWrapper(elemClass); final Object array = Array.newInstance(elemClass, values.size()); int idx = 0;
/** * Create a typed name object. * * @param name The name. * @param type The type. * @return An object encapsulating the specified name and type. */ @SuppressWarnings("unchecked") @Nonnull public static <T> TypedName<T> create(String name, TypeToken<T> type) { Preconditions.checkNotNull(name, "name"); Preconditions.checkNotNull(type, "type"); if (type.isPrimitive()) { type = TypeToken.of((Class<T>) ClassUtils.primitiveToWrapper(type.getRawType())); } TypedName<T> attribute = new TypedName<>(name.intern(), type); return (TypedName<T>) FIELD_CACHE.intern(NAME_EQUIVALENCE.wrap(attribute)).get(); }
private void createSetters() { Field[] fields = outClass.getDeclaredFields(); setters = new HashMap<>(); for (Field field : fields) { Class outputField = ClassUtils.primitiveToWrapper(field.getType()); String fieldName = field.getName(); setters.put(fieldName,PojoUtils.createSetter(outClass,fieldName,outputField)); } }
private PojoUtils.Getter generateGetterForKeyField() throws NoSuchFieldException, SecurityException { Field f = pojoClass.getDeclaredField(keyField); Class c = ClassUtils.primitiveToWrapper(f.getType()); PojoUtils.Getter classGetter = PojoUtils.createGetter(pojoClass, keyField, c); return classGetter; }