You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by rjain <rj...@yahoo.com> on 2006/11/10 16:26:23 UTC
How to get Type of variable
I have a ArrayList which can contain either items of TypeA or TypeB
#foreach($item in $arrayList)
#if ($item <isoftype> TypeA)
Do something
#else ($item <isoftype> TypeB)
Do something else
#end
#end
How do I get the Type of Item in the Arraylist?
Thanks!!
--
View this message in context: http://www.nabble.com/How-to-get-Type-of-variable-tf2608406.html#a7279003
Sent from the Velocity - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: How to get Type of variable
Posted by rjain <rj...@yahoo.com>.
Thanks Paul. I could get a simpler solution, created a method called
getTypeId for both the objects
#foreach ($branch in $cBranchList)
#if($branch.BizObject.getTypeId().equals("TypeA"))
#else
#end
#end
I haven't tried your solution, I guess it should work too.
Paul Loy wrote:
>
> Try doing a .class?
>
> #foreach($item in $arrayList)
> #set($classType = $item.class)
>
> #if ($classType == "com.domain.classname")
> do something
> #else
> don't do it!
> #end
> #end
>
> (this code isn't tested... just in my head!)
>
> rjain wrote:
>> I have a ArrayList which can contain either items of TypeA or TypeB
>>
>> #foreach($item in $arrayList)
>> #if ($item <isoftype> TypeA)
>> Do something
>> #else ($item <isoftype> TypeB)
>> Do something else
>> #end
>> #end
>>
>> How do I get the Type of Item in the Arraylist?
>>
>> Thanks!!
>>
>
>
> --
>
> *Paul Loy
> Software Developer*
>
> Peoples Archive Ltd.
> Middlesex House
> 34-42 Cleveland Street
> London, W1T 4LB
>
> Tel: +44 (0) 20 7323 0323
> Fax: +44 (0) 20 7580 1938
> paul.loy@peoplesarchive.com
>
>
>
--
View this message in context: http://www.nabble.com/How-to-get-Type-of-variable-tf2608406.html#a7279296
Sent from the Velocity - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: How to get Type of variable
Posted by Paul Loy <pa...@peoplesarchive.com>.
Try doing a .class?
#foreach($item in $arrayList)
#set($classType = $item.class)
#if ($classType == "com.domain.classname")
do something
#else
don't do it!
#end
#end
(this code isn't tested... just in my head!)
rjain wrote:
> I have a ArrayList which can contain either items of TypeA or TypeB
>
> #foreach($item in $arrayList)
> #if ($item <isoftype> TypeA)
> Do something
> #else ($item <isoftype> TypeB)
> Do something else
> #end
> #end
>
> How do I get the Type of Item in the Arraylist?
>
> Thanks!!
>
--
*Paul Loy
Software Developer*
Peoples Archive Ltd.
Middlesex House
34-42 Cleveland Street
London, W1T 4LB
Tel: +44 (0) 20 7323 0323
Fax: +44 (0) 20 7580 1938
paul.loy@peoplesarchive.com
RE: How to get Type of variable
Posted by "THOMAS, BRIAN M (SBCSI)" <bt...@att.com>.
Sorry; error:
> $item.class.equals($A.class)
>
> will tell you whether A is an instance of typeA.
I meant to say that it will tell you whether $item is an instance of the
same class as $A, which gives you what you want if $A is also an
instance of typeA.
brain[sic]
> -----Original Message-----
> From: THOMAS, BRIAN M (SBCSI)
> Sent: Friday, November 10, 2006 9:53 AM
> To: Velocity Users List
> Subject: RE: How to get Type of variable
>
>
> Because $item is of type java.lang.Object, $item.getClass()
> yields a java.lang.Class representing the class of $item,
> which has a method
> getName() returning a java.lang.String containing its fully
> qualified class name. Using Velocity's shortcut syntax which
> allows us to write
> $ref.getSomething() as $ref.something, then the expression
>
> $item.class.name
>
> will give you the fully-qualified class name of $item.
>
> If, as in your example, you don't need the classname as a
> string, then the expression
>
Or, if
> typeA is a java.lang.Class of the type that you want, it would be
>
> $item.class.equals(typeA)
>
> For other possibilities, you have all the public methods of
> java.lang.Class.
>
>
>
> brain[sic]
>
> > -----Original Message-----
> > From: rjain [mailto:rjain15@yahoo.com]
> > Sent: Friday, November 10, 2006 9:26 AM
> > To: velocity-user@jakarta.apache.org
> > Subject: How to get Type of variable
> >
> >
> >
> > I have a ArrayList which can contain either items of TypeA or TypeB
> >
> > #foreach($item in $arrayList)
> > #if ($item <isoftype> TypeA)
> > Do something
> > #else ($item <isoftype> TypeB)
> > Do something else
> > #end
> > #end
> >
> > How do I get the Type of Item in the Arraylist?
> >
> > Thanks!!
> > --
> > View this message in context:
> > http://www.nabble.com/How-to-get-Type-of-variable-tf2608406.ht
> ml#a7279003
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
RE: How to get Type of variable
Posted by "THOMAS, BRIAN M (SBCSI)" <bt...@att.com>.
Because $item is of type java.lang.Object, $item.getClass() yields a
java.lang.Class representing the class of $item, which has a method
getName() returning a java.lang.String containing its fully qualified
class name. Using Velocity's shortcut syntax which allows us to write
$ref.getSomething() as $ref.something, then the expression
$item.class.name
will give you the fully-qualified class name of $item.
If, as in your example, you don't need the classname as a string, then
the expression
$item.class.equals($A.class)
will tell you whether A is an instance of typeA. Or, if typeA is a
java.lang.Class of the type that you want, it would be
$item.class.equals(typeA)
For other possibilities, you have all the public methods of
java.lang.Class.
brain[sic]
> -----Original Message-----
> From: rjain [mailto:rjain15@yahoo.com]
> Sent: Friday, November 10, 2006 9:26 AM
> To: velocity-user@jakarta.apache.org
> Subject: How to get Type of variable
>
>
>
> I have a ArrayList which can contain either items of TypeA or TypeB
>
> #foreach($item in $arrayList)
> #if ($item <isoftype> TypeA)
> Do something
> #else ($item <isoftype> TypeB)
> Do something else
> #end
> #end
>
> How do I get the Type of Item in the Arraylist?
>
> Thanks!!
> --
> View this message in context:
> http://www.nabble.com/How-to-get-Type-of-variable-tf2608406.ht
ml#a7279003
Sent from the Velocity - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org