You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Mark D. Anderson" <md...@discerning.com> on 2002/10/13 05:01:11 UTC

catching unbound methods?

I'm still trying to get an answer regarding how to catch unbound methods
(usually due to typing errors in templates).
Here is my question from before.

Thanks for any help....

-mda

----- Original Message ----- 
From: "Mark D. Anderson" <md...@discerning.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Monday, September 30, 2002 4:51 PM
Subject: catching syntax and spelling errors


If there is a VTL syntax error such as:
   #esle
then an exception is thrown.

If there is an unbound symbol such as 
   $claendar
then there is an event and (by default) a log warning.

The same is true (I believe) if there is a reference to an unbound member:
  $calendar.FirstDayOfWeak

But if there is a spelling mistake in a method name:
  $calendar.getFirstDayOfWeak()
Then there is no warning, no event, no nothing.

It is hard to tell with a speed-read where that might be done, but looking at
     org/apache/velocity/runtime/parser/node/ASTMethod.java
it appears that a missing method circumstance is just dropped on the floor.

Or maybe I'm missing something.....

-mda




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by Jonathan Revusky <re...@wanadoo.es>.
On Sunday 13 October 2002 08:00 am, Nathan Bubna wrote:
>
> i suggest you RTM (read the manuals) more carefully.  looking into the code
> at ASTMethod.java and such is NOT the way to begin.

Well, looking at the code does have one advantage, which is that docs 
sometimes (frequently, even) lie and the code does not.

Of course, there probably are legitimate reasons for discouraging people from 
looking at the code. People who eat sausages should be protected from knowing 
what is in them. ;-)

Cheers,

Jonathan Revusky
--
FreeMarker-Velocity comparison doc
http://freemarker.sourceforge.net/fmVsVel.html
Velocity->FreeMarker template conversion utility
http://freemarker.sourceforge.net/usCavalry.html


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by Derick Fernando <de...@xenocex.com>.
Perhaps this is more suited to the dev list, but what about a DEBUG mode
which reports undeclared varaibles?

Thanks,
Derick


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/13/02 4:55 PM, "Nathan Bubna" <na...@esha.com> wrote:

> Geir said:
> ...
>> I just want to note that you can get Velocity to tell you when it
> encounters
>> things that appear to be references ($<foo>) but don't have any backing
>> value.  There is a configuration parameter :
> ...
>> runtime.log.invalid.references
>> 
>> Set this to true and Velocity should log any non-quiet reference like
>> thingies w/o backing data.
> 
> ah.  well, i learn something new everyday.  now what about Mark's question?
> will this property log null returns from $foo.somenull ?  and would there be
> a difference between $foo.bar  (where getBar(), get("bar"), or whatever
> returns null)  and $foo.br  where no getBr(), get(String), or whatever can
> be found in $foo?

I am not 100% sure at this moment, but will try.

There should be no difference in how the reference is resolved - i.e. It
doesn't matter if the uberspector tries getBar(), get("bar") or GetBar()

I'll go figure it out :)

-- 
Geir Magnusson Jr. 
geirm@adeptra.com                                    +1-203-355-2219 (w)
Adeptra Inc.                                         +1-203-247-1713 (m)



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by Nathan Bubna <na...@esha.com>.
Geir said:
...
> I just want to note that you can get Velocity to tell you when it
encounters
> things that appear to be references ($<foo>) but don't have any backing
> value.  There is a configuration parameter :
...
> runtime.log.invalid.references
>
> Set this to true and Velocity should log any non-quiet reference like
> thingies w/o backing data.

ah.  well, i learn something new everyday.  now what about Mark's question?
will this property log null returns from $foo.somenull ?  and would there be
a difference between $foo.bar  (where getBar(), get("bar"), or whatever
returns null)  and $foo.br  where no getBr(), get(String), or whatever can
be found in $foo?

Nathan Bubna
nathan@esha.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by "Mark D. Anderson" <md...@discerning.com>.
> runtime.log.invalid.references
> 
> Set this to true and Velocity should log any non-quiet reference like
> thingies w/o backing data.

In my experience, this only deals with misspelled variable names, such as $nucular.
It doesn't report anything for correct variable names with misspelled method names,
such as $nuclear.getExpolsion().

-mda



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by "Mark D. Anderson" <md...@discerning.com>.
> runtime.log.invalid.references
> 
> Set this to true and Velocity should log any non-quiet reference like
> thingies w/o backing data.

In my experience, this only deals with misspelled variable names, such as $nucular.
It doesn't report anything for correct variable names with misspelled method names,
such as $nuclear.getExpolsion().

-mda



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/13/02 2:28 PM, "Nathan Bubna" <na...@esha.com> wrote:

> Mark said:
>> Nathan said:
>>> i suggest you RTM (read the manuals) more carefully.  looking into the
> code
>>> at ASTMethod.java and such is NOT the way to begin.
>> 
>> I've read them til I'm blue in the face; they don't give any sort of
> precise
>> description of how reflection is applied nor what happens when lookup
> fails.
> 
> 
> anyway, to deal more with your complaint...  again i repeat, you were wrong
> to say that plain $somenull references in the templates cause errors or
> warnings to be logged.   null reference errors are logged ONLY when the
> $somenull or $foo.somenull reference is within a VTL directive.
> 
> now, i can see someone having cause to need or want the *option* to log
> warnings when such references are encountered, but for myself, those would
> just annoy me.  as it is, i am occasionally annoyed by the errors logged
> when i do #set( $foo = $somenull ).   i have in the past, and will continue
> to support making such VTL valid and functional (i.e. having that code set
> the $foo reference to null instead of leaving it's previous value).  so, to
> get to my point, i don't agree that the current behaviour is a big problem.
> i was just trying to explain to you what the current behaviour is since you
> seem to be confused about it.


I just want to note that you can get Velocity to tell you when it encounters
things that appear to be references ($<foo>) but don't have any backing
value.  There is a configuration parameter :

As a constant :

RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID

or as a string literal

runtime.log.invalid.references

Set this to true and Velocity should log any non-quiet reference like
thingies w/o backing data.

Geir


-- 
Geir Magnusson Jr. 
geirm@adeptra.com                                    +1-203-355-2219 (w)
Adeptra Inc.                                         +1-203-247-1713 (m)



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by Nathan Bubna <na...@esha.com>.
Mark said:
> Nathan said:
> > i suggest you RTM (read the manuals) more carefully.  looking into the
code
> > at ASTMethod.java and such is NOT the way to begin.
>
> I've read them til I'm blue in the face; they don't give any sort of
precise
> description of how reflection is applied nor what happens when lookup
fails.

my apologies if you have read them, but you must admit you were quite
mistaken about velocity accessing class members.  the user-guide section on
References (http://jakarta.apache.org/velocity/user-guide.html#References)
talks about this.  i quote, " The Property $customer.Address has the exact
same effect as using the Method $customer.getAddress(). "

to my knowledge, you are correct that the precise reflection/method lookup
algorithm is not specified.  this would probably be a good area for
improvement in the docs.

anyway, to deal more with your complaint...  again i repeat, you were wrong
to say that plain $somenull references in the templates cause errors or
warnings to be logged.   null reference errors are logged ONLY when the
$somenull or $foo.somenull reference is within a VTL directive.

now, i can see someone having cause to need or want the *option* to log
warnings when such references are encountered, but for myself, those would
just annoy me.  as it is, i am occasionally annoyed by the errors logged
when i do #set( $foo = $somenull ).   i have in the past, and will continue
to support making such VTL valid and functional (i.e. having that code set
the $foo reference to null instead of leaving it's previous value).  so, to
get to my point, i don't agree that the current behaviour is a big problem.
i was just trying to explain to you what the current behaviour is since you
seem to be confused about it.

Nathan Bubna
nathan@esha.com



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by Jonathan Revusky <re...@wanadoo.es>.
On Sunday 13 October 2002 07:41 pm, Mark D. Anderson wrote:
> > i suggest you RTM (read the manuals) more carefully.  looking into the
> > code at ASTMethod.java and such is NOT the way to begin.
>
> I've read them til I'm blue in the face; they don't give any sort of
> precise description of how reflection is applied nor what happens when
> lookup fails.
>
> The point of my example was that "week" was misspelled as "weak".
> Thus
>    $calendar.getFirstDayOfWeak()
> is interpolated as an empty string (in my experience) with no warning at
> all. This makes templates very fragile. The person wanted:
>    $calendar.getFirstDayOfWeek()
> Thus typos in method names go undetected.

There has been an interesting debate recently on the Freemarker-devel list 
about this very subject. Earlier versions of FreeMarker (before 2.1) were 
very loose about undefined variables and, in fact, suffered from this exact 
problem, that if somebody misspelled a variable name, the template rendering 
machinery would just convert the variable into an empty string (or empty list 
or empty hash, case depending) and just keep chugging along as if nothing had 
happened.

I decided that, even at the cost of some transitional pain, we should move to 
stricter semantics and now (at least by default) references to undefined 
variables throw an exception. 

Interestingly enough, various people felt that the older semantics were 
better, and that it was better just to keep going (in the old way) when an 
error was encountered. Or, IOW, it was better to output an incomplete (or 
even incorrect) page than an error message. Well, that behavior can still be 
configured, but I cannot help but think that people preferred the legacy 
behavior (despite its dangerous implications) simply because it was... 
well.... the legacy behavior. They were used to it.

All of this has jogged my memory and it reminds me of an anecdote -- a very 
old anecdote, possibly apocryphal. I think this dates back to the 1980's when 
computer-generated form letters became a big thing.

A financial company had a list of what the financial industry calls "high 
net-worth individuals" (rich guys) to whom it sent a form letter pitching 
some investment scheme.

There was some bug in their templating mechanism and the letters were all sent 
out as something like:

Dear {rich_bastard}:

blah blah

<LOL>

Funny, though I suppose somebody did get fired.... :-(

What is striking to me is that this is the exact same scheme that Velocity 
uses. If, in your template, you have a variable exposed called, let's say 
$RichBastard

and a template writer mistakenly writes:

$rich_bastard or even $richBastard

then apparently the template engine just passes it along as schmoo, as in:

Dear $rich_bastard

blah blah

<ROTFL>

Of course, since this is the scheme that Velocity has used for the past 2 
years, it must be the result of some careful though.

I suppose.... Well, at least the licensing issues are clear. The Apache 
license does specifically disclaim any responsibility for any damages 
etcetera resulting from use of the software... So at least the juridical 
angle is covered well... ;-)

Cheers,

Jonathan Revusky
--
FreeMarker-Velocity comparison doc
http://freemarker.sourceforge.net/fmVsVel.html
Velocity->FreeMarker template conversion utility
http://freemarker.sourceforge.net/usCavalry.html


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by "Mark D. Anderson" <md...@discerning.com>.
> i suggest you RTM (read the manuals) more carefully.  looking into the code
> at ASTMethod.java and such is NOT the way to begin.

I've read them til I'm blue in the face; they don't give any sort of precise
description of how reflection is applied nor what happens when lookup fails.

The point of my example was that "week" was misspelled as "weak".
Thus
   $calendar.getFirstDayOfWeak()
is interpolated as an empty string (in my experience) with no warning at all.
This makes templates very fragile. The person wanted:
   $calendar.getFirstDayOfWeek()
Thus typos in method names go undetected.

This is not one of the existing EventHandlers -- you can get called if
a method invocation throws, but not if it does not exist.

-mda



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: catching unbound methods?

Posted by Nathan Bubna <na...@esha.com>.
Mark said:
...
> If there is an unbound symbol such as
>    $claendar
> then there is an event and (by default) a log warning.

i don't believe this is accurate.  to my knowledge, the null references only
cause errors when they are within a VTL directive.  e.g.

#set( $foo = $somenull )  or #if( $bar > $someothernull )bar!#end

a reference such as $somenull that is just out there in the template on its
own will be treated as just so much schmoo and render as '$somenull'

> The same is true (I believe) if there is a reference to an unbound member:
>   $calendar.FirstDayOfWeak
>
> But if there is a spelling mistake in a method name:
>   $calendar.getFirstDayOfWeak()
> Then there is no warning, no event, no nothing.

no, velocity does not access class members, only methods.  so the reference

$bar.foo will try to find a method like getFoo(), getfoo(), foo(), isFoo(),
and so on... (i'm not sure on the order or extent of the algorithm though, i
think it has recently changed)

thus,  $calendar.FirstDayOfWeak is no different than
$calendar.getFirstDayOfWeak()

i suggest you RTM (read the manuals) more carefully.  looking into the code
at ASTMethod.java and such is NOT the way to begin.

Nathan Bubna
nathan@esha.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>