Example usage for org.apache.commons.lang ClassUtils wrapperToPrimitive

List of usage examples for org.apache.commons.lang ClassUtils wrapperToPrimitive

Introduction

In this page you can find the example usage for org.apache.commons.lang ClassUtils wrapperToPrimitive.

Prototype

public static Class<?> wrapperToPrimitive(Class<?> cls) 

Source Link

Document

Converts the specified wrapper class to its corresponding primitive class.

This method is the counter part of primitiveToWrapper().

Usage

From source file:org.hibernate.internal.util.ReflectHelper.java

public static void setArchetypeValue(Locatable loc, String propertyPath, Object propertyValue,
        Archetype archetype) throws InstantiationException, IllegalAccessException {

    //      Archetype archetype = ArchetypeRepository.INSTANCE.getArchetype(loc.getArchetypeNodeId());
    if (archetype == null) {
        archetype = ArchetypeRepository.INSTANCE.getArchetype(loc.getArchetypeNodeId());
    }//from www  . ja  v a 2s .co  m

    Map<String, CObject> pathNodeMap = archetype.getPathNodeMap();
    String nodePath = ReflectHelper.getArchetypeNodePath(archetype, propertyPath);
    if (nodePath.compareTo(propertyPath) == 0) {
        loc.set(nodePath, propertyValue);
    } else {
        CObject node = pathNodeMap.get(nodePath);
        Object target = loc.itemAtPath(nodePath);
        if (target == null) {
            Class klass = ArchetypeRepository.INSTANCE.getRMBuilder().retrieveRMType(node.getRmTypeName());
            target = klass.newInstance();
        }

        String attributePath = propertyPath.substring(nodePath.length());
        String[] attributePathSegments = attributePath.split("/");
        Object tempTarget = target;
        for (String pathSegment : attributePathSegments) {
            if (!pathSegment.isEmpty()) {
                Class klass = ReflectHelper.getter(tempTarget.getClass(), pathSegment).getReturnType();
                PropertyAccessor propertyAccessor = new ChainedPropertyAccessor(new PropertyAccessor[] {
                        PropertyAccessorFactory.getPropertyAccessor(tempTarget.getClass(), null),
                        PropertyAccessorFactory.getPropertyAccessor("field") });

                Setter setter = propertyAccessor.getSetter(tempTarget.getClass(), pathSegment);
                Getter getter = propertyAccessor.getGetter(tempTarget.getClass(), pathSegment);
                if (klass.isPrimitive() || ClassUtils.wrapperToPrimitive(klass) != null
                        || String.class.isAssignableFrom(klass) || Set.class.isAssignableFrom(klass)) {
                    if (propertyValue instanceof Locatable) {
                        String uid = ((Locatable) propertyValue).getUid().getValue();
                        setter.set(tempTarget, uid, null);
                        loc.getAssociatedObjects().put(uid, propertyValue);
                    } else if (propertyValue instanceof ArchetypeProxy) {
                        LazyInitializer li = ((ArchetypeProxy) propertyValue).getHibernateLazyInitializer();
                        setter.set(tempTarget, li.getIdentifier(), null);
                    } else {
                        if (propertyValue instanceof String) {
                            if (Short.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, Short.parseShort((String) propertyValue), null);
                            }
                            if (Integer.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, Integer.parseInt((String) propertyValue), null);
                            }
                            if (Long.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, Long.parseLong((String) propertyValue), null);
                            }
                            if (Float.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, Float.parseFloat((String) propertyValue), null);
                            }
                            if (Double.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, Double.parseDouble((String) propertyValue), null);
                            }
                            if (Boolean.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, Boolean.parseBoolean((String) propertyValue), null);
                            }
                            if (DateTime.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, DvDateTimeParser.parseDateTime((String) propertyValue),
                                        null);
                            }
                            if (String.class.isAssignableFrom(klass)) {
                                setter.set(tempTarget, propertyValue, null);
                            }
                        } else {
                            setter.set(tempTarget, propertyValue, null);
                        }
                    }
                } else {
                    Object value = klass.newInstance();
                    setter.set(tempTarget, value, null);
                    tempTarget = value;
                }
            }
        }
    }
}

From source file:org.jdto.util.MemberUtils.java

/**
 * Get the number of steps required to promote a primitive number to another
 * type./*from w ww.j  ava 2s  . c om*/
 *
 * @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;
}

From source file:org.restlet.ext.jaxrs.internal.client.JaxRsClientInvocationHandler.java

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);
    }// w  w w .  j  a  v  a 2  s  . com

    String representationAsText = null;

    try {
        Representation representation = clientResource.getApplication().getConverterService()
                .toRepresentation(value);
        representationAsText = representation.getText();
    } catch (IOException e) {
        throw new WebApplicationException(e);
    }

    return representationAsText;
}

From source file:org.sglj.service.rmi.RmiUtils.java

/**
 * 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
 *//*from w  w w. j a  va 2s .  c  o  m*/
@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;
}