use of org.apache.commons.beanutils.Converter in project onebusaway-gtfs-modules by OneBusAway.
the class DeferredValueConverter method convertValue.
/**
* Converts the specified value as appropriate such that the resulting value
* can be assigned to the specified property of the specified bean.
*/
public Object convertValue(BeanWrapper targetBean, String targetPropertyName, Object value) {
if (value == null) {
return null;
}
Class<?> expectedValueType = targetBean.getPropertyType(targetPropertyName);
Class<?> actualValueType = value.getClass();
/**
* When we introspect the "id" property of an IdentityBean instance, the
* return type is always Serializable, when the actual type is String or
* AgencyAndId. This causes trouble with the "isAssignableFrom" check below,
* so we do a first check here.
*/
Object parentObject = targetBean.getWrappedInstance(Object.class);
if (parentObject instanceof IdentityBean && targetPropertyName.equals("id")) {
Class<?> idType = getIdentityBeanIdType(parentObject);
if (idType == AgencyAndId.class && actualValueType == String.class) {
return _support.resolveAgencyAndId(targetBean, targetPropertyName, (String) value);
}
}
if (expectedValueType.isAssignableFrom(actualValueType)) {
return value;
}
if (isPrimitiveAssignable(expectedValueType, actualValueType)) {
return value;
}
if (actualValueType == String.class) {
String stringValue = (String) value;
if (AgencyAndId.class.isAssignableFrom(expectedValueType)) {
return _support.resolveAgencyAndId(targetBean, targetPropertyName, stringValue);
}
if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
Serializable id = stringValue;
if (getIdType(expectedValueType) == AgencyAndId.class) {
GtfsReaderContext context = _support.getReader().getGtfsReaderContext();
String agencyId = context.getAgencyForEntity(expectedValueType, stringValue);
id = new AgencyAndId(agencyId, stringValue);
}
Object entity = _dao.getEntityForId(expectedValueType, id);
if (entity == null) {
throw new IllegalStateException("entity not found: type=" + expectedValueType.getName() + " id=" + id);
}
return entity;
}
Class<?> parentEntityType = parentObject.getClass();
Converter converter = _support.resolveConverter(parentEntityType, targetPropertyName, expectedValueType);
if (converter != null) {
return converter.convert(expectedValueType, value);
}
}
throw new IllegalStateException("no conversion possible from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\"");
}
use of org.apache.commons.beanutils.Converter in project onebusaway-gtfs-modules by OneBusAway.
the class ObjectEquality method objectsAreEqual.
public static boolean objectsAreEqual(Object expected, Object actual) {
boolean nullA = expected == null;
boolean nullB = actual == null;
if (nullA && nullB)
return true;
if (nullA ^ nullB)
return false;
Class<?> expectedType = expected.getClass();
Class<?> actualType = actual.getClass();
/**
* Implementation note: This conversion theoretically will happen over and
* over with the same value. Is there some way to cache it?
*/
if (!actualType.isAssignableFrom(expectedType) && expectedType == String.class) {
Converter converter = ConvertUtils.lookup(actualType);
if (converter != null) {
Object converted = converter.convert(actualType, expected);
if (converted != null)
expected = converted;
}
}
return (expected == null && actual == null) || (expected != null && expected.equals(actual));
}
use of org.apache.commons.beanutils.Converter in project onebusaway-gtfs-modules by OneBusAway.
the class DeferredValueSupportTest method testResolveConverter_FieldMappingConverter.
@Test
public void testResolveConverter_FieldMappingConverter() {
EntitySchema schema = new EntitySchema(Object.class, "object.txt", false);
FieldMappingAndConverter field = mock(FieldMappingAndConverter.class);
when(field.getCsvFieldName()).thenReturn("xyz");
schema.addField(field);
_schemaCache.addEntitySchema(schema);
Converter converter = _support.resolveConverter(Object.class, "xyz", String.class);
assertSame(field, converter);
}
use of org.apache.commons.beanutils.Converter in project apex-core by apache.
the class StringCodecs method register.
public static <T> void register(final Class<? extends StringCodec<?>> codec, final Class<T> clazz) throws InstantiationException, IllegalAccessException {
check();
final StringCodec<?> codecInstance = codec.newInstance();
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
return value == null ? null : codecInstance.fromString(value.toString());
}
}, clazz);
codecs.put(clazz, codec);
}
use of org.apache.commons.beanutils.Converter in project apex-core by apache.
the class StringCodecs method loadDefaultConverters.
public static void loadDefaultConverters() {
LOG.debug("Loading default converters for BeanUtils");
ConvertUtils.register(new Converter() {
@Override
@SuppressWarnings("unchecked")
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
for (Class<?> clazz = value.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
Class<? extends StringCodec> codec = codecs.get(clazz);
if (codec == null) {
continue;
}
StringCodec instance;
try {
instance = codec.newInstance();
} catch (IllegalAccessException ex) {
throw new RuntimeException("Internal Error - it's impossible for this exception to be thrown!", ex);
} catch (InstantiationException ex) {
throw new RuntimeException("Internal Error - it's impossible for this exception to be thrown!", ex);
}
return instance.toString(value);
}
return value.toString();
}
}, String.class);
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
return value == null ? null : URI.create(value.toString());
}
}, URI.class);
}
Aggregations