seq_contains uses String representation for Java Objects

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

seq_contains uses String representation for Java Objects

Konrad Reiche
Hey everyone,

I am using Freemarker for my Java application, that's why I find myself
often processing whole Java
objects which eases the template creation.

Now I have noticed the following, when invoking `seq_contains` on a list
of Java Objects and not on,
for instance Strings or numbers, I receive true when both Objects have
the same String representation,
even though they are actually different Objects due to `hash()`,
`equals()` or `compareTo()`.

For now I have extended my `toString()` method to contain more
information, which seems to be better,
but is there another solution to my problem?

Best Konrad

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: seq_contains uses String representation for Java Objects

Daniel Dekany
That's a tricky one... The problem is that FreeMarker doesn't work
directly with the Java objects. Instead they go through a
transformation according the object_wrapper setting. After this
transformation doing identity comparisons is not possible in general.
Also, in your case, the objects will have string+hash type
(multi-typed value), an so ?seq_contains sees that these values are
strings, and so it just compare the string values.

To do identity comparison is possible if you can assume that all the
values that you compare are wrapped by the object_wrapper so that they
implement AdapterTemplateModel, which they do in your case. But
?seq_contains doesn't (and shouldn't) exploit that, but you could
write a TemplateMethodModelEx that does that.

--
Best regards,
 Daniel Dekany


Tuesday, July 31, 2012, 12:53:26 AM, Konrad Reiche wrote:

> Hey everyone,
>
> I am using Freemarker for my Java application, that's why I find myself
> often processing whole Java
> objects which eases the template creation.
>
> Now I have noticed the following, when invoking `seq_contains` on a list
> of Java Objects and not on,
> for instance Strings or numbers, I receive true when both Objects have
> the same String representation,
> even though they are actually different Objects due to `hash()`,
> `equals()` or `compareTo()`.
>
> For now I have extended my `toString()` method to contain more
> information, which seems to be better,
> but is there another solution to my problem?
>
> Best Konrad


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user