X convert()

in broker-core/src/main/java/org/apache/qpid/server/model/AttributeValueConverter.java [1305:1427]


        X convert(final Object value, final ConfiguredObject object)
        {
            if(value == null)
            {
                return null;
            }
            else if(_klazz.isInstance(value))
            {
                return (X) value;
            }
            else if(value instanceof Map)
            {
                @SuppressWarnings("unchecked")
                X proxyObject =
                        (X) Proxy.newProxyInstance(_klazz.getClassLoader(), new Class[]{_klazz}, new InvocationHandler()
                        {
                            @Override
                            public Object invoke(final Object proxy, final Method method, final Object[] args)
                                    throws Throwable
                            {
                                AttributeValueConverter<?> converter = _propertyConverters.get(method);
                                Map map = (Map) value;
                                if (converter != null)
                                {
                                    return convertValue(map, converter, method, proxy);
                                }
                                else if ("toString".equals(method.getName()) && method.getParameterTypes().length == 0)
                                {
                                    return _klazz.getSimpleName() + " : " + map.toString();
                                }
                                else if ("hashCode".equals(method.getName()) && method.getParameterTypes().length == 0)
                                {
                                    return map.hashCode();
                                }
                                else if ("equals".equals(method.getName())
                                         && method.getParameterTypes().length == 1
                                         && method.getParameterTypes()[0] == Object.class)
                                {
                                    if (_klazz.isInstance(args[0]))
                                    {
                                        for (Map.Entry<Method, AttributeValueConverter<?>> entry : _propertyConverters.entrySet())
                                        {
                                            AttributeValueConverter<?> conv = entry.getValue();
                                            Method meth = entry.getKey();

                                            Object lvalue = convertValue(map, conv, meth, proxy);
                                            Object rvalue = meth.invoke(args[0]);
                                            if ((lvalue == null && rvalue != null) || (lvalue != null && !lvalue.equals(
                                                    rvalue)))
                                            {
                                                return false;
                                            }
                                        }
                                        return true;
                                    }
                                    else
                                    {
                                        return false;
                                    }
                                }
                                throw new UnsupportedOperationException(
                                        "The proxy class implements only attribute getters and toString(), hashCode() and equals()");
                            }

                            private Object convertValue(final Map map,
                                                        final AttributeValueConverter<?> converter,
                                                        final Method method, final Object proxy)
                            {
                                String attributeName = getNameFromMethod(method, getTypeFromMethod(method));
                                if (_derivedValueMethod.containsKey(method))
                                {
                                    return converter.convert(_derivedValueMethod.get(method).getValue((X) proxy),
                                                             object);
                                }
                                else if (map.containsKey(attributeName))
                                {
                                    return converter.convert(map.get(attributeName), object);
                                }
                                else
                                {
                                    return ClassUtils.defaultValue(method.getReturnType());
                                }
                            }
                        });
                if(_factoryMethod != null)
                {
                    try
                    {
                        @SuppressWarnings("unchecked")
                        X createdObject = (X) _factoryMethod.invoke(null, proxyObject);
                        return createdObject;
                    }
                    catch (IllegalAccessException | InvocationTargetException e)
                    {
                        throw new IllegalArgumentException("Cannot convert to " + _klazz.getName() + " due to error invoking factory method", e);
                    }
                }
                else
                {
                    return proxyObject;
                }

            }
            else if(value instanceof String)
            {
                String interpolated = AbstractConfiguredObject.interpolate(object, (String) value);
                if(interpolated.trim().isEmpty())
                {
                    return null;
                }
                ObjectMapper objectMapper = new ObjectMapper();
                try
                {
                    return convert(objectMapper.readValue(interpolated, Map.class), object);
                }
                catch (IOException e)
                {
                    throw new IllegalArgumentException("Cannot convert type " + value.getClass() + " to a " + _klazz.getName(), e);
                }

            }
            throw new IllegalArgumentException("Cannot convert type " + value.getClass() + " to a " + _klazz.getName());
        }