Search in sources :

Example 1 with ObjectAccessException

use of com.thoughtworks.xstream.converters.reflection.ObjectAccessException in project hudson-2.x by hudson.

the class RobustReflectionConverter method writeValueToImplicitCollection.

private Map writeValueToImplicitCollection(UnmarshallingContext context, Object value, Map implicitCollections, Object result, String itemFieldName) {
    String fieldName = mapper.getFieldNameForItemTypeAndName(context.getRequiredType(), value.getClass(), itemFieldName);
    if (fieldName != null) {
        if (implicitCollections == null) {
            // lazy instantiation
            implicitCollections = new HashMap();
        }
        Collection collection = (Collection) implicitCollections.get(fieldName);
        if (collection == null) {
            Class fieldType = mapper.defaultImplementationOf(reflectionProvider.getFieldType(result, fieldName, null));
            if (!Collection.class.isAssignableFrom(fieldType)) {
                throw new ObjectAccessException("Field " + fieldName + " of " + result.getClass().getName() + " is configured for an implicit Collection, but field is of type " + fieldType.getName());
            }
            if (pureJavaReflectionProvider == null) {
                pureJavaReflectionProvider = new PureJavaReflectionProvider();
            }
            collection = (Collection) pureJavaReflectionProvider.newInstance(fieldType);
            reflectionProvider.writeField(result, fieldName, collection, null);
            implicitCollections.put(fieldName, collection);
        }
        collection.add(value);
    }
    return implicitCollections;
}
Also used : HashMap(java.util.HashMap) ObjectAccessException(com.thoughtworks.xstream.converters.reflection.ObjectAccessException) Collection(java.util.Collection) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Aggregations

ObjectAccessException (com.thoughtworks.xstream.converters.reflection.ObjectAccessException)1 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1