You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by William Tam <em...@gmail.com> on 2007/10/08 02:32:31 UTC

Patch to convert from String to Boolean

I wasn't able to convert a String to a Boolean using a route such as
from("direct:test").convertBodyTo(Boolean.class).  The problem is the
annotated converter method ObjectConverter.toBoolean(Object value) is
never reached.  The reason is that the converter is registered with
the key [class java.lang.Object=>class java.lang.Boolean] as taken
from the signature.  However, my route uses the key [class
java.lang.String=>class java.lang.Boolean] to look up a converter (so
none found)  since my in-message type is String (not Object).  I have
attached a patch which includes a unit test.

Regards,
William

Re: Patch to convert from String to Boolean

Posted by William Tam <em...@gmail.com>.
cool.

On 10/9/07, James Strachan <ja...@gmail.com> wrote:
> IIRC the reason I skipped Object was to avoid finding Object based
> converters before interface based converters. i.e. Object should be
> used last after all other base classes or interfaces are attempted.
>
> I've just patched the type converter to look for Object based
> converters after the base classes and any interfaces - along with
> adding a test case for testing String -> Boolean and all appears well
> now
>
>
> On 08/10/2007, William Tam <em...@gmail.com> wrote:
> > I see.  I think we can do something like this instead.
> >
> >            Class fromSuperClass = fromType.getSuperclass();
> >             if (fromSuperClass != null) {
> >                 TypeConverter converter = getTypeConverter(toType,
> > fromSuperClass);
> >                 if (converter == null && !fromSuperClass.equals(Object.class)) {
> >                     converter = findTypeConverter(toType,
> > fromSuperClass, value);
> >                 }
> >                 if (converter != null) {
> >                     return converter;
> >                 }
> >             }
> >
> > BTW, you can use my test case in the patch to verify it.  (just ignore
> > the ObjectConverter change).
> >
> >
> > On 10/8/07, Hiram Chirino <hi...@hiramchirino.com> wrote:
> > > I think it was trying to break out of the recursion.. but perhaps it's
> > > doing the check at the wrong point.
> > >
> > > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > > In method org.apache.camel.impl.converter.DefaultTypeConverter.findTypeConverter(),
> > > > any reasons why it skips if "!fromSuperClass.equals(Object.class)"?
> > > > My test case seems to work I leave that out since it is then try to
> > > > find the converter for Object->Boolean.
> > > >
> > > >        Class fromSuperClass = fromType.getSuperclass();
> > > >             if (fromSuperClass != null &&
> > > > !fromSuperClass.equals(Object.class)) {
> > > >                 TypeConverter converter = getTypeConverter(toType,
> > > > fromSuperClass);
> > > >                 if (converter == null) {
> > > >                     converter = findTypeConverter(toType,
> > > > fromSuperClass, value);
> > > >                 }
> > > >                 if (converter != null) {
> > > >                     return converter;
> > > >                 }
> > > >             }
> > > >
> > > > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > > > Good point.  I'll take a look at that.
> > > > >
> > > > > On 10/8/07, Guillaume Nodet <gn...@gmail.com> wrote:
> > > > > > Shouldn't the converter registry be able to do that ? If there is a
> > > > > > conversion from x to y available, it should use it whenever the object to be
> > > > > > converted inherits / implements x.
> > > > > >
> > > > > > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > > > > >
> > > > > > > I wasn't able to convert a String to a Boolean using a route such as
> > > > > > > from("direct:test").convertBodyTo(Boolean.class).  The problem is the
> > > > > > > annotated converter method ObjectConverter.toBoolean(Object value) is
> > > > > > > never reached.  The reason is that the converter is registered with
> > > > > > > the key [class java.lang.Object=>class java.lang.Boolean] as taken
> > > > > > > from the signature.  However, my route uses the key [class
> > > > > > > java.lang.String=>class java.lang.Boolean] to look up a converter (so
> > > > > > > none found)  since my in-message type is String (not Object).  I have
> > > > > > > attached a patch which includes a unit test.
> > > > > > >
> > > > > > > Regards,
> > > > > > > William
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Cheers,
> > > > > > Guillaume Nodet
> > > > > > ------------------------
> > > > > > Blog: http://gnodet.blogspot.com/
> > > > > >
> > > > >
> > > >
> > >
> > >
> > > --
> > > Regards,
> > > Hiram
> > >
> > > Blog: http://hiramchirino.com
> > >
> >
>
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source SOA
> http://open.iona.com
>

Re: Patch to convert from String to Boolean

Posted by James Strachan <ja...@gmail.com>.
IIRC the reason I skipped Object was to avoid finding Object based
converters before interface based converters. i.e. Object should be
used last after all other base classes or interfaces are attempted.

I've just patched the type converter to look for Object based
converters after the base classes and any interfaces - along with
adding a test case for testing String -> Boolean and all appears well
now


On 08/10/2007, William Tam <em...@gmail.com> wrote:
> I see.  I think we can do something like this instead.
>
>            Class fromSuperClass = fromType.getSuperclass();
>             if (fromSuperClass != null) {
>                 TypeConverter converter = getTypeConverter(toType,
> fromSuperClass);
>                 if (converter == null && !fromSuperClass.equals(Object.class)) {
>                     converter = findTypeConverter(toType,
> fromSuperClass, value);
>                 }
>                 if (converter != null) {
>                     return converter;
>                 }
>             }
>
> BTW, you can use my test case in the patch to verify it.  (just ignore
> the ObjectConverter change).
>
>
> On 10/8/07, Hiram Chirino <hi...@hiramchirino.com> wrote:
> > I think it was trying to break out of the recursion.. but perhaps it's
> > doing the check at the wrong point.
> >
> > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > In method org.apache.camel.impl.converter.DefaultTypeConverter.findTypeConverter(),
> > > any reasons why it skips if "!fromSuperClass.equals(Object.class)"?
> > > My test case seems to work I leave that out since it is then try to
> > > find the converter for Object->Boolean.
> > >
> > >        Class fromSuperClass = fromType.getSuperclass();
> > >             if (fromSuperClass != null &&
> > > !fromSuperClass.equals(Object.class)) {
> > >                 TypeConverter converter = getTypeConverter(toType,
> > > fromSuperClass);
> > >                 if (converter == null) {
> > >                     converter = findTypeConverter(toType,
> > > fromSuperClass, value);
> > >                 }
> > >                 if (converter != null) {
> > >                     return converter;
> > >                 }
> > >             }
> > >
> > > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > > Good point.  I'll take a look at that.
> > > >
> > > > On 10/8/07, Guillaume Nodet <gn...@gmail.com> wrote:
> > > > > Shouldn't the converter registry be able to do that ? If there is a
> > > > > conversion from x to y available, it should use it whenever the object to be
> > > > > converted inherits / implements x.
> > > > >
> > > > > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > > > >
> > > > > > I wasn't able to convert a String to a Boolean using a route such as
> > > > > > from("direct:test").convertBodyTo(Boolean.class).  The problem is the
> > > > > > annotated converter method ObjectConverter.toBoolean(Object value) is
> > > > > > never reached.  The reason is that the converter is registered with
> > > > > > the key [class java.lang.Object=>class java.lang.Boolean] as taken
> > > > > > from the signature.  However, my route uses the key [class
> > > > > > java.lang.String=>class java.lang.Boolean] to look up a converter (so
> > > > > > none found)  since my in-message type is String (not Object).  I have
> > > > > > attached a patch which includes a unit test.
> > > > > >
> > > > > > Regards,
> > > > > > William
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Cheers,
> > > > > Guillaume Nodet
> > > > > ------------------------
> > > > > Blog: http://gnodet.blogspot.com/
> > > > >
> > > >
> > >
> >
> >
> > --
> > Regards,
> > Hiram
> >
> > Blog: http://hiramchirino.com
> >
>


-- 
James
-------
http://macstrac.blogspot.com/

Open Source SOA
http://open.iona.com

Re: Patch to convert from String to Boolean

Posted by William Tam <em...@gmail.com>.
I see.  I think we can do something like this instead.

           Class fromSuperClass = fromType.getSuperclass();
            if (fromSuperClass != null) {
                TypeConverter converter = getTypeConverter(toType,
fromSuperClass);
                if (converter == null && !fromSuperClass.equals(Object.class)) {
                    converter = findTypeConverter(toType,
fromSuperClass, value);
                }
                if (converter != null) {
                    return converter;
                }
            }

BTW, you can use my test case in the patch to verify it.  (just ignore
the ObjectConverter change).


On 10/8/07, Hiram Chirino <hi...@hiramchirino.com> wrote:
> I think it was trying to break out of the recursion.. but perhaps it's
> doing the check at the wrong point.
>
> On 10/8/07, William Tam <em...@gmail.com> wrote:
> > In method org.apache.camel.impl.converter.DefaultTypeConverter.findTypeConverter(),
> > any reasons why it skips if "!fromSuperClass.equals(Object.class)"?
> > My test case seems to work I leave that out since it is then try to
> > find the converter for Object->Boolean.
> >
> >        Class fromSuperClass = fromType.getSuperclass();
> >             if (fromSuperClass != null &&
> > !fromSuperClass.equals(Object.class)) {
> >                 TypeConverter converter = getTypeConverter(toType,
> > fromSuperClass);
> >                 if (converter == null) {
> >                     converter = findTypeConverter(toType,
> > fromSuperClass, value);
> >                 }
> >                 if (converter != null) {
> >                     return converter;
> >                 }
> >             }
> >
> > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > Good point.  I'll take a look at that.
> > >
> > > On 10/8/07, Guillaume Nodet <gn...@gmail.com> wrote:
> > > > Shouldn't the converter registry be able to do that ? If there is a
> > > > conversion from x to y available, it should use it whenever the object to be
> > > > converted inherits / implements x.
> > > >
> > > > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > > >
> > > > > I wasn't able to convert a String to a Boolean using a route such as
> > > > > from("direct:test").convertBodyTo(Boolean.class).  The problem is the
> > > > > annotated converter method ObjectConverter.toBoolean(Object value) is
> > > > > never reached.  The reason is that the converter is registered with
> > > > > the key [class java.lang.Object=>class java.lang.Boolean] as taken
> > > > > from the signature.  However, my route uses the key [class
> > > > > java.lang.String=>class java.lang.Boolean] to look up a converter (so
> > > > > none found)  since my in-message type is String (not Object).  I have
> > > > > attached a patch which includes a unit test.
> > > > >
> > > > > Regards,
> > > > > William
> > > > >
> > > > >
> > > >
> > > >
> > > > --
> > > > Cheers,
> > > > Guillaume Nodet
> > > > ------------------------
> > > > Blog: http://gnodet.blogspot.com/
> > > >
> > >
> >
>
>
> --
> Regards,
> Hiram
>
> Blog: http://hiramchirino.com
>

Re: Patch to convert from String to Boolean

Posted by Hiram Chirino <hi...@hiramchirino.com>.
I think it was trying to break out of the recursion.. but perhaps it's
doing the check at the wrong point.

On 10/8/07, William Tam <em...@gmail.com> wrote:
> In method org.apache.camel.impl.converter.DefaultTypeConverter.findTypeConverter(),
> any reasons why it skips if "!fromSuperClass.equals(Object.class)"?
> My test case seems to work I leave that out since it is then try to
> find the converter for Object->Boolean.
>
>        Class fromSuperClass = fromType.getSuperclass();
>             if (fromSuperClass != null &&
> !fromSuperClass.equals(Object.class)) {
>                 TypeConverter converter = getTypeConverter(toType,
> fromSuperClass);
>                 if (converter == null) {
>                     converter = findTypeConverter(toType,
> fromSuperClass, value);
>                 }
>                 if (converter != null) {
>                     return converter;
>                 }
>             }
>
> On 10/8/07, William Tam <em...@gmail.com> wrote:
> > Good point.  I'll take a look at that.
> >
> > On 10/8/07, Guillaume Nodet <gn...@gmail.com> wrote:
> > > Shouldn't the converter registry be able to do that ? If there is a
> > > conversion from x to y available, it should use it whenever the object to be
> > > converted inherits / implements x.
> > >
> > > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > > >
> > > > I wasn't able to convert a String to a Boolean using a route such as
> > > > from("direct:test").convertBodyTo(Boolean.class).  The problem is the
> > > > annotated converter method ObjectConverter.toBoolean(Object value) is
> > > > never reached.  The reason is that the converter is registered with
> > > > the key [class java.lang.Object=>class java.lang.Boolean] as taken
> > > > from the signature.  However, my route uses the key [class
> > > > java.lang.String=>class java.lang.Boolean] to look up a converter (so
> > > > none found)  since my in-message type is String (not Object).  I have
> > > > attached a patch which includes a unit test.
> > > >
> > > > Regards,
> > > > William
> > > >
> > > >
> > >
> > >
> > > --
> > > Cheers,
> > > Guillaume Nodet
> > > ------------------------
> > > Blog: http://gnodet.blogspot.com/
> > >
> >
>


-- 
Regards,
Hiram

Blog: http://hiramchirino.com

Re: Patch to convert from String to Boolean

Posted by William Tam <em...@gmail.com>.
In method org.apache.camel.impl.converter.DefaultTypeConverter.findTypeConverter(),
any reasons why it skips if "!fromSuperClass.equals(Object.class)"?
My test case seems to work I leave that out since it is then try to
find the converter for Object->Boolean.

       Class fromSuperClass = fromType.getSuperclass();
            if (fromSuperClass != null &&
!fromSuperClass.equals(Object.class)) {
                TypeConverter converter = getTypeConverter(toType,
fromSuperClass);
                if (converter == null) {
                    converter = findTypeConverter(toType,
fromSuperClass, value);
                }
                if (converter != null) {
                    return converter;
                }
            }

On 10/8/07, William Tam <em...@gmail.com> wrote:
> Good point.  I'll take a look at that.
>
> On 10/8/07, Guillaume Nodet <gn...@gmail.com> wrote:
> > Shouldn't the converter registry be able to do that ? If there is a
> > conversion from x to y available, it should use it whenever the object to be
> > converted inherits / implements x.
> >
> > On 10/8/07, William Tam <em...@gmail.com> wrote:
> > >
> > > I wasn't able to convert a String to a Boolean using a route such as
> > > from("direct:test").convertBodyTo(Boolean.class).  The problem is the
> > > annotated converter method ObjectConverter.toBoolean(Object value) is
> > > never reached.  The reason is that the converter is registered with
> > > the key [class java.lang.Object=>class java.lang.Boolean] as taken
> > > from the signature.  However, my route uses the key [class
> > > java.lang.String=>class java.lang.Boolean] to look up a converter (so
> > > none found)  since my in-message type is String (not Object).  I have
> > > attached a patch which includes a unit test.
> > >
> > > Regards,
> > > William
> > >
> > >
> >
> >
> > --
> > Cheers,
> > Guillaume Nodet
> > ------------------------
> > Blog: http://gnodet.blogspot.com/
> >
>

Re: Patch to convert from String to Boolean

Posted by William Tam <em...@gmail.com>.
Good point.  I'll take a look at that.

On 10/8/07, Guillaume Nodet <gn...@gmail.com> wrote:
> Shouldn't the converter registry be able to do that ? If there is a
> conversion from x to y available, it should use it whenever the object to be
> converted inherits / implements x.
>
> On 10/8/07, William Tam <em...@gmail.com> wrote:
> >
> > I wasn't able to convert a String to a Boolean using a route such as
> > from("direct:test").convertBodyTo(Boolean.class).  The problem is the
> > annotated converter method ObjectConverter.toBoolean(Object value) is
> > never reached.  The reason is that the converter is registered with
> > the key [class java.lang.Object=>class java.lang.Boolean] as taken
> > from the signature.  However, my route uses the key [class
> > java.lang.String=>class java.lang.Boolean] to look up a converter (so
> > none found)  since my in-message type is String (not Object).  I have
> > attached a patch which includes a unit test.
> >
> > Regards,
> > William
> >
> >
>
>
> --
> Cheers,
> Guillaume Nodet
> ------------------------
> Blog: http://gnodet.blogspot.com/
>

Re: Patch to convert from String to Boolean

Posted by Guillaume Nodet <gn...@gmail.com>.
Shouldn't the converter registry be able to do that ? If there is a
conversion from x to y available, it should use it whenever the object to be
converted inherits / implements x.

On 10/8/07, William Tam <em...@gmail.com> wrote:
>
> I wasn't able to convert a String to a Boolean using a route such as
> from("direct:test").convertBodyTo(Boolean.class).  The problem is the
> annotated converter method ObjectConverter.toBoolean(Object value) is
> never reached.  The reason is that the converter is registered with
> the key [class java.lang.Object=>class java.lang.Boolean] as taken
> from the signature.  However, my route uses the key [class
> java.lang.String=>class java.lang.Boolean] to look up a converter (so
> none found)  since my in-message type is String (not Object).  I have
> attached a patch which includes a unit test.
>
> Regards,
> William
>
>


-- 
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/