Re: FM 2.3.12: NullPointerException in BlockAssignment.getCanonicalForm() when assign body is zero length

"Jonathan Revusky" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
On Tue, Apr 22, 2008 at 11:27 PM, Klotz, Leigh <[email protected]> wrote:
> Hello all; I've just joined this list, so please let me know if this
>  isn't the correct place to be posting .

Well, I suppose it's an okay place to be posting. The bug tracker is
also a possibility or maybe the dev list. We're not very picky about
it.

>
>  The following template gets an error in getCanonicalForm():
>
>  ---Failing Template---
>  <#ftl>
>  <#assign x></#assign>
>
>  ---Test Program---
>  import java.io.IOException;
>  import freemarker.template.Configuration;
>  import freemarker.template.Template;
>
>  public class CanonicalTest {
>
>     public static void main(String args[]) throws IOException {
>         String filename = args[0];
>         Configuration config = new Configuration();
>         Template t = config.getTemplate(filename);
>         System.out.print(t.getRootTreeNode().getCanonicalForm());
>     }
>  }
>
>  ---Execution Trace---
>  java -classpath
>  .\;c:/ds/svn1/docushare3/vendor/freemarker/freemarker.jar CanonicalTest
>  AssignFail.ftl
>  Exception in thread "main" java.lang.NullPointerException
>         at
>  freemarker.core.BlockAssignment.getCanonicalForm(BlockAssignment.java:15
>  0)
>         at
>  freemarker.core.MixedContent.getCanonicalForm(MixedContent.java:100)
>         at CanonicalTest.main(CanonicalTest.java:11)
>
>
>  This template will succeed:
>  ---Passing Template---
>  <#ftl>
>  <#assign x> </#assign>
>
>  I believe freemarker-2.3.12/src/freemarker/core/BlockAssignment.java
>  needs to have this:
>  ---Suggested Fix---
>   public String getCanonicalForm() {
>    ...
>         return "<#" + key + " " + varName +
>             (namespaceExp != null ? " in " +
>  namespaceExp.getCanonicalForm() : "")
>             + ">" + (nestedBlock != null ?
>  nestedBlock.getCanonicalForm() : "") + "</#" + key + ">";
>     }


Yes, that's a bug in 2.3.x it seems. It's actually not present in the
SVN head, which will (EVENTUALLY) be FM 2.4.

We're hitting the NPE if the block is empty. I just committed a fix in
the 2.3 branch. Thanks.

The bug is not just there BTW if you call getCanonicalForm(). It just
does bug out with a template like:

<#assign x></assign>${x}

The attempt to dereference caused an InvalidReferenceException. x was
never assigned, even though it should still be assigned to the empty
string, of course. I guess nobody ever reported this in so many years,
because the block within an assignment block is basically never empty
in practice.

By the way, why are you playing around with getCanonicalForm()? Just curious.

I think you should know that all that stuff has been completely
reworked in a much more flexible way in 2.4, so if you're interested
in writing some tool that mucks with FTL, you're better off probably
using that, even though it's prerelease.

See the freemarker.core.helpers.* package.

http://freemarker.svn.sourceforge.net/viewvc/freemarker/trunk/freemarker/src/freemarker/core/helpers/

In particular, DefaultTreeDumper and CanonicalizingTreeDumper

They implement the tree-walking API defined here:

http://freemarker.svn.sourceforge.net/viewvc/freemarker/trunk/freemarker/src/freemarker/core/ast/ASTVisitor.java?view=markup

That's the abstract base class.

So all that getCanonicalForm() stuff will basically be superseded by the above.

Oh, and probably any conversation that develops out of this might be
more appropriate on freemarker-dev, but it's a borderline case, I
suppose. We're not so strict about that.

Regards,

JR



which implements the API defined here:





>
>
>  Leigh.
>
>  -------------------------------------------------------------------------
>  This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
>  Don't miss this year's exciting event. There's still time to save $100.
>  Use priority code J8TL2D2.
>  http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
>  _______________________________________________
>  FreeMarker-user mailing list
>  [email protected]
>  https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.