Hi - sorry for the late reply. Some money making stuff was a bit important.

If I have a choice list like this:
          <choice>
                    <when>
                        <simple>${header.type} == ${type:Type.TYPE1}</simple>
                        <to uri="direct:toType1Handler"/>
                    </when>
                    <when>
                        <simple>${header.type} == ${type:Type.TYPE2}</simple>
                        <to uri="direct:toType2Handler"/>
                    </when>
                    <when>
                        <simple>${header.type} == ${type:Type.TYPE3}</simple>
                        <to uri="direct:toType3Handler"/>
                    </when>
                    <otherwise>
                        <log message="Don't know how to handle ${header.type} - 
ignoring" loggingLevel="WARN"/>
                    </otherwise>
                </choice>

With Type being:

public enum Type {

    TYPE1("This is type 1"), TYPE2("This is type 2"),TYPE3("This is type 3");



    private final String description;

    Type(String s) {
        this.description = s;
    }

    @Override
    public String toString() {
        return description;
    }

}


The header 'type' is an actual enum and not a string.

The method ObjectHelper.lookupConstantsFieldValue will do a comparison on all 
the field names and if found return the toString value of the field value. In 
this case is different than the default implementation of toString on enum 
which returns the name of the enum, thus the comparison in the choice list 
fails. It will always end up in the otherwise. If I were to use the default 
toString method it would work.

For the above example, the lookupConstantsFieldValue will for the first choice 
be given the values (Type.class, "TYPE1") - then lookupConstantsFieldValue will 
find TYPE1 field in the Type class, get the actual enum and then return the 
toString on the value, which in this case will be "This is type 1" causing the 
comparison to fail although it found the right one.

Hopefully it makes sense 🙂





Svend Ole Nielsen       Office +4596962640
Developer       
svend-ole.niel...@addsecure.com<mailto:svend-ole.niel...@addsecure.com>
        www.addsecure.com<http://www.addsecure.com/>
[cid:addsecure-logo_50e643a1-ceaa-48aa-817f-52769402fe0c.png]<http://www.addsecure.com/>
        [cid:fb_626b0b2c-0bba-4c8b-a645-72b180ce970e.png] 
<https://www.facebook.com/AddSecure/>   
[cid:li_2f98ce3b-2546-4c48-bcb2-9680df7d09ea.png] 
<https://www.linkedin.com/company/addsecure/>   
[cid:yt_8a8408e0-b9f1-4cca-9d75-77a496a95f00.png] 
<https://www.youtube.com/channel/UC9RMhgoc3z23vy93yBRAygA>
For a safer and smarter world
NOTE: This e-mail (including any attachments) is for the sole use of the 
intended recipient(s) and may contain information that  confidential and/or 
protected by legal privilege. Any unauthorized review, use, copy, disclosure or 
distribution of this e-mail is strictly prohibited. If you are not the intended 
recipient, please notify AddSecure immediately and destroy all copies of this 
e-mail. AddSecure does not accept any liability for breach of security, error 
or virus that may result from the trans­mission of this message.



________________________________
Fra: Claus Ibsen <claus.ib...@gmail.com>
Sendt: 15. marts 2023 12:33
Til: users@camel.apache.org <users@camel.apache.org>
Emne: Re: Enum comparing does not work when toString is overridden

CAUTION: External email
Hi

Can you maybe post some more details such as a bit of code of your enums. As we 
could possible improve that code to deal with enums in a specific way.

On Mon, Mar 6, 2023 at 12:30 PM Svend Ole Nielsen 
<svend-ole.niel...@addsecure.com<mailto:svend-ole.niel...@addsecure.com>> wrote:
Hi

I have been scratching my head for a couple of days wondering why my simple 
comparison in a choice list in Camel has always gone wrong. I'm comparing 
"complex" enums that have name and then a more human eatable name. This human 
eatable name is being printed out in an overridden toString method.

Found out that if I don't override toString everything works fine. It looks 
like there is something fishy going on with the 'lookupConstantFieldValue' 
method in ObjectHelper class. It does some comparing on name and eventually 
returns toString. Would have thought it would make more sense to return name - 
seen as in a normal non-overridden tostring that returns name.

Have searched Jira but found nothing related to this so wondering if I have 
stumpled upon a bug of some sorts







Svend Ole Nielsen       Office +4596962640
Developer       
svend-ole.niel...@addsecure.com<mailto:svend-ole.niel...@addsecure.com>
        www.addsecure.com<http://www.addsecure.com/>
[cid:186e50b5c732fe8053a1]<http://www.addsecure.com/>   
[cid:186e50b5c73900cd1922] <https://www.facebook.com/AddSecure/>   
[cid:186e50b5c73916028e73] <https://www.linkedin.com/company/addsecure/>   
[cid:186e50b5c7396547e524] 
<https://www.youtube.com/channel/UC9RMhgoc3z23vy93yBRAygA>
For a safer and smarter world
NOTE: This e-mail (including any attachments) is for the sole use of the 
intended recipient(s) and may contain information that  confidential and/or 
protected by legal privilege. Any unauthorized review, use, copy, disclosure or 
distribution of this e-mail is strictly prohibited. If you are not the intended 
recipient, please notify AddSecure immediately and destroy all copies of this 
e-mail. AddSecure does not accept any liability for breach of security, error 
or virus that may result from the trans­mission of this message.




--
Claus Ibsen
-----------------
@davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Reply via email to