public Object convert()

in blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java [141:191]


    public Object convert(Object fromValue, final ReifiedType type) throws Exception {
        // Discard null values
        if (fromValue == null) {
            return null;
        }
        // First convert service proxies
        if (fromValue instanceof Convertible) {
            return ((Convertible) fromValue).convert(type);
        } else if (fromValue instanceof UnwrapperedBeanHolder) {
            UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) fromValue;
            if (isAssignable(holder.unwrapperedBean, type)) {
                return BeanRecipe.wrap(holder, type.getRawClass());
            } else {
                fromValue = BeanRecipe.wrap(holder, Object.class);
            }
        } else if (isAssignable(fromValue, type)) {
            // If the object is an instance of the type, just return it
            return fromValue;
        }
        
        final Object finalFromValue = fromValue;
        ConversionResult result = null;
        AccessControlContext acc = blueprintContainer.getAccessControlContext();
        if (acc == null) {
            result = convertWithConverters(fromValue, type);
        } else {
            result = AccessController.doPrivileged(new PrivilegedExceptionAction<ConversionResult>() {
                public ConversionResult run() throws Exception {
                    return convertWithConverters(finalFromValue, type);
                }            
            }, acc);
        }
        if (result == null) {
            if (fromValue instanceof Number && Number.class.isAssignableFrom(unwrap(toClass(type)))) {
                return convertToNumber((Number) fromValue, toClass(type));
            } else if (fromValue instanceof String) {
                return convertFromString((String) fromValue, toClass(type), blueprintContainer);
            } else if (toClass(type).isArray() && (fromValue instanceof Collection || fromValue.getClass().isArray())) {
                return convertToArray(fromValue, type);
            } else if (Map.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Map || fromValue instanceof Dictionary)) {
                return convertToMap(fromValue, type);
            } else if (Dictionary.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Map || fromValue instanceof Dictionary)) {
                return convertToDictionary(fromValue, type);
            } else if (Collection.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Collection || fromValue.getClass().isArray())) {
                return convertToCollection(fromValue, type);
            } else {
                throw new Exception("Unable to convert value " + fromValue + " to type " + type);
            }
        }
        return result.value;
    }