You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Philip Savkin <ph...@hot.lv> on 2002/04/17 14:48:28 UTC

New object instantiation

Hi!

Is there any way to instantiate a new object instance?

I mean something like this:
#set ($rand = new java.util.Random())
$rand.nextInt()
...

Regards,
  Philip


----------------------------------
Why won't you also try? http://mail.hot.lv/


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


Re: New object instantiation

Posted by William Bajzek <w_...@yahoo.com>.
I did something similar by doing something like this:

context.put("HashMapClass",HashMap.class);

and then in the template:

#set ($map = $HashMapClass.newInstance())

or something like that.

=====
William Bajzek
http://william.bajzek.com/

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

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


Re: New object instantiation

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 9:21 AM, "Attila Szegedi" <sz...@freemail.hu> wrote:

> ----- Original Message -----
> From: "Geir Magnusson Jr." <ge...@optonline.net>
> To: <ve...@jakarta.apache.org>
> Sent: 2002. április 17. 14:50
> Subject: Re: New object instantiation
> 
> 
>> 
>> 
>> No, not in Velocity proper.  However, there is a tool in
>> commons/sandbox/rupert written by Christoph (I think) that does this, or
> you
>> could do it yourself as shown, modulo any real error checking :
> 
> ... and syntactic validity. Just try compiling a class with a method named
> "new" :-)

Oof.  Not enough coffee, clearly.

See the problem with glock's?

:)

> 
>> 
>>  public class MyGlock
>>  {
>>     public Object new(String classname)
>>     {
>>         return Class.forName(classname).newInstance();
>>     }
>>  }
>> 
> 
> Sorry, couldn't resist... :-)
> Attila.
> 

I wouldn't have been able to either. :)

Thank you.  I deserved that.

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting

The cost of synchronization is much less that the cost of stupidity.


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


Re: New object instantiation

Posted by Attila Szegedi <sz...@freemail.hu>.
----- Original Message -----
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: 2002. április 17. 14:50
Subject: Re: New object instantiation


>
>
> No, not in Velocity proper.  However, there is a tool in
> commons/sandbox/rupert written by Christoph (I think) that does this, or
you
> could do it yourself as shown, modulo any real error checking :

... and syntactic validity. Just try compiling a class with a method named
"new" :-)

>
>  public class MyGlock
>  {
>     public Object new(String classname)
>     {
>         return Class.forName(classname).newInstance();
>     }
>  }
>

Sorry, couldn't resist... :-)
Attila.


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


Re: New object instantiation

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/17/02 8:48 AM, "Philip Savkin" <ph...@hot.lv> wrote:

> Hi!
> 
> Is there any way to instantiate a new object instance?
> 
> I mean something like this:
> #set ($rand = new java.util.Random())
> $rand.nextInt()
> ...

Burn the heathen!

:)


No, not in Velocity proper.  However, there is a tool in
commons/sandbox/rupert written by Christoph (I think) that does this, or you
could do it yourself as shown, modulo any real error checking :

 public class MyGlock
 {
    public Object new(String classname)
    {
        return Class.forName(classname).newInstance();
    }
 }

So then 

  context.put("tool", new MyGlock());

And
  #set($rand = $tool.new("java.util.Random"))

Note that you are limited to classes that can be instantiated this way, and
of course, it's like a loaded gun in the wrong hands....

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"The greatest pleasure in life is doing what people say you cannot do."
        - Walter Bagehot



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