protected Object internalCreate()

in xbean-reflect/src/main/java/org/apache/xbean/recipe/CollectionRecipe.java [120:174]


    protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException {
        Class type = getType(expectedType);

        if (!RecipeHelper.hasDefaultConstructor(type)) {
            throw new ConstructionException("Type does not have a default constructor " + type.getName());
        }

        // create collection instance
        Object o;
        try {
            o = type.newInstance();
        } catch (Exception e) {
            throw new ConstructionException("Error while creating collection instance: " + type.getName());
        }
        if (!(o instanceof Collection)) {
            throw new ConstructionException("Specified collection type does not implement the Collection interface: " + type.getName());
        }
        Collection instance = (Collection) o;

        // add to execution context if name is specified
        if (getName() != null) {
            ExecutionContext.getContext().addObject(getName(), instance);
        }

        // get component type
        Type[] typeParameters = RecipeHelper.getTypeParameters(Collection.class, expectedType);
        Type componentType = Object.class;
        if (typeParameters != null && typeParameters.length == 1 && typeParameters[0] instanceof Class) {
            componentType = typeParameters[0];
        }

        boolean refAllowed = options.contains(Option.LAZY_ASSIGNMENT);

        int index = 0;
        for (Object value : list) {
            value = RecipeHelper.convert(componentType, value, refAllowed, registry);

            if (value instanceof Reference) {
                Reference reference = (Reference) value;
                if (instance instanceof List) {
                    // add a null place holder in the list that will be updated later
                    //noinspection unchecked
                    instance.add(null);
                    reference.setAction(new UpdateList((List) instance, index));
                } else {
                    reference.setAction(new UpdateCollection(instance));
                }
            } else {
                //noinspection unchecked
                instance.add(value);
            }
            index++;
        }
        return instance;
    }