List of usage examples for org.apache.commons.beanutils ConvertUtils lookup
public static Converter lookup(Class clazz)
Look up and return any registered Converter for the specified destination class; if there is no registered Converter, return null
.
For more details see ConvertUtilsBean
.
From source file:com.comcast.cereal.impl.ReflectionHelper.java
/** * Convert the given object to be castable to the given <code>goal</code> type. * //from w w w. j av a 2 s . co m * @param obj * the object to convert * @param goal * the goal type * * @return the converted object */ public static Object convert(Object obj, Class<?> goal) { if (null == obj) { return obj; } else { if (goal.isInstance(obj)) { return obj; } else if (asPrimitive(goal).equals(asPrimitive(obj.getClass()))) { return obj; } else { Converter converter = ConvertUtils.lookup(goal); return (null != converter) ? converter.convert(goal, obj) : obj; } } }
From source file:net.servicefixture.converter.ObjectConverter.java
/** * Converts string data to java object./*w w w .ja v a2s . c o m*/ * * @param data * @param destType * @return */ public static Object convert(String data, Class destType, boolean checkType) { // Convert EMPTY_TOKEN to an empty array if (EMPTY_TOKEN.equals(data) && CollectionBuilderFactory.isCollectionType(destType)) { return CollectionBuilderFactory.createCollectionBuilder(null, null, destType).getCollection(); } // Check if it s a jexl token if (isExpression(data)) { return evaluateJexlToken(extractExpression(data), destType, checkType); } if (Calendar.class.isAssignableFrom(destType)) { destType = Calendar.class; } // If the destType is not supported by ConvertUtils if (ConvertUtils.lookup(destType) == null) { return specialConvert(data, destType); } return ConvertUtils.convert(data, destType); }
From source file:com.zaradai.kunzite.trader.config.statics.digester.DigesterStaticLoaderUnitTest.java
@Test public void shouldRegisterConverters() throws Exception { uut.load(""); assertThat(ConvertUtils.lookup(DateTime.class), instanceOf(DateTimeConverter.class)); assertThat(ConvertUtils.lookup(OptionType.class), instanceOf(OptionTypeConverter.class)); assertThat(ConvertUtils.lookup(InstrumentType.class), instanceOf(InstrumentTypeConverter.class)); }
From source file:com.subakva.formicid.Container.java
public Converter getConverter(Class type) { Converter converter = ConvertUtils.lookup(type); if (converter == null) { throw new ConversionException("Unknown conversion type: " + type); }// w w w. j a va2 s . c om return converter; }
From source file:es.caib.zkib.jxpath.util.BasicTypeConverter.java
/** * Returns true if it can convert the supplied * object to the specified class./*from w w w . j a va2s . co m*/ * @param object to check * @param toType prospective destination class * @return boolean */ public boolean canConvert(Object object, final Class toType) { if (object == null) { return true; } final Class useType = TypeUtils.wrapPrimitive(toType); Class fromType = object.getClass(); if (useType.isAssignableFrom(fromType)) { return true; } if (useType == String.class) { return true; } if (object instanceof Boolean && (Number.class.isAssignableFrom(useType) || "java.util.concurrent.atomic.AtomicBoolean".equals(useType.getName()))) { return true; } if (object instanceof Number && (Number.class.isAssignableFrom(useType) || useType == Boolean.class)) { return true; } if (object instanceof String && (useType == Boolean.class || useType == Character.class || useType == Byte.class || useType == Short.class || useType == Integer.class || useType == Long.class || useType == Float.class || useType == Double.class)) { return true; } if (fromType.isArray()) { // Collection -> array if (useType.isArray()) { Class cType = useType.getComponentType(); int length = Array.getLength(object); for (int i = 0; i < length; i++) { Object value = Array.get(object, i); if (!canConvert(value, cType)) { return false; } } return true; } if (Collection.class.isAssignableFrom(useType)) { return canCreateCollection(useType); } if (Array.getLength(object) > 0) { Object value = Array.get(object, 0); return canConvert(value, useType); } return canConvert("", useType); } if (object instanceof Collection) { // Collection -> array if (useType.isArray()) { Class cType = useType.getComponentType(); Iterator it = ((Collection) object).iterator(); while (it.hasNext()) { Object value = it.next(); if (!canConvert(value, cType)) { return false; } } return true; } if (Collection.class.isAssignableFrom(useType)) { return canCreateCollection(useType); } if (((Collection) object).size() > 0) { Object value; if (object instanceof List) { value = ((List) object).get(0); } else { Iterator it = ((Collection) object).iterator(); value = it.next(); } return canConvert(value, useType); } return canConvert("", useType); } if (object instanceof NodeSet) { return canConvert(((NodeSet) object).getValues(), useType); } if (object instanceof Pointer) { return canConvert(((Pointer) object).getValue(), useType); } return ConvertUtils.lookup(useType) != null; }
From source file:com.rodaxsoft.mailgun.MailingListMemberManager.java
/** * Converts the <code>vars</code> object into an instance of the varsType and * returns the members of the mailing list * @param varsType The target type for the <i>vars</i> object. * <i>Note: If no registered {@link Converter} is found for the varsType, * a {@link ContextedRuntimeException} will be thrown.</i> * @return A list of list member objects * @throws ContextedException If no registered {@link Converter} is found for the varsType * @see ConvertUtils#register(Converter, Class) * @since 0.2/*from w ww.j av a2 s . co m*/ * */ List<ListMember> getListMembers(Class<?> varsType) throws ContextedException { List<ListMember> members = null; if (varsType != null) { if (ConvertUtils.lookup(varsType) != null) { listMemberConverter.setVarClass(varsType); } else { ContextedException cre; cre = new ContextedException("No registered Converter for varType"); cre.addContextValue("varType", varsType.getName()); throw cre; } } final String path = "/lists/" + listAddress + "/members"; RESTResponse response = invokeGet(path); if (response.success()) { JSONObject jsonObj = response.toJSONObject(); JSONArray jsonArray = jsonObj.getJSONArray("items"); @SuppressWarnings("unchecked") Iterator<JSONObject> iter = jsonArray.iterator(); while (iter.hasNext()) { JSONObject obj = iter.next(); if (null == members) { members = new ArrayList<>(); } ListMember lm = (ListMember) ConvertUtils.convert(obj, ListMember.class); members.add(lm); } } return members; }
From source file:com.projity.field.FieldConverter.java
/** * /*from w ww .j av a 2 s .c o m*/ * @param value. Convert from this value * @param clazz. Convert to this clazz type. * @return object of type clazz. * @throws FieldParseException */ private Object _convert(Object value, Class clazz, FieldContext context) throws FieldParseException { try { if (value instanceof String) { Object result = null; if (context == null) result = ConvertUtils.convert((String) value, clazz); else { Converter contextConverter = null; HashMap<Class, Converter> contextMap = contextMaps.get(context); if (contextMap != null) contextConverter = contextMap.get(clazz); if (contextConverter != null) { contextConverter.convert(clazz, value); } else { System.out.println("no context converter found "); result = ConvertUtils.convert((String) value, clazz); } } // if (result instanceof java.util.Date) { // dates need to be normalized // result = new Date(DateTime.gmt((Date) result)); // } if (result == null) { throw new FieldParseException("Invalid type"); } return result; } // Because of stupidity of beanutils which assumes type string, I implement this by hand Converter converter = ConvertUtils.lookup(clazz); if (converter == null) { System.out.println( "converter is null for class " + clazz + " instance " + instance.hashCode() + " resetting"); instance = new FieldConverter(); converter = ConvertUtils.lookup(String.class); } return converter.convert(clazz, value); } catch (ConversionException conversionException) { throw new FieldParseException(conversionException); } }
From source file:com.feilong.core.bean.BeanUtilTest.java
/** * Copy property./* ww w .jav a 2 s . c o m*/ */ @Test public void testCopyProperties1() { User user = new User(); user.setId(5L); user.setMoney(new BigDecimal(500000)); user.setDate(new Date()); user.setNickNames(toArray("feilong", "", "venusdrogon")); // ConvertUtils.register(new DateLocaleConverter(Locale.US, TO_STRING_STYLE), Date.class); BeanUtil.register(new DateLocaleConverter(Locale.US, TO_STRING_STYLE), Date.class); Converter converter = ConvertUtils.lookup(Date.class); LOGGER.debug("{},{}", converter.getClass().getSimpleName(), converter.convert(Date.class, new Date().toString())); User user2 = new User(); BeanUtil.copyProperties(user2, user, "date", "money", "nickNames"); LOGGER.debug(JsonUtil.format(user2)); converter = ConvertUtils.lookup(Date.class); LOGGER.debug("{},{}", converter.getClass().getSimpleName(), converter.convert(Date.class, new Date().toString())); }
From source file:com.algoTrader.PropertySearch.java
/** * <p>Convert the value to an object of the specified class (if * possible).</p>/*from ww w . ja v a 2 s . c om*/ * * @param value Value to be converted (may be null) * @param type Class of the value to be converted to * @return The converted value * * @exception ConversionException if thrown by an underlying Converter */ protected Object convert(Object value, Class type) { Converter converter = ConvertUtils.lookup(type); Object result; if (converter != null) { result = converter.convert(type, value); } else { result = value; } return result; }
From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java
public static RowMapperFactory getScalarMapper(final Type itemType, final int idx, boolean req) { if (itemType == String.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getString(idx); }// w w w. j a va 2 s. c om }; } if (itemType == Integer.class || itemType == Integer.TYPE) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getInt(idx); } }; } if (itemType == URL.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getURL(idx); } }; } if (itemType == BigInteger.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { final BigDecimal res = rs.getBigDecimal(idx); return res == null ? null : res.toBigInteger(); } }; } if (itemType == BigDecimal.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBigDecimal(idx); } }; } if (itemType == InputStream.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBinaryStream(idx); } }; } if (itemType == Boolean.class || itemType == Boolean.TYPE) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBoolean(idx); } }; } if (itemType == Blob.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getBlob(idx); } }; } if (itemType == java.sql.Date.class || itemType == java.util.Date.class) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return rs.getTimestamp(idx); } }; } if (itemType instanceof Class) { final Class itemClass = (Class) itemType; final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass); if (columnMapper != null) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { return columnMapper.map(rs, idx); } }; } final Converter converter = ConvertUtils.lookup(itemClass); if (converter != null) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { String s = rs.getString(idx); if (s == null) { return null; } return converter.convert(itemClass, s); } }; } if (Enum.class.isAssignableFrom((Class<?>) itemType)) { return new ScalarRMF() { public Object map(ResultSet rs) throws SQLException { String s = rs.getString(idx); if (s == null) { return null; } //noinspection unchecked return Enum.valueOf((Class<Enum>) itemType, s); } }; } } if (req) { throw new IllegalArgumentException("no mapping defined for " + itemType); } return null; }