本文整理汇总了Java中org.apache.commons.lang.ClassUtils.wrapperToPrimitive方法的典型用法代码示例。如果您正苦于以下问题:Java ClassUtils.wrapperToPrimitive方法的具体用法?Java ClassUtils.wrapperToPrimitive怎么用?Java ClassUtils.wrapperToPrimitive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.lang.ClassUtils
的用法示例。
在下文中一共展示了ClassUtils.wrapperToPrimitive方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPrimitivePromotionCost
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
/**
* Get 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;
}
示例2: setFieldFromObject
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
/**
* Set a field of an object with a value, also if the field is of primitive type.
*
* Apart from this, it mimics behavior from java.reflection.Field.set(Object, Object).
*/
public static void setFieldFromObject(Object object, Field field, Object value) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (!field.getType().isPrimitive()) {
field.set(object, value);
return;
}
Class<?> primitiveClass = value.getClass();
if (!primitiveClass.isPrimitive()) {
primitiveClass = ClassUtils.wrapperToPrimitive(primitiveClass);
if (primitiveClass == null) {
throw new IllegalArgumentException(field + " is a primitive field but the value is neither primitive nor wrapper.");
}
}
String assignerName = "set" + WordUtils.capitalize(primitiveClass.getSimpleName());
Method assigner = Field.class.getMethod(assignerName, new Class<?>[] {Object.class, primitiveClass});
assigner.invoke(field, new Object[] {object, value});
}
示例3: getPrimitivePromotionCost
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
/**
* Get 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
*/
@SuppressWarnings("rawtypes")
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;
}
示例4: toString
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
/**
* Gets a custom string representation based on the parameter (0 argument) methodName.
*
* @param obj
* @param klass
* @param methodName
* @return
*/
public static String toString(final Object obj, final Class<?> klass, String methodName)
throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
if (StringUtils.isBlank(methodName)) {
methodName = "toString";
}
boolean isPrimitiveOrWrapped =
obj.getClass().isPrimitive() || ClassUtils.wrapperToPrimitive(obj.getClass()) != null;
if (isPrimitiveOrWrapped) {
return String.valueOf(obj);
} else if (Date.class.equals(klass)) {
return ((Date) obj).toString();
} else if (Calendar.class.equals(klass)) {
return ((Calendar) obj).getTime().toString();
} else if(isArray(obj)) {
return toStringFromArray(obj);
} else {
Method method = klass.getMethod(methodName);
return (String) method.invoke(obj);
}
}
示例5: isSimpleProperty
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
public static Boolean isSimpleProperty(Class<?> clazz) {
if (clazz.isPrimitive()) {
return true;
} else if (ClassUtils.wrapperToPrimitive(clazz) != null) {
return true;
} else if (String.class.isAssignableFrom(clazz)) {
return true;
} else if (Date.class.isAssignableFrom(clazz)) {
return true;
} else if (BigDecimal.class.isAssignableFrom(clazz)) {
return true;
}
return false;
}
示例6: isScalarValue
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
public static boolean isScalarValue (Object value)
{
return ClassUtils.wrapperToPrimitive (value.getClass ()) != null;
}
示例7: getTypeCode
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
private static char getTypeCode(Object wrapper) {
Class primitiveType = ClassUtils.wrapperToPrimitive(wrapper.getClass());
ObjectStreamField typeCodeProvider = new ObjectStreamField(IRRELEVANT, primitiveType);
return typeCodeProvider.getTypeCode();
}
示例8: isScalarValue
import org.apache.commons.lang.ClassUtils; //导入方法依赖的package包/类
/**
* 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
*/
private boolean isScalarValue(Object value) {
return ClassUtils.wrapperToPrimitive(value.getClass()) != null;
}