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

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

Introduction

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

Prototype

public static Class<?> primitiveToWrapper(final Class<?> cls) 

Source Link

Document

Converts the specified primitive Class object to its corresponding wrapper Class object.

NOTE: From v2.2, this method handles Void.TYPE , returning Void.TYPE .

Usage

From source file:org.vaadin.viritin.ListContainer.java

public static Class<?> getNestedPropertyType(DynaClass bean, String name) throws IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
    if (bean == null) {
        // The type is not properly initilized yet, just leave it as generic
        // Object
        return Object.class;
    }/* w  ww  . j a va 2  s.  c om*/
    // Resolve nested references
    while (resolver.hasNested(name)) {
        String next = resolver.next(name);
        if (resolver.isIndexed(next) || resolver.isMapped(next)) {
            String property = resolver.getProperty(next);
            Class<?> clazz = Class.forName(bean.getName());
            Class<?> detectTypeParameter = detectTypeParameter(clazz, property,
                    resolver.isIndexed(name) ? 0 : 1);
            bean = WrapDynaClass.createDynaClass(detectTypeParameter);
            return getNestedPropertyType(bean, resolver.remove(name));
        }
        DynaProperty db = bean.getDynaProperty(next);
        bean = WrapDynaClass.createDynaClass(db.getType());
        name = resolver.remove(name);
    }
    if (resolver.isMapped(name) || resolver.isIndexed(name)) {
        String property = resolver.getProperty(name);
        Class<?> clazz = Class.forName(bean.getName());
        return detectTypeParameter(clazz, property, resolver.isIndexed(name) ? 0 : 1);
    }
    Class<?> type;
    DynaProperty dynaProperty = bean.getDynaProperty(name);
    if (dynaProperty != null) {
        type = dynaProperty.getType();
    } else { //happens for default methods
        Method method = obtainGetterOfProperty(bean, name);
        type = method.getReturnType();
    }
    if (type.isPrimitive() == true) {
        // Vaadin can't handle primitive types in _all_ places, so use
        // wrappers instead. FieldGroup works, but e.g. Table in _editable_
        // mode fails for some reason
        return ClassUtils.primitiveToWrapper(type);
    }
    return type;
}

From source file:spoon.processing.ProcessorPropertiesImpl.java

public <T> T get(Class<T> type, String name) {
    if (type.isPrimitive()) {
        type = (Class<T>) ClassUtils.primitiveToWrapper(type);
    }/* w  ww .j  a va  2  s.  c o m*/
    T result = (T) _properties.get(name);
    if (result == null) {
        return null;
    } else {
        return (type.isAssignableFrom(result.getClass())) ? result : null;
    }
}

From source file:tech.sirwellington.alchemy.generator.ObjectGenerators.java

private static void injectField(Object pojo, Field field, Map<Class<?>, AlchemyGenerator<?>> generatorMappings)
        throws IllegalArgumentException, IllegalAccessException {
    Class<?> typeOfField = field.getType();
    typeOfField = ClassUtils.primitiveToWrapper(typeOfField);

    AlchemyGenerator<?> generator = determineGeneratorFor(typeOfField, field, generatorMappings);

    if (generator == null) {
        LOG.warn("Could not find a suitable AlchemyGenerator for field {} with type {}", field, typeOfField);
        return;//from w ww. java  2s.c o  m
    }

    Object value = generator.get();

    boolean originalAccessibility = field.isAccessible();

    try {
        field.setAccessible(true);
        field.set(pojo, value);
    } finally {
        field.setAccessible(originalAccessibility);
    }
}

From source file:therian.Operation.java

/**
 * Learn whether {@code operator} seems to implement {@code this}.
 *
 * @param operator to check//from w ww .  jav  a2 s .  c o  m
 * @return boolean
 */
public boolean matches(Operator<?> operator) {
    final Type expectedType = TypeUtils.unrollVariables(
            TypeUtils.getTypeArguments(operator.getClass(), Operator.class),
            Operator.class.getTypeParameters()[0]);

    if (!TypeUtils.isInstance(this, expectedType)) {
        return false;
    }

    final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(expectedType, Operation.class);
    for (Class<?> c : ClassUtils.hierarchy(TypeUtils.getRawType(expectedType, operator.getClass()))) {
        if (c.equals(Operation.class)) {
            break;
        }
        for (TypeVariable<?> var : c.getTypeParameters()) {
            Type type = Types.resolveAt(this, var, typeArguments);
            if (type == null || typeArguments == null) {
                continue;
            }
            if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) {
                type = ClassUtils.primitiveToWrapper((Class<?>) type);
            }
            if (!TypeUtils.isAssignable(type, TypeUtils.unrollVariables(typeArguments, var))) {
                return false;
            }
        }
    }
    return true;
}

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

@SuppressWarnings("unchecked")
@Override/* w  w w. ja  v a 2  s .co m*/
public boolean perform(TherianContext context, Convert<? extends Collection, ?> convert) {
    final Type targetElementType = context.eval(GetElementType.of(convert.getTargetPosition()));
    final int size = convert.getSourcePosition().getValue().size();

    final Object result = Array.newInstance(TypeUtils.getRawType(targetElementType, null), size);

    final Object[] toFill;
    final boolean primitiveTargetElementType = targetElementType instanceof Class<?>
            && ((Class<?>) targetElementType).isPrimitive();

    if (primitiveTargetElementType) {
        toFill = (Object[]) Array.newInstance(ClassUtils.primitiveToWrapper((Class<?>) targetElementType),
                size);
    } else {
        toFill = (Object[]) result;
    }
    convert.getSourcePosition().getValue().toArray(toFill);
    if (primitiveTargetElementType) {
        for (int i = 0; i < size; i++) {
            Array.set(result, i, toFill[i]);
        }
    }
    ((Position.Writable<Object>) convert.getTargetPosition()).setValue(result);
    return true;
}

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

@Override
public boolean perform(TherianContext context, Convert<? extends Object, ? super List> convert) {
    final Object source = convert.getSourcePosition().getValue();
    if (source == null) {
        return false;
    }//from  ww w  . jav a  2 s .c  o  m
    final List<?> list;
    if (TypeUtils.isArrayType(convert.getSourcePosition().getType())) {
        final Object[] array;
        if (source instanceof Object[]) {
            array = (Object[]) source;
        } else {
            final Class<?> primitiveType = (Class<?>) TypeUtils
                    .getArrayComponentType(convert.getSourcePosition().getType());
            final int len = Array.getLength(source);
            array = (Object[]) Array.newInstance(ClassUtils.primitiveToWrapper(primitiveType), len);
            for (int i = 0; i < len; i++) {
                array[i] = Array.get(source, i);
            }
        }
        list = Arrays.asList(array);
    } else {
        list = Collections.singletonList(convert.getSourcePosition().getValue());
    }
    convert.getTargetPosition().setValue(list);
    return true;
}

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

@Override
public boolean supports(TherianContext context, Convert<?, ?> convert) {
    if (!super.supports(context, convert)) {
        return false;
    }//  w w  w. j a va2s .  c  om
    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.ELCoercionConverter.java

public boolean supports(Convert<?, ?> operation) {
    final Class<?> rawTargetType = getRawTargetType(operation);
    final Class<?> useTargetType = ObjectUtils.defaultIfNull(ClassUtils.primitiveToWrapper(rawTargetType),
            rawTargetType);//  w  ww. j  av a 2s .  c o  m

    // per UEL spec v2.2 section 1.18:
    if (String.class.equals(useTargetType)) {
        return true;
    }
    final Object source = operation.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:yoyo.framework.standard.shared.commons.lang.ClassUtilsTest.java

@Test
public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    List<Class<?>> classes = new ArrayList<Class<?>>() {
        {/* w w  w .  j  a va2  s  .  c  o m*/
            add(Integer.class);
            add(Long.class);
        }
    };
    assertThat(ClassUtils.convertClassesToClassNames(classes).toArray(),
            is(new Object[] { "java.lang.Integer", "java.lang.Long" }));
    List<String> classNames = new ArrayList<String>() {
        {
            add("java.lang.Integer");
            add("java.lang.Long");
        }
    };
    assertThat(ClassUtils.convertClassNamesToClasses(classNames).toArray(), is(classes.toArray()));
    assertThat(ClassUtils.getAllInterfaces(String.class).toArray(), is(new Object[] {
            java.io.Serializable.class, java.lang.Comparable.class, java.lang.CharSequence.class }));
    assertThat(ClassUtils.getAllSuperclasses(HashMap.class).toArray(),
            is(new Object[] { java.util.AbstractMap.class, java.lang.Object.class }));
    assertThat(ClassUtils.getClass("java.lang.String").toString(), is("class java.lang.String"));
    assertThat(ClassUtils.getPackageCanonicalName(String.class), is("java.lang"));
    assertThat(ClassUtils.getPackageName(String.class), is("java.lang"));
    assertThat(ClassUtils.getPublicMethod(String.class, "length", new Class[] {}), is(not(nullValue())));
    assertThat(ClassUtils.getShortCanonicalName(String.class), is("String"));
    assertThat(ClassUtils.getShortClassName(String.class), is("String"));
    assertThat(ClassUtils.getSimpleName(String.class), is("String"));
    assertThat(ClassUtils.isAssignable(String.class, Object.class), is(true));
    assertThat(ClassUtils.isInnerClass(Foo.class), is(true));
    assertThat(ClassUtils.isInnerClass(String.class), is(false));
    assertThat(ClassUtils.isPrimitiveOrWrapper(Integer.class), is(true));
    assertThat(ClassUtils.isPrimitiveOrWrapper(String.class), is(false));
    assertThat(ClassUtils.isPrimitiveWrapper(Integer.class), is(true));
    assertThat(ClassUtils.isPrimitiveWrapper(String.class), is(false));
    assertThat(ClassUtils.primitivesToWrappers(new Class[] { Integer.class, Long.class }),
            is(not(nullValue())));
    assertThat(ClassUtils.primitiveToWrapper(Integer.class), is(not(nullValue())));
    assertThat(ClassUtils.toClass(1L, 2L), is(not(nullValue())));
    assertThat(ClassUtils.wrappersToPrimitives(new Class[] { Integer.class, Long.class }),
            is(not(nullValue())));
    assertThat(ClassUtils.wrapperToPrimitive(Integer.class), is(not(nullValue())));
}