public CollectionHelper createCollectionHelper()

in ch-commons-xbean/src/main/java/com/cloudhopper/commons/xbean/XmlBean.java [571:620]


    public CollectionHelper createCollectionHelper(XmlParser.Node node, Object obj, Object value, String propertyName, BeanProperty property, String valueAttrString, String keyAttrString) throws PropertyPermissionException, XmlBeanClassException {
        // special handling for "collections"
        CollectionHelper newch = null;
        
        // determine "propertyName" for elements within the collection
        String valuePropertyName = (valueAttrString != null ? valueAttrString : "value");

        // determine type of objects to create by determining generic type
        if (property.getField() == null) {
            throw new PropertyPermissionException(propertyName, node.getPath(), obj.getClass(), "Unable to access field for collection type [" + value.getClass().getName() + "]");
        } else {
            Type type = property.getField().getGenericType();
            if (!(type instanceof ParameterizedType)) {
                throw new PropertyPermissionException(propertyName, node.getPath(), obj.getClass(), "Only collection types with a parameterized type are supported");
            } else {
                ParameterizedType pt = (ParameterizedType)type;
                Type[] pts = pt.getActualTypeArguments();
                
                if (value instanceof Map) {
                    // Maps must have 2 parameterized types
                    if (pts.length != 2) {
                        throw new PropertyPermissionException(propertyName, node.getPath(), obj.getClass(), "Only map types with 2 parameterized types are supported; actual [" + pts.length + "]");
                    }
                    Type keyType = pts[0];
                    Type valueType = pts[1];
                    if (!(keyType instanceof Class && valueType instanceof Class)) {
                        throw new PropertyPermissionException(propertyName, node.getPath(), obj.getClass(), "Only a map with paramertized types of concrete classes are supported (e.g. TreeMap<String,Integer> rather than TreeMap<String,ArrayList<String>>)");
                    }
                    Class keyClass = (Class)keyType;
                    Class valueClass = (Class)valueType;
                    Map m = (Map)value;
                    newch = CollectionHelper.createMapType(m, valuePropertyName, valueClass, keyAttrString, keyClass);
                } else {
                    // Collections must have 1 parameterized type
                    if (pts.length != 1) {
                        throw new PropertyPermissionException(propertyName, node.getPath(), obj.getClass(), "Only collection types with 1 parameterized type are supported; actual [" + pts.length + "]");
                    }
                    Type valueType = pts[0];
                    if (!(valueType instanceof Class)) {
                        throw new PropertyPermissionException(propertyName, node.getPath(), obj.getClass(), "Only a collection with a parameterized type of a concrete class is supported (e.g. ArrayList<String> rather than ArrayList<ArrayList<String>>)");
                    }
                    Class valueClass = (Class)valueType;
                    Collection c = (Collection)value;
                    newch = CollectionHelper.createCollectionType(c, valuePropertyName, valueClass);
                }
            }
        }
        
        return newch;
    }