List of usage examples for org.apache.commons.beanutils ConvertUtilsBean ConvertUtilsBean
public ConvertUtilsBean()
From source file:BeanUtilsExampleV4.java
public static void main(String args[]) throws Exception { BeanUtilsExampleV4 diff = new BeanUtilsExampleV4(); Actor actor = diff.prepareData();//from w w w.j a v a 2s. c om ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean(); convertUtilsBean.deregister(String.class); convertUtilsBean.register(new MyStringConverter(), String.class); convertUtilsBean.deregister(Long.class); convertUtilsBean.register(new MyLongConverter(), Long.class); convertUtilsBean.register(new MyLongConverter(), Long.TYPE); BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean()); System.err.println("==== Values before calling describe ==== "); System.err.println("By PropertyUtils: " + PropertyUtils.getProperty(actor, "name")); System.err.println("By BeanUtils: " + beanUtilsBean.getProperty(actor, "name")); System.err.println(beanUtilsBean.getProperty(actor, "worth")); Map describedData = beanUtilsBean.describe(actor); // check the map System.err.println("==== Values in Map ==== "); System.err.println(describedData.get("name")); System.err.println(describedData.get("worth")); // create a new Actor Bean Actor newActor = new Actor(); beanUtilsBean.populate(newActor, describedData); System.err.println("==== Values after calling populate ==== "); System.err.println(beanUtilsBean.getProperty(newActor, "name")); System.err.println(beanUtilsBean.getProperty(newActor, "worth")); }
From source file:com.hbc.api.trade.bdata.common.util.BeanUtilsEnhance.java
/** * ?orig??dest<p/>/*ww w . ja v a 2 s . co m*/ * ???Javabyte, shortdestinteger??? * @param dest * @param orig */ public static void copyProperties(Object dest, Object orig) { ConvertUtilsBean convertUtil = new ConvertUtilsBean(); convertUtil.register(false, true, 10); BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtil); try { beanUtils.copyProperties(dest, orig); } catch (IllegalAccessException | InvocationTargetException e) { log.error("?from:" + JSON.toJSONString(orig) + ", to: " + JSON.toJSONString(dest), e); throw new ParamValidateException(CommonReturnCodeEnum.PARAM_ERROR); } }
From source file:com.yahoo.elide.utils.coerce.CoerceUtil.java
/** * Perform CoerceUtil setup./*from w w w. j a v a 2 s . co m*/ */ private static void setup() { BeanUtilsBean.setInstance(new BeanUtilsBean(new ConvertUtilsBean() { @Override /* * Overriding lookup to execute enum converter if target is enum * or map convert if source is map */ public Converter lookup(Class<?> sourceType, Class<?> targetType) { if (targetType.isEnum()) { return TO_ENUM_CONVERTER; } else if (Map.class.isAssignableFrom(sourceType)) { return FROM_MAP_CONVERTER; } else { return super.lookup(sourceType, targetType); } } })); }
From source file:com.github.steveash.typedconfig.resolver.ConvertableValueResolver.java
public ConvertableValueResolver(Class<?> targetClazz, String key) { this.targetClazz = targetClazz; this.key = key; this.converter = new ConvertUtilsBean(); }
From source file:com.teamsun.framework.util.BeanUtils.java
public BeanUtils() { super(new ConvertUtilsBean(), new PropertyUtilsBean()); }
From source file:com.custardsource.maven.plugins.jmx.SetAttribute.java
@Override public Object execute(MBeanServerConnection connection) throws Exception { ObjectName name = new ObjectName(objectName); ConvertUtilsBean converter = new ConvertUtilsBean(); Object attributeValue = converter.convert(value, ClassUtils.getClass(type)); connection.setAttribute(name, new Attribute(attributeName, attributeValue)); return connection.getAttribute(name, attributeName); }
From source file:com.gs.obevo.db.impl.core.changetypes.CsvStaticDataReader.java
public CsvReaderDataSource getFileDataSource(int csvVersion, DaTable table, String content, char dataDelimiter, String nullToken, Function<String, String> convertDbObjectName) { CsvReaderDataSource fileSource = new CsvReaderDataSource(csvVersion, "fileSource", new StringReader(content), dataDelimiter, convertDbObjectName, nullToken); ConvertUtilsBean cub = new ConvertUtilsBean(); for (DaColumn col : table.getColumns()) { Class targetClassName;//from w ww.j a v a 2 s. co m // This is to handle cases in Sybase ASE where a column comes back in quotes, e.g. "Date" // This happens if the column name happens to be a keyword, e.g. for Date String columnName = col.getName(); if (columnName.startsWith("\"") && columnName.endsWith("\"")) { columnName = columnName.substring(1, columnName.length() - 1); } try { // this is to handle "tinyint" if (col.getColumnDataType().getTypeClassName().equalsIgnoreCase("byte")) { targetClassName = Integer.class; } else { targetClassName = Class.forName(col.getColumnDataType().getTypeClassName()); } } catch (ClassNotFoundException e) { throw new DeployerRuntimeException(e); } fileSource.addDerivedField(new MyDerivedField(csvVersion, convertDbObjectName.valueOf(columnName), targetClassName, cub, nullToken)); } fileSource.init(); // initialize so that we can discover the fields in the file return fileSource; }
From source file:com.astamuse.asta4d.web.initialization.SimplePropertyFileInitializer.java
protected BeanUtilsBean retrieveBeanUtilsBean() { BeanUtilsBean bu = new BeanUtilsBean(new ConvertUtilsBean() { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override/*from w w w . j ava 2s . co m*/ public Object convert(String value, Class clazz) { if (clazz.isEnum()) { return Enum.valueOf(clazz, value); } else if (clazz.equals(Class.class)) { try { return Class.forName(value); } catch (Exception e) { throw new RuntimeException(e); } } else if (expectInstance(clazz)) { try { return Class.forName(value).newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } else { return super.convert(value, clazz); } } }); return bu; }
From source file:com.custardsource.maven.plugins.jmx.Invoke.java
@Override public Object execute(MBeanServerConnection connection) throws Exception { ObjectName name = new ObjectName(objectName); int len = parameters == null ? 0 : parameters.length; String[] signature = new String[len]; Object[] arguments = new Object[len]; ConvertUtilsBean converter = new ConvertUtilsBean(); for (int i = 0; i < len; i++) { signature[i] = parameters[i].getType(); arguments[i] = converter.convert(parameters[i].getValue(), ClassUtils.getClass(parameters[i].getType())); if (signature[i].equals("java.lang.String") && arguments[i] == null) { arguments[i] = ""; }/*from w w w.j a v a 2s .c o m*/ } return connection.invoke(name, operation, arguments, signature); }
From source file:com.puppycrawl.tools.checkstyle.api.AutomaticBean.java
/** * Creates a BeanUtilsBean that is configured to use * type converters that throw a ConversionException * instead of using the default value when something * goes wrong.//from ww w . j a v a2s . c om * * @return a configured BeanUtilsBean */ private static BeanUtilsBean createBeanUtilsBean() { final ConvertUtilsBean cub = new ConvertUtilsBean(); cub.register(new BooleanConverter(), Boolean.TYPE); cub.register(new BooleanConverter(), Boolean.class); cub.register(new ArrayConverter(boolean[].class, new BooleanConverter()), boolean[].class); cub.register(new ByteConverter(), Byte.TYPE); cub.register(new ByteConverter(), Byte.class); cub.register(new ArrayConverter(byte[].class, new ByteConverter()), byte[].class); cub.register(new CharacterConverter(), Character.TYPE); cub.register(new CharacterConverter(), Character.class); cub.register(new ArrayConverter(char[].class, new CharacterConverter()), char[].class); cub.register(new DoubleConverter(), Double.TYPE); cub.register(new DoubleConverter(), Double.class); cub.register(new ArrayConverter(double[].class, new DoubleConverter()), double[].class); cub.register(new FloatConverter(), Float.TYPE); cub.register(new FloatConverter(), Float.class); cub.register(new ArrayConverter(float[].class, new FloatConverter()), float[].class); cub.register(new IntegerConverter(), Integer.TYPE); cub.register(new IntegerConverter(), Integer.class); cub.register(new ArrayConverter(int[].class, new IntegerConverter()), int[].class); cub.register(new LongConverter(), Long.TYPE); cub.register(new LongConverter(), Long.class); cub.register(new ArrayConverter(long[].class, new LongConverter()), long[].class); cub.register(new ShortConverter(), Short.TYPE); cub.register(new ShortConverter(), Short.class); cub.register(new ArrayConverter(short[].class, new ShortConverter()), short[].class); cub.register(new RelaxedStringArrayConverter(), String[].class); // BigDecimal, BigInteger, Class, Date, String, Time, TimeStamp // do not use defaults in the default configuration of ConvertUtilsBean return new BeanUtilsBean(cub, new PropertyUtilsBean()); }