You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by MN...@tacintel.com on 2004/03/09 19:03:23 UTC

[PATCH] instanceof

usage:

#if ($foo instanceof "java.lang.String")
 - or -
#if ($foo instanceof $bar)

- only references are supported on the left-hand side
- right hand side is evaluated and toString() is passed to
ClassLoader.loadClass()

Here is the patch and the newly added files.

Not sure what the consensus is on the 'goodness' of having an 'instanceof'
check, but IMHO we are already presenting template developers with POJOs in
the context... Why not allow them to check instanceof?

Thanks,
Michael


Re: [PATCH] instanceof

Posted by Nathan Green <ng...@inco5.com>.
I feel like donating more code, so here goes:

public boolean instanceOf(Class c, Class n)
{
     return n.isAssignableFrom(c);
}
public boolean instanceOf(Object o, String className)
{
     try {
         return instanceOf(o.getClass(), Class.forName(className));
     } catch (ClassNotFoundException cnfe) {}
     return false;
}	

This code is more "correct" than what I posted previously.
The prior code had variable performance, and could be faster
or slower, depending on how many comparisons were required
to find a match.  This code has a fixed execution time,
which is almost completely dependent on Class.forName().


Geir Magnusson Jr wrote:
[SNIP]
> 
> Why is
> 
>   #if($tool.instanceOf($foo, "java.lang.String") == true )
> 
> so bad?
> 

Works for me.

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: [PATCH] instanceof

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
On Mar 9, 2004, at 1:03 PM, MNewcomb@tacintel.com wrote:

> usage:
>
> #if ($foo instanceof "java.lang.String")
>  - or -
> #if ($foo instanceof $bar)
>
> - only references are supported on the left-hand side
> - right hand side is evaluated and toString() is passed to
> ClassLoader.loadClass()
>
> Here is the patch and the newly added files.
>
> Not sure what the consensus is on the 'goodness' of having an 
> 'instanceof'
> check, but IMHO we are already presenting template developers with 
> POJOs in
> the context... Why not allow them to check instanceof?

Because class doesn't matter.  One of the cool things about VTL (and 
other things like it) is that the class of the object in the context is 
irrelevant.  For example, I could put either

public class Foo  {
   public String getName() {...}
}

or a map in the context as 'thingy', setup via :

   Map m = new HashMap();
   m.put("name", "geir");

and then in the template

    $thingy.name

and I get the desired results.  I as the designer don't need to care 
about the implementation of the model data, I just use it according to 
whatever the implicit contract says....

Adding 'instanceof' as an operator would be really contrary to the idea 
of simplicity and separation, and would really limit how the context 
could be populated for a given template.  That may not matter to you at 
all, but I've taken advantage of this countless times, using maps and 
such for the prototyping, and then when my model comes together, using 
real objects and never having to change the templates.

Why is

   #if($tool.instanceOf($foo, "java.lang.String") == true )

so bad?

-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org