opensubscriber
   Find in this group all groups
 
Unknown more information…

v : velocity-user@jakarta.apache.org 10 November 2006 • 11:52PM -0500

RE: How to get Type of variable
by THOMAS, BRIAN M (SBCSI)

REPLY TO AUTHOR
 
REPLY TO GROUP




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@yaho...]
> Sent: Friday, November 10, 2006 9:26 AM
> To: velocity-user@jaka...
> 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@jaka...
For additional commands, e-mail: velocity-user-help@jaka...


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jaka...
For additional commands, e-mail: velocity-user-help@jaka...


Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

Related Messages

opensubscriber is not affiliated with the authors of this message nor responsible for its content.