org.apache.commons.beanutils.converters.StringConverter Java Examples

The following examples show how to use org.apache.commons.beanutils.converters.StringConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Injector.java    From elepy with Apache License 2.0 6 votes vote down vote up
private Object getProp(Class<?> returnType, Property annotation) {

        final Class<?> primitiveWrapper = ClassUtils.primitiveToWrapper(returnType);
        StringConverter stringConverter = new StringConverter();

        final var configuration = elepyContext.getDependency(Configuration.class);

        try {
            final Object o = configuration.get(primitiveWrapper, annotation.key());

            if (o != null) {
                return o;
            }

            if (isEmpty(annotation.defaultValue())) {
                return null;
            }

            return stringConverter.convert(primitiveWrapper, annotation.defaultValue());
        } catch (ConversionException e) {
            logger.error(e.getMessage(), e);
            return null;
        }

    }
 
Example #2
Source File: FieldHolder.java    From bluetooth-gatt-parser with Apache License 2.0 5 votes vote down vote up
private AbstractConverter getConverter() {
    FieldFormat fieldFormat = field.getFormat();
    int size = fieldFormat.getSize();
    switch (fieldFormat.getType()) {
        case BOOLEAN: return new BooleanConverter();
        case UINT:
            if (size < 32) {
                return new IntegerConverter();
            } else if (size < 64) {
                return new LongConverter();
            } else {
                return new BigIntegerConverter();
            }
        case SINT:
            if (size <= 32) {
                return new IntegerConverter();
            } else if (size <= 64) {
                return new LongConverter();
            } else {
                return new BigIntegerConverter();
            }
        case FLOAT_IEE754:
        case FLOAT_IEE11073: return size <= 32 ? new FloatConverter() : new DoubleConverter();
        case UTF8S:
        case UTF16S: return new StringConverter();
        default:
            throw new IllegalStateException("Unsupported field format: " + fieldFormat.getType());
    }
}
 
Example #3
Source File: ConvertUtil.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Register standard default null.
 * 
 * @see ConvertUtilsBean#registerPrimitives(boolean) registerPrimitives(boolean throwException)
 * @see ConvertUtilsBean#registerStandard(boolean,boolean) registerStandard(boolean throwException, boolean defaultNull)
 * @see ConvertUtilsBean#registerOther(boolean) registerOther(boolean throwException)
 * @see ConvertUtilsBean#registerArrays(boolean,int) registerArrays(boolean throwException, int defaultArraySize)
 * @see ConvertUtilsBean#deregister(Class) ConvertUtilsBean.deregister(Class)
 * @since 1.11.2
 */
public static void registerStandardDefaultNull(){
    ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
    ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
    ConvertUtils.register(new BooleanConverter(null), Boolean.class);
    ConvertUtils.register(new ByteConverter(null), Byte.class);
    ConvertUtils.register(new CharacterConverter(null), Character.class);
    ConvertUtils.register(new DoubleConverter(null), Double.class);
    ConvertUtils.register(new FloatConverter(null), Float.class);
    ConvertUtils.register(new IntegerConverter(null), Integer.class);
    ConvertUtils.register(new LongConverter(null), Long.class);
    ConvertUtils.register(new ShortConverter(null), Short.class);
    ConvertUtils.register(new StringConverter(null), String.class);
}
 
Example #4
Source File: ToAliasBeanTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test read properties to alias bean1.
 */
@Test
public void testToAliasBean1(){
    ArrayConverter arrayConverter = new ArrayConverter(String[].class, new StringConverter(), 2);
    char[] allowedChars = { ':' };
    arrayConverter.setAllowedChars(allowedChars);

    ConvertUtils.register(arrayConverter, String[].class);

    DangaMemCachedConfig dangaMemCachedConfig = toAliasBean(getResourceBundle("messages.memcached"), DangaMemCachedConfig.class);
    assertThat(
                    dangaMemCachedConfig,
                    allOf(//
                                    hasProperty("serverList", arrayContaining("172.20.3-1.23:11211", "172.20.31.22:11211")),
                                    hasProperty("poolName", is("sidsock2")),
                                    hasProperty("expireTime", is(180)),
                                    hasProperty("weight", arrayContaining(2)),
                                    hasProperty("initConnection", is(10)),
                                    hasProperty("minConnection", is(5)),
                                    hasProperty("maxConnection", is(250)),
                                    hasProperty("maintSleep", is(30)),
                                    hasProperty("nagle", is(false)),
                                    hasProperty("socketTo", is(3000)),
                                    hasProperty("aliveCheck", is(false))
                    //
                    ));
}
 
Example #5
Source File: FieldHolder.java    From bluetooth-gatt-parser with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a String representation of the field or a default value in case if the field cannot
 * be converted to a String.
 * @param def the default value to be returned if an error occurs converting the field
 * @return a String representation of the field
 */
public String getString(String def) {
    return new StringConverter(def).convert(String.class, prepareValue());
}