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

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

Introduction

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

Prototype

public static Class<?> wrapperToPrimitive(final 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.evosuite.testcase.AssignmentStatement.java

/** {@inheritDoc} */
@Override/*  w w w . j a va 2s  . c o  m*/
public boolean mutate(TestCase test, TestFactory factory) {
    assert (isValid());

    // Either mutate parameter, or source
    if (Randomness.nextDouble() < 0.5) {
        Set<VariableReference> objects = getSourceReplacements();
        objects.remove(retval);
        objects.remove(parameter);

        if (!objects.isEmpty()) {
            VariableReference newRetVal = Randomness.choice(objects);
            // Need to double check, because we might try to replace e.g.
            // a long with an int, which is assignable
            // but if the long is assigned to a Long field, then it is not!
            if (parameter.isAssignableTo(newRetVal)) {

                // Need to check array status because commons lang
                // is sometimes confused about what is assignable
                if (parameter.isArray() == newRetVal.isArray()) {
                    retval = newRetVal;
                    assert (isValid());
                    return true;
                }
            }
        }

    } else {

        List<VariableReference> objects = test.getObjects(parameter.getType(), retval.getStPosition());
        objects.remove(retval);
        objects.remove(parameter);
        if (!objects.isEmpty()) {
            VariableReference choice = Randomness.choice(objects);
            if (choice.isAssignableTo(retval)) {

                // Need special care if it is a wrapper class
                if (retval.getGenericClass().isWrapperType()) {
                    Class<?> rawClass = ClassUtils.wrapperToPrimitive(retval.getVariableClass());
                    if (!retval.getVariableClass().equals(rawClass)
                            && !retval.getVariableClass().equals(choice.getVariableClass())) {
                        return false;
                    }
                }

                parameter = choice;
                assert (isValid());

                return true;
            }
        }
    }

    return false;
}

From source file:org.evosuite.testcase.TestFactory.java

/**
 * Assign a value to an array index//from ww  w . ja  v a  2  s .  c  om
 *
 * @param test
 * @param array
 * @param arrayIndex
 * @param position
 * @throws ConstructionFailedException
 */
public void assignArray(TestCase test, VariableReference array, int arrayIndex, int position)
        throws ConstructionFailedException {
    List<VariableReference> objects = test.getObjects(array.getComponentType(), position);
    Iterator<VariableReference> iterator = objects.iterator();
    GenericClass componentClass = new GenericClass(array.getComponentType());
    // Remove assignments from the same array
    while (iterator.hasNext()) {
        VariableReference var = iterator.next();
        if (var instanceof ArrayIndex) {
            if (((ArrayIndex) var).getArray().equals(array))
                iterator.remove();
            // Do not assign values of same type as array to elements
            // This may e.g. happen if we have Object[], we could otherwise assign Object[] as values
            else if (((ArrayIndex) var).getArray().getType().equals(array.getType()))
                iterator.remove();
        }
        if (componentClass.isWrapperType()) {
            Class<?> rawClass = ClassUtils.wrapperToPrimitive(componentClass.getRawClass());
            if (!var.getVariableClass().equals(rawClass)
                    && !var.getVariableClass().equals(componentClass.getRawClass())) {
                iterator.remove();
            }
        }

    }
    logger.debug("Reusable objects: " + objects);
    assignArray(test, array, arrayIndex, position, objects);
}

From source file:org.grouplens.lenskit.eval.graph.ComponentNodeBuilder.java

static String shortClassName(Class<?> type) {
    if (ClassUtils.isPrimitiveOrWrapper(type)) {
        if (!type.isPrimitive()) {
            type = ClassUtils.wrapperToPrimitive(type);
        }//  w  ww .j a va 2  s  .  c  o m
        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, ".");
    }
}

From source file:org.lenskit.data.entities.TypedName.java

@Override
public String toString() {
    String tname;//from  ww w  .j  av a  2 s . c om
    if (ClassUtils.isPrimitiveWrapper(type)) {
        tname = ClassUtils.wrapperToPrimitive(type).getName();
    } else {
        tname = type.getCanonicalName();
    }
    return "TypedName[" + name + ": " + tname + "]";
}

From source file:org.tango.server.attribute.ReflectAttributeBehavior.java

private Class<?> getInputClass(final AttributeValue value) {
    Class<?> input = value.getValue().getClass();
    if (Number.class.isAssignableFrom(input) || Boolean.class.isAssignableFrom(input)) {
        input = ClassUtils.wrapperToPrimitive(value.getValue().getClass());
    }//w  ww.  j  a  v a 2  s. co  m
    return input;
}

From source file:org.tango.server.attribute.ReflectAttributeBehavior.java

private Class<?> getParamSetter() {
    Class<?> paramSetter = setter.getParameterTypes()[0];
    if (Number.class.isAssignableFrom(paramSetter) || Boolean.class.isAssignableFrom(paramSetter)) {
        paramSetter = ClassUtils.wrapperToPrimitive(setter.getParameterTypes()[0]);
    }//from w  w  w  .  j ava  2  s  . com
    return paramSetter;
}

From source file:org.tango.server.command.ReflectCommandBehavior.java

private void checkInputType(final Object arg) throws DevFailed {
    final Class<?>[] paramType = executeMethod.getParameterTypes();
    if (paramType.length > 1) {
        DevFailedUtils.throwDevFailed("INIT_FAILED", "Command can have only one parameter");
    }/*from   w w  w.j  a v a 2  s . c  o  m*/
    Class<?> paramMethod = paramType[0];
    if (Number.class.isAssignableFrom(paramMethod) || Boolean.class.isAssignableFrom(paramMethod)) {
        paramMethod = ClassUtils.wrapperToPrimitive(paramMethod);
    }
    Class<?> input = arg.getClass();
    if (Number.class.isAssignableFrom(input) || Boolean.class.isAssignableFrom(input)) {
        input = ClassUtils.wrapperToPrimitive(arg.getClass());
    }
    if (!paramMethod.isAssignableFrom(input)) {
        DevFailedUtils.throwDevFailed("TYPE_ERROR", "type mismatch, expected was "
                + paramMethod.getCanonicalName() + ", input is " + arg.getClass().getCanonicalName());
    }
}

From source file:therian.operator.convert.ELCoercionConverter.java

@Override
public boolean supports(TherianContext context, Convert<?, ?> convert) {
    if (!super.supports(context, convert)) {
        return false;
    }/* w  ww.  j  a v a2  s. c o m*/
    final Class<?> rawTargetType = getRawTargetType(convert);
    final Class<?> useTargetType = ObjectUtils.defaultIfNull(ClassUtils.primitiveToWrapper(rawTargetType),
            rawTargetType);

    // per UEL spec v2.2 section 1.18:
    if (String.class.equals(useTargetType)) {
        return true;
    }
    final Object source = convert.getSourcePosition().getValue();

    if (BigDecimal.class.equals(useTargetType) || BigInteger.class.equals(useTargetType)
            || Number.class.isAssignableFrom(useTargetType)
                    && ClassUtils.wrapperToPrimitive(useTargetType) != null) {
        return source == null || source instanceof String || source instanceof Character
                || source instanceof Number;
    }
    if (Character.class.equals(useTargetType)) {
        return source == null || source instanceof String || source instanceof Number;
    }
    if (Boolean.class.equals(useTargetType)) {
        return source == null || source instanceof String;
    }
    if (Enum.class.isAssignableFrom(useTargetType)) {
        return source == null || source instanceof String;
    }
    return source == null || "".equals(source)
            || source instanceof String && PropertyEditorManager.findEditor(useTargetType) != null;
}

From source file:therian.operator.convert.EnumToNumberConverter.java

@Override
public boolean supports(TherianContext context, Convert<? extends Enum<?>, ?> operation) {
    if (!TypeUtils.isAssignable(operation.getSourcePosition().getType(), Enum.class)) {
        return false;
    }/*from   www.j  av a  2 s . c o  m*/
    final Type targetType = operation.getTargetPosition().getType();
    if (targetType instanceof Class == false) {
        return false;
    }
    final Class<?> targetClass = (Class<?>) targetType;
    return Number.class.isAssignableFrom(targetClass) && ClassUtils.wrapperToPrimitive(targetClass) != null;
}

From source file:therian.operator.DefaultImmutableChecker.java

@Override
protected boolean isImmutable(Object object) {
    if (object == null) {
        return true;
    }/*from   w  w  w  .  j a v  a 2s .c  o  m*/
    if (object instanceof String) {
        return true;
    }
    if (object instanceof Enum) {
        return true;
    }
    if (object instanceof Annotation) {
        return true;
    }
    Class<?> cls = object.getClass();
    if (cls.isPrimitive()) {
        return true;
    }
    if (ClassUtils.wrapperToPrimitive(cls) != null) {
        return true;
    }
    return false;
}