Hi All.
With current SnakeYAML implementation I would actually try a bit different approach (since I don't really like custom Representation/Constructor :)
You can try to create something like this
public class PropertyDelegate extends Property {
private Property delegate;
public PropertyDelegate(String nameSubstitute, Property property) {
super(nameSubstitute, property.getType());
delegate = property;
}
@Override
public Class<?>[] getActualTypeArguments() {
return delegate.getActualTypeArguments();
}
@Override
public void set(Object object, Object value) throws Exception {
delegate.set(object, value);
}
@Override
public Object get(Object object) {
return delegate.get(object);
}
}
Then you can extend PropertyUtils something like this
public class MyPropertyUtils extends PropertyUtils {
private final Map<Class<?>, Map<String, Property>> myPropertiesCache = new HashMap<Class<?>, Map<String, Property>>();
@Override
protected Map<String, Property> getPropertiesMap(Class<?> type, BeanAccess bAccess)
throws IntrospectionException {
if (myPropertiesCache.containsKey(type)) {
return myPropertiesCache.get(type);
}
Map<String, Property> properties = super.getPropertiesMap(type, bAccess);
HashMap<String, Property> processedProperties = new HashMap<String, Property>(
properties.size());
for (Map.Entry<String, Property> name2property : properties.entrySet()) {
String newName = processPropertyName(name2property.getKey());
processedProperties.put(newName,
new PropertyDelegate(newName, name2property.getValue()));
}
myPropertiesCache.put(type, processedProperties);
return processedProperties;
}
private String processPropertyName(String name) {
/* do something with your name */
return name;
}
}
Unfortunately you have to maintain your own cache of property map, because it is private in PropertyUtils. And as you can see I prefer to copy results (you can modify properties map directly, but then don't forget to remove mappings with original name).
Of course you need to put some of you logic in getPropertiesMap (maybe you want to change property names only for some classes or even only for some property).
I have no idea if this works :) I am afraid you have to try.
And I would really appreciate your feedback on this approach, thx
-Alex
P.S.
Don't forget to set your MyPropertyUtils to constructor and representer ;)