本文整理汇总了Java中org.apache.commons.beanutils.ConvertUtils.convert方法的典型用法代码示例。如果您正苦于以下问题:Java ConvertUtils.convert方法的具体用法?Java ConvertUtils.convert怎么用?Java ConvertUtils.convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.beanutils.ConvertUtils
的用法示例。
在下文中一共展示了ConvertUtils.convert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toValue
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
private static Object toValue(String initial, Class clazz) {
Object value;
try {
if (initial != null) {
value = ConvertUtils.convert(initial, clazz);
} else {
if (clazz.isArray()) {
value = Array.newInstance(clazz.getComponentType(), 0);
} else {
value = clazz.newInstance();
}
}
} catch (Throwable t) {
log.warn("No es pot convertir '" + initial + "' a " + clazz.getName());
value = null;
}
return (value);
}
示例2: convertToObject
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* 基于Apache BeanUtils转换字符串到相应类型.
*
* @param value 待转换的字符串.
* @param toType 转换目标类型.
*/
public static Object convertToObject(String value, Class<?> toType) {
Object cvt_value=null;
try {
cvt_value=ConvertUtils.convert(value, toType);
// if(toType==Date.class){
// System.out.println("0----0");
// SimpleDateFormat dateFormat=null;
// try{
// dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// cvt_value=dateFormat.parse(value);
// }catch(Exception e){
// dateFormat=new SimpleDateFormat("yyyy-MM-dd");
// cvt_value=dateFormat.parse(value);
// }
// }
} catch (Exception e) {
throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
}
return cvt_value;
}
示例3: getVariableMap
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
@JsonIgnore
public Map<String, Object> getVariableMap() {
ConvertUtils.register(new DateConverter(), java.util.Date.class);
if (StringUtils.isBlank(keys)) {
return map;
}
String[] arrayKey = keys.split(",");
String[] arrayValue = values.split(",");
String[] arrayType = types.split(",");
for (int i = 0; i < arrayKey.length; i++) {
String key = arrayKey[i];
String value = arrayValue[i];
String type = arrayType[i];
Class<?> targetType = Enum.valueOf(PropertyType.class, type).getValue();
Object objectValue = ConvertUtils.convert(value, targetType);
map.put(key, objectValue);
}
return map;
}
示例4: fastPopulate
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* 将Map的值装入指定的对象中
*
* @param obj 对象
* @param properties map值
* @throws java.lang.reflect.InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void fastPopulate(Object obj, Map<String, Object> properties) {
Class<?> classz = obj.getClass();
Map<String, PropertiesEntry> map = getFastBeanCache().getPropertiesEntryMap(classz);
if (map.size() > 0) {
try {
for (Map.Entry<String, Object> entry : properties.entrySet()) {
PropertiesEntry pe = map.get(entry.getKey());
if (pe != null) {
if (pe.getSet() != null) {
if (entry.getValue() != null) {
Object v = ConvertUtils.convert(entry.getValue(), pe.getType());
pe.getSet().invoke(obj, v);
} else {
entry.setValue(null);
}
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
示例5: findListByHaloView
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <X> List<X> findListByHaloView(HaloMap parameter) {
Integer begin = 0;
Integer end = null;
if (null != parameter.get(HaloDao.ADDBEGIN)) {
begin = (Integer) ConvertUtils.convert(parameter.get(HaloDao.ADDBEGIN), Integer.class);
parameter.remove(HaloDao.ADDBEGIN);
}
if (null != parameter.get(HaloDao.ADDEND)) {
end = (Integer) ConvertUtils.convert(parameter.get(HaloDao.ADDEND), Integer.class);
parameter.remove(HaloDao.ADDEND);
}
SQLQuery query = null;
if (this.entityType.getName().equals("com.ht.halo.map.HaloViewMap")) {
query = CreateMySqlQueryByHaloViewToMap(parameter);
} else {
query = CreateMySqlQueryByHaloViewToBean(parameter);
}
if (null != end) {
query.setFirstResult(begin);
query.setMaxResults(end - begin);
}
return query.list();
}
示例6: appendQueryTerm
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* Helper method to {@code expandSearchTerms}: builds up a query string with
* joining ampersands and converts the value given into a string.
*
* @param query The StringBuilder which is building up the query.
* @param argument The search parameter.
* @param value The value to search for.
*
* @throws IllegalSearchTermException if {@code value} is null.
*
* @see #expandSearchTerms(Map)
* @see ConvertUtilsBean#convert(Object)
*/
private void appendQueryTerm(StringBuilder query, String argument, Object value)
{
if (value == null)
{
// This message is sensible as find() will not call this method if it
// finds the term's immediate value is null. It will only get here with
// value == null when looping through an array or collection.
throw new IllegalSearchTermException(
argument, "Search term \"" + argument + "\" contains a null value.");
}
String strValue = ConvertUtils.convert(value);
if (query.length() > 0)
{
query.append('&');
}
query.append(argument);
query.append('=');
query.append(strValue);
}
示例7: convertGenericType
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
public static Object convertGenericType(String fieldName, Object value, FieldType type) {
if (FieldType.DATE == type) {
return convertDate(fieldName, value);
}
if (type.getClasses().length == 0)
return error(INVALID_FORMAT, fieldName);
Class<?> clz = type.getClasses()[0];
value = ConvertUtils.convert(value, clz);
if (value == null || !clz.isAssignableFrom(value.getClass())) {
return error(INVALID_FORMAT, fieldName);
}
return value;
}
示例8: getVariableMap
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
public Map<String, Object> getVariableMap() {
Map<String, Object> vars = new HashMap<String, Object>();
ConvertUtils.register(new DateConverter(), java.util.Date.class);
if (StringUtil.isBlank(keys)) {
return vars;
}
String[] arrayKey = keys.split(",");
String[] arrayValue = values.split(",");
String[] arrayType = types.split(",");
for (int i = 0; i < arrayKey.length; i++) {
String key = arrayKey[i];
String value = arrayValue[i];
String type = arrayType[i];
Class<?> targetType = Enum.valueOf(PropertyType.class, type).getValue();
Object objectValue = ConvertUtils.convert(value, targetType);
vars.put(key, objectValue);
}
return vars;
}
示例9: getActionParameterValues
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* @param parameters
* @param request
* @param values
* @return
*/
private static Object[] getActionParameterValues(List<ParameterMetaData> parameters, Map<String, Object> body, Map<String, Object> values) {
if (body != null) {
values.putAll(body);
}
Object[] params = new Object[parameters.size()];
for (int i = 0; i < parameters.size(); i++) {
ParameterMetaData param = parameters.get(i);
Object value = values.get(param.getName());
if (value != null && ! value.getClass().equals(param.getType())) {
value = ConvertUtils.convert(value, param.getType());
}
params[i] = value;
}
return params;
}
示例10: populate
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
*
* @param rs
* @param builder
* @throws SQLException
*/
private void populate(ResultSet rs, Message.Builder builder)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();// 列个数
String columnLabel = null;// 列名
Object columnValue = null;// 列值
Descriptors.FieldDescriptor fieldDescriptor = null;
for (int i = 1; i <= columnCount; i++) {
columnLabel = metaData.getColumnLabel(i);
columnValue = rs.getObject(i);
if (columnValue == null)
continue;// 如果为空,继续下一个
fieldDescriptor = descriptor.findFieldByName(columnLabel);
if (fieldDescriptor == null)
continue;// 如果为空,继续下一个
// 转换为相应的类型 ,会自动将 date 类型转换为long
if (fieldDescriptor.getType().equals(FieldDescriptor.Type.ENUM)) {
columnValue = fieldDescriptor.getEnumType().findValueByNumber(
(int) columnValue);
} else {
columnValue = ConvertUtils.convert(columnValue, fieldDescriptor
.getDefaultValue().getClass());
}
builder.setField(fieldDescriptor, columnValue);
}
}
示例11: getParameter
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* Get the first parameter value, converted to the requested type.
* @param parameters used to extract parameter value from
* @param parameterName name of the parameter
* @param returnType type of object to return
* @return the converted parameter value. Null, if the parameter has no value.
* @throws IllegalArgumentException when no conversion for the given returnType is available or if returnType is null.
* @throws InvalidArgumentException when conversion to the given type was not possible
*/
@SuppressWarnings("unchecked")
public <T extends Object> T getParameter(Parameters parameters, String parameterName, Class<T> returnType) {
if(returnType == null)
{
throw new IllegalArgumentException("ReturnType cannot be null");
}
try
{
Object result = null;
String stringValue = parameters.getParameter(parameterName);
if(stringValue != null)
{
result = ConvertUtils.convert(stringValue, returnType);
if(result instanceof String)
{
// If a string is returned, no converter has been found
throw new IllegalArgumentException("Unable to convert parameter to type: " + returnType.getName());
}
}
return (T) result;
}
catch(ConversionException ce)
{
// Conversion failed, wrap in Illegal
throw new InvalidArgumentException("Parameter value for '" + parameterName + "' should be a valid " + returnType.getSimpleName());
}
}
示例12: getProperty
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* Get the property value, converted to the requested type.
*
* @param propertyName name of the parameter
* @param type int
* @param returnType type of object to return
* @return the converted parameter value. Null, if the property has no
* value.
* @throws IllegalArgumentException when no conversion for the given
* returnType is available or if returnType is null.
* @throws InvalidArgumentException when conversion to the given type was
* not possible due to an error while converting
*/
@SuppressWarnings("unchecked")
public <T extends Object> T getProperty(String propertyName, int type, Class<T> returnType)
{
if (returnType == null) { throw new IllegalArgumentException("ReturnType cannot be null"); }
try
{
Object result = null;
String stringValue = getProperty(propertyName, type);
if (stringValue != null)
{
result = ConvertUtils.convert(stringValue, returnType);
if ((result instanceof String) && (! returnType.equals(String.class)))
{
// If a string is returned, no converter has been found (for non-String return type)
throw new IllegalArgumentException("Unable to convert parameter to type: " + returnType.getName());
}
}
return (T) result;
}
catch (ConversionException ce)
{
// Conversion failed, wrap in Illegal
throw new InvalidArgumentException("Query property value for '" + propertyName + "' should be a valid "
+ returnType.getSimpleName());
}
}
示例13: getParameter
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
@Override
public T getParameter(String parameterName, Class<T> clazz) throws InvalidArgumentException {
String param = getParameter(parameterName);
if (param == null) return null;
Object obj = ConvertUtils.convert(param, clazz);
if (obj != null && obj.getClass().equals(clazz))
{
return (T) obj;
}
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] {parameterName});
}
示例14: toLongs
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* String[] to long[]
*
* @param str
* @return
*/
public static long[] toLongs(String[] str) {
if (str == null || str.length < 1)
return new long[] { 0L };
long[] ret = new long[str.length];
ret = (long[]) ConvertUtils.convert(str, long.class);
return ret;
}
示例15: convertValue
import org.apache.commons.beanutils.ConvertUtils; //导入方法依赖的package包/类
/**
* 转换字符串类型到clazz的property类型的值.
*
* @param value 待转换的字符串
* @param clazz 提供类型信息的Class
* @param propertyName 提供类型信息的Class的属性.
*/
public static Object convertValue(Object value, Class<?> toType) {
try {
DateConverter dc = new DateConverter();
dc.setUseLocaleFormat(true);
dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
ConvertUtils.register(dc, Date.class);
return ConvertUtils.convert(value, toType);
} catch (Exception e) {
throw convertReflectionExceptionToUnchecked(e);
}
}