[ 
https://issues.apache.org/jira/browse/VELOCITY-911?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16808766#comment-16808766
 ] 

Sergiu Dumitriu commented on VELOCITY-911:
------------------------------------------

[~luppim] I closed the issue because as it is stated, it is not valid. The 
evaluation is correct, according to the intended behavior.

If you feel that the documentation needs to be further enhanced to be less 
ambiguous about how being "empty" is defined, then feel free to create another 
issue.

About "and other objects that have a public isEmpty() method", this applies to 
all the other checks as well. In general, Velocity only calls public methods by 
default, so it's not relevant to mention that the method must be public here. 
And all method checks work simply by checking if such a method exists and is 
accessible, the actual type of the object isn't considered. I like your wording 
here: "$foo is an object with an isEmpty() method which evaluates to a true 
value". How about this instead: "$foo is an object with a public isEmpty() 
method which evaluates to true", and similar for all the other checks.

One option would be to put the longer technical description from the 
{{directive.if.empty_check}} definition into the {{If / ElseIf / Else}} 
definition in the user guide, but I feel that it is too technical for the user 
guide, which is supposed to be short and simple.

> Incorrect evaluation of IF-Directive for objects with isEmpty-method
> --------------------------------------------------------------------
>
>                 Key: VELOCITY-911
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-911
>             Project: Velocity
>          Issue Type: Bug
>    Affects Versions: 2.0, 2.1
>            Reporter: M. Luppi
>            Assignee: Claude Brisson
>            Priority: Major
>         Attachments: VelocityIsEmptyBugMCVE.java, mcve-project.zip, 
> template.vm
>
>
> With Velocity 2.0 the logic of evaluation for IF-Directives changed and now 
> uses an extended approach to determine whether it is true. This bug report 
> only refers to the case where $foo is an +object+. The documentation for 2.0 
> and 2.1 state as follows:
> {quote}The variable $foo is evaluated to determine whether it is true, which 
> will happen under one of those circumstances: [...]
>  $foo is an object (other than a string, a number or a collection) which is 
> not null
> _Source: 
> [http://velocity.apache.org/engine/2.0/user-guide.html#if-elseif-else|http://velocity.apache.org/engine/2.0/user-guide.html#if-elseif-else]_
> {quote}
> After a long debugging session I have found that objects with a public 
> isEmpty-method are not being handled according to the documentation cited 
> above. Thereby, such objects are evaluated as false if there is an 
> isEmpty-method that returns false. The correct evaluation result according to 
> the documentation should be true since the object is not null and not a 
> string, a number or a collection.
> It gets even more confusing when we take access modifiers into account. If 
> the access of the isEmpty-method is not public, the behaviour is as it should 
> be. When we make it public, however, the above explained bug occurs in 
> version 2.0 as well as 2.1.
> See the attached classes and the template or the whole project of the MCVE 
> (minimal, complete, and verifiable example) as ZIP.
> _The output of the test is as follows:_
> {code:java}
> null object
> -----------
> [ OK ] Expected.
> non-null object with isEmpty-method (public)
> --------------------------------------------
> isEmpty evaluates to true
> [FAIL] Not expected since object is not null.
> isEmpty evaluates to false
> [ OK ] Expected.
> non-null object with isEmpty-method (package-private)
> -----------------------------------------------------
> isEmpty evaluates to true
> [ OK ] Expected.
> isEmpty evaluates to false
> [ OK ] Expected.
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org
For additional commands, e-mail: dev-h...@velocity.apache.org

Reply via email to