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:[EMAIL PROTECTED] 
> 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to