You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by La...@MSCsoftware.com on 2004/09/04 16:21:44 UTC
creating new instances
Is it possible to create new instances of a class from a velocity
template? I'd like to do something like "#set($foo =
com.foo.bar.class.getConstructor().newInstance())". I know that doesn't
work, but is there another way to achieve that?
Larry Siden, IBS consultant, Viper team
x72612
Re: creating new instances
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Larry,
> We are writing Velocity (actually DVSL) macros for a framework in which we
> have no access to any of the Java source code. I'm looking for a hack
> that will allow me to create instances of classes that I write myself and
> place in the classpath.
Okay, then how about this?
## $dummy can be anything so long as it's an object
#set($dummy = "")
#set($foo = $dummy.class.forName("java.lang.StringBuffer").newInstance())
$foo.append("Hello ").append($bar).append("!")
This says hello to whatever is in $bar.
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai(shinobu.kawai@gmail.com)
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by La...@MSCsoftware.com.
My error, Shinobu. It was toolbox.props. I was able to confirm that the
classes were getting loaded, but couldn't access them from the DVSL macros
using $context.toolbox... or whatever it was. Haven't had much time to
look at this b/c we've been rushing to meet a deadline.
Larry Siden, IBS consultant, Viper team
x72612
Shinobu Kawai <sh...@gmail.com>
09/05/2004 07:19 PM
Please respond to
"Velocity Users List" <ve...@jakarta.apache.org>
To
Velocity Users List <ve...@jakarta.apache.org>
cc
Subject
Re: creating new instances
Hi Larry,
> I already tried going that route, w/ a toolbox.xml. I was able to
verify
> that the classes I refered to in toolbox.xml were being loaded. Whoever
> created this in-house framework I'm using did something in the DVSL
> sources and rebuilt it so that I still could not access them from the
> Velocity context. I think I tried something like
> "$context.toolbox.myClass ..." but nothing seemed to work.
I'm just skimming through the DVSL documents, so correct me if I'm wrong.
http://jakarta.apache.org/velocity/dvsl/users-guide.html
It looks like it doesn't use "toolbox.xml", but "toolbox.props". I know
that VelocityView uses "toolbox.xml". Maybe you can access them if you
use the property file instead.
But since you mention that "the classes I referred to in toolbox.xml were
being loaded", I'm guessing that you are doing that. IMHO, it might be
best if you can contact the in-house framework developers, or have
access to their source code. If it is unexpected behavior, it should be
fixed.
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai(shinobu.kawai@gmail.com)
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Larry,
> I already tried going that route, w/ a toolbox.xml. I was able to verify
> that the classes I refered to in toolbox.xml were being loaded. Whoever
> created this in-house framework I'm using did something in the DVSL
> sources and rebuilt it so that I still could not access them from the
> Velocity context. I think I tried something like
> "$context.toolbox.myClass ..." but nothing seemed to work.
I'm just skimming through the DVSL documents, so correct me if I'm wrong.
http://jakarta.apache.org/velocity/dvsl/users-guide.html
It looks like it doesn't use "toolbox.xml", but "toolbox.props". I know
that VelocityView uses "toolbox.xml". Maybe you can access them if you
use the property file instead.
But since you mention that "the classes I referred to in toolbox.xml were
being loaded", I'm guessing that you are doing that. IMHO, it might be
best if you can contact the in-house framework developers, or have
access to their source code. If it is unexpected behavior, it should be fixed.
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai(shinobu.kawai@gmail.com)
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by Mike Kienenberger <mk...@alaska.net>.
Lawrence.Siden@MSCsoftware.com wrote:
> I already tried going that route, w/ a toolbox.xml. I was able to verify
> that the classes I refered to in toolbox.xml were being loaded. Whoever
> created this in-house framework I'm using did something in the DVSL
> sources and rebuilt it so that I still could not access them from the
> Velocity context. I think I tried something like
> "$context.toolbox.myClass ..." but nothing seemed to work. But I'm going
> to try Shinobu's method using Class.forName right away!
If you have a toolbox entry like:
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
Then, you access it using $math.some_method_on_MathTool()
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by La...@MSCsoftware.com.
I already tried going that route, w/ a toolbox.xml. I was able to verify
that the classes I refered to in toolbox.xml were being loaded. Whoever
created this in-house framework I'm using did something in the DVSL
sources and rebuilt it so that I still could not access them from the
Velocity context. I think I tried something like
"$context.toolbox.myClass ..." but nothing seemed to work. But I'm going
to try Shinobu's method using Class.forName right away!
Larry Siden, IBS consultant, Viper team
x72612
Mike Kienenberger <mk...@alaska.net>
09/04/2004 11:33 AM
Please respond to
"Velocity Users List" <ve...@jakarta.apache.org>
To
Velocity Users List <ve...@jakarta.apache.org>
cc
Velocity Users List <ve...@jakarta.apache.org>
Subject
Re: creating new instances
Lawrence.Siden@MSCsoftware.com wrote:
> We are writing Velocity (actually DVSL) macros for a framework in which
we
> have no access to any of the Java source code. I'm looking for a hack
> that will allow me to create instances of classes that I write myself
and
> place in the classpath.
If you have the ability to specify velocity tools in a toolbox.xml file,
that will let you do just about anything you need.
-Mike
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by Mike Kienenberger <mk...@alaska.net>.
Lawrence.Siden@MSCsoftware.com wrote:
> We are writing Velocity (actually DVSL) macros for a framework in which we
> have no access to any of the Java source code. I'm looking for a hack
> that will allow me to create instances of classes that I write myself and
> place in the classpath.
If you have the ability to specify velocity tools in a toolbox.xml file,
that will let you do just about anything you need.
-Mike
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by La...@MSCsoftware.com.
We are writing Velocity (actually DVSL) macros for a framework in which we
have no access to any of the Java source code. I'm looking for a hack
that will allow me to create instances of classes that I write myself and
place in the classpath.
Larry Siden, IBS consultant, Viper team
x72612
Shinobu Kawai <sh...@gmail.com>
09/04/2004 10:52 AM
Please respond to
"Velocity Users List" <ve...@jakarta.apache.org>
To
Velocity Users List <ve...@jakarta.apache.org>
cc
Subject
Re: creating new instances
Hi Larry,
> Is it possible to create new instances of a class from a velocity
> template? I'd like to do something like "#set($foo =
> com.foo.bar.class.getConstructor().newInstance())". I know that doesn't
> work, but is there another way to achieve that?
Just out of curiosity, why do you want to do that?
Anyways, you can use reflection, I guess.
In java:
context.put("classes", this.getClass());
context.put("bar", "Larry");
In the template:
#set($foo = $classes.forName("java.lang.StringBuffer").newInstance())
$foo.append("Hello ").append($bar).append("!")
And you will get:
Hello Larry!
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai(shinobu.kawai@gmail.com)
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Larry,
> Is it possible to create new instances of a class from a velocity
> template? I'd like to do something like "#set($foo =
> com.foo.bar.class.getConstructor().newInstance())". I know that doesn't
> work, but is there another way to achieve that?
Just out of curiosity, why do you want to do that?
Anyways, you can use reflection, I guess.
In java:
context.put("classes", this.getClass());
context.put("bar", "Larry");
In the template:
#set($foo = $classes.forName("java.lang.StringBuffer").newInstance())
$foo.append("Hello ").append($bar).append("!")
And you will get:
Hello Larry!
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai(shinobu.kawai@gmail.com)
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: creating new instances
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Larry,
> Is it possible to create new instances of a class from a velocity
> template? I'd like to do something like "#set($foo =
> com.foo.bar.class.getConstructor().newInstance())". I know that doesn't
> work, but is there another way to achieve that?
I just found this Tool:
http://jakarta.apache.org/velocity/tools/generic/ToolLoader.html
Looks like it's going to disappear in 1.2, though.
http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/generic/ToolLoader.html
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org