org.apache.commons.beanutils.Converter类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(176)

本文整理了Java中org.apache.commons.beanutils.Converter类的一些代码示例,展示了Converter类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Converter类的具体详情如下:
包路径:org.apache.commons.beanutils.Converter
类名称:Converter

Converter介绍

[英]General purpose data type converter that can be registered and used within the BeanUtils package to manage the conversion of objects from one type to another.

Converter subclasses bundled with the BeanUtils library are required to be thread-safe, as users of the library may call conversion methods from more than one thread simultaneously.

Custom converter subclasses created by users of the library can be non-thread-safe if the application using them is single-threaded. However it is recommended that they be written in a thread-safe manner anyway.
[中]可在BeanUtils包中注册和使用的通用数据类型转换器,用于管理对象从一种类型到另一种类型的转换。
与BeanUtils库绑定的转换器子类必须是线程安全的,因为库的用户可以同时从多个线程调用转换方法。
如果使用库的应用程序是单线程的,则库用户创建的自定义转换器子类可能是非线程安全的。但是,无论如何,建议以线程安全的方式编写它们。

代码示例

代码示例来源:origin: commons-beanutils/commons-beanutils

/**
 * Convert the input object into an output object of the
 * specified type by delegating to the underlying {@link Converter}
 * implementation.
 *
 * @param <T> The result type of the conversion
 * @param type Data type to which this value should be converted
 * @param value The input value to be converted
 * @return The converted value.
 */
public <T> T convert(final Class<T> type, final Object value) {
  return converter.convert(type, value);
}

代码示例来源:origin: wildfly/wildfly

/**
 * Convert the input object into an output object of the
 * specified type by delegating to the underlying {@link Converter}
 * implementation.
 *
 * @param <T> The result type of the conversion
 * @param type Data type to which this value should be converted
 * @param value The input value to be converted
 * @return The converted value.
 */
public <T> T convert(final Class<T> type, final Object value) {
  return converter.convert(type, value);
}

代码示例来源:origin: alibaba/canal

public Object convert(Class type, Object value) {
    if (value == null) {
      if (useDefault) {
        return (defaultValue);
      } else {
        throw new ConversionException("No value specified");
      }
    }

    if (value instanceof byte[]) {
      return (value);
    }

    // BLOB类型,canal直接存储为String("ISO-8859-1")
    if (value instanceof String) {
      try {
        return ((String) value).getBytes("ISO-8859-1");
      } catch (Exception e) {
        throw new ConversionException(e);
      }
    }

    return converter.convert(type, value); // byteConvertor进行转化
  }
}

代码示例来源:origin: commons-beanutils/commons-beanutils

/**
 * Convert the specified value into a String.  If the specified value
 * is an array, the first element (converted to a String) will be
 * returned.  The registered {@link Converter} for the
 * <code>java.lang.String</code> class will be used, which allows
 * applications to customize Object->String conversions (the default
 * implementation simply uses toString()).
 *
 * @param value Value to be converted (may be null)
 * @return The converted String value or null if value is null
 */
public String convert(Object value) {
  if (value == null) {
    return null;
  } else if (value.getClass().isArray()) {
    if (Array.getLength(value) < 1) {
      return (null);
    }
    value = Array.get(value, 0);
    if (value == null) {
      return null;
    } else {
      final Converter converter = lookup(String.class);
      return (converter.convert(String.class, value));
    }
  } else {
    final Converter converter = lookup(String.class);
    return (converter.convert(String.class, value));
  }
}

代码示例来源:origin: wildfly/wildfly

/**
 * Convert the specified value into a String.  If the specified value
 * is an array, the first element (converted to a String) will be
 * returned.  The registered {@link Converter} for the
 * <code>java.lang.String</code> class will be used, which allows
 * applications to customize Object->String conversions (the default
 * implementation simply uses toString()).
 *
 * @param value Value to be converted (may be null)
 * @return The converted String value or null if value is null
 */
public String convert(Object value) {
  if (value == null) {
    return null;
  } else if (value.getClass().isArray()) {
    if (Array.getLength(value) < 1) {
      return (null);
    }
    value = Array.get(value, 0);
    if (value == null) {
      return null;
    } else {
      final Converter converter = lookup(String.class);
      return (converter.convert(String.class, value));
    }
  } else {
    final Converter converter = lookup(String.class);
    return (converter.convert(String.class, value));
  }
}

代码示例来源:origin: commons-beanutils/commons-beanutils

/**
 * Convert the specified value to an object of the specified class (if
 * possible).  Otherwise, return a String representation of the value.
 *
 * @param value Value to be converted (may be null)
 * @param clazz Java class to be converted to (must not be null)
 * @return The converted value
 *
 * @throws ConversionException if thrown by an underlying Converter
 */
public Object convert(final String value, final Class<?> clazz) {
  if (log.isDebugEnabled()) {
    log.debug("Convert string '" + value + "' to class '" +
         clazz.getName() + "'");
  }
  Converter converter = lookup(clazz);
  if (converter == null) {
    converter = lookup(String.class);
  }
  if (log.isTraceEnabled()) {
    log.trace("  Using converter " + converter);
  }
  return (converter.convert(clazz, value));
}

代码示例来源:origin: wildfly/wildfly

/**
 * Convert the specified value to an object of the specified class (if
 * possible).  Otherwise, return a String representation of the value.
 *
 * @param value Value to be converted (may be null)
 * @param clazz Java class to be converted to (must not be null)
 * @return The converted value
 *
 * @throws ConversionException if thrown by an underlying Converter
 */
public Object convert(final String value, final Class<?> clazz) {
  if (log.isDebugEnabled()) {
    log.debug("Convert string '" + value + "' to class '" +
         clazz.getName() + "'");
  }
  Converter converter = lookup(clazz);
  if (converter == null) {
    converter = lookup(String.class);
  }
  if (log.isTraceEnabled()) {
    log.trace("  Using converter " + converter);
  }
  return (converter.convert(clazz, value));
}

代码示例来源:origin: pmd/pmd

@Override
public void visit(SimpleBeanModelNode node, Element parent) {
  Element nodeElement = parent.getOwnerDocument().createElement(SCHEMA_NODE_ELEMENT);
  nodeElement.setAttribute(SCHEMA_NODE_CLASS_ATTRIBUTE, node.getNodeType().getCanonicalName());
  for (Entry<String, Object> keyValue : node.getSettingsValues().entrySet()) {
    // I don't think the API is intended to be used like that
    // but ConvertUtils wouldn't use the convertToString methods
    // defined in the converters otherwise.
    // Even when a built-in converter is available, objects are
    // still converted with Object::toString which fucks up the
    // conversion...
    String value = (String) ConvertUtils.lookup(keyValue.getValue().getClass()).convert(String.class, keyValue.getValue());
    if (value == null) {
      continue;
    }
    Element setting = parent.getOwnerDocument().createElement(SCHEMA_PROPERTY_ELEMENT);
    setting.setAttribute(SCHEMA_PROPERTY_NAME, keyValue.getKey());
    setting.setAttribute(SCHEMA_PROPERTY_TYPE, node.getSettingsTypes().get(keyValue.getKey()).getCanonicalName());
    setting.appendChild(parent.getOwnerDocument().createCDATASection(value));
    nodeElement.appendChild(setting);
  }
  parent.appendChild(nodeElement);
  super.visit(node, nodeElement);
}

代码示例来源:origin: DozerMapper/dozer

public Object convert(Object srcFieldValue, Class destFieldClass, DateFormatContainer dateFormatContainer, String destFieldName, Object destObj) {
  if (srcFieldValue == null || destFieldClass == null || (srcFieldValue.equals("") && !destFieldClass.equals(String.class))) {
    return null;
  }
  Converter converter = getPrimitiveOrWrapperConverter(destFieldClass, dateFormatContainer, destFieldName, destObj);
  try {
    return converter.convert(destFieldClass, unwrapSrcFieldValue(srcFieldValue));
  } catch (org.apache.commons.beanutils.ConversionException e) {
    throw new com.github.dozermapper.core.converters.ConversionException(e);
  }
}

代码示例来源:origin: commons-beanutils/commons-beanutils

/**
 * <p>Convert the value to an object of the specified class (if
 * possible).</p>
 *
 * @param value Value to be converted (may be null)
 * @param type Class of the value to be converted to
 * @return The converted value
 *
 * @throws ConversionException if thrown by an underlying Converter
 * @since 1.8.0
 */
protected Object convert(final Object value, final Class<?> type) {
  final Converter converter = getConvertUtils().lookup(type);
  if (converter != null) {
    log.trace("        USING CONVERTER " + converter);
    return converter.convert(type, value);
  } else {
    return value;
  }
}

代码示例来源:origin: commons-beanutils/commons-beanutils

log.trace("  Using converter " + converter);
converted = converter.convert(targetType, value);
    log.trace("  Using converter " + converter);
  converted = converter.convert(String.class, converted);

代码示例来源:origin: commons-beanutils/commons-beanutils

element = elementConverter.convert(String.class, element);
if (element != null) {
  buffer.append(element);

代码示例来源:origin: commons-beanutils/commons-beanutils

Array.set(array, i, converter.convert(type, values[i]));

代码示例来源:origin: wildfly/wildfly

log.trace("  Using converter " + converter);
converted = converter.convert(targetType, value);
    log.trace("  Using converter " + converter);
  converted = converter.convert(String.class, converted);

代码示例来源:origin: wildfly/wildfly

Array.set(array, i, converter.convert(type, values[i]));

代码示例来源:origin: wildfly/wildfly

element = elementConverter.convert(String.class, element);
if (element != null) {
  buffer.append(element);

代码示例来源:origin: wildfly/wildfly

/**
 * <p>Convert the value to an object of the specified class (if
 * possible).</p>
 *
 * @param value Value to be converted (may be null)
 * @param type Class of the value to be converted to
 * @return The converted value
 *
 * @throws ConversionException if thrown by an underlying Converter
 * @since 1.8.0
 */
protected Object convert(final Object value, final Class<?> type) {
  final Converter converter = getConvertUtils().lookup(type);
  if (converter != null) {
    log.trace("        USING CONVERTER " + converter);
    return converter.convert(type, value);
  } else {
    return value;
  }
}

代码示例来源:origin: commons-beanutils/commons-beanutils

element = elementConverter.convert(componentType, element);
Array.set(newArray, i, element);

代码示例来源:origin: wildfly/wildfly

element = elementConverter.convert(componentType, element);
Array.set(newArray, i, element);

代码示例来源:origin: jenkinsci/configuration-as-code-plugin

@Nonnull
@Override
public Object configure(CNode config, ConfigurationContext context) throws ConfiguratorException {
  return Stapler.lookupConverter(target).convert(target, SecretSourceResolver.resolve(context, config.asScalar().toString()));
}

相关文章

微信公众号

最新文章

更多

Converter类方法