You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Kelvin Tan <ke...@relevanz.com> on 2001/10/25 04:26:44 UTC

Accessing class constants

I'm running Velocity 1.1. Pretty sure this has been asked before, but
couldn't find a reference to it in the mailing list archives.

Am I able to access class constants directly (ie not through an accessor
method)?

if class Foo has

public static final String HOME = "bar"

am I able to access $foo.HOME, or do I need to add an accessor method?

I looked through the source code, and there's no mention about fields in the
Introspector class, only methods. I could've missed it though.


Re: Accessing class constants

Posted by Bojan Smojver <bo...@binarix.com>.
Kelvin Tan wrote:
> 
> [snip]
> > > if class Foo has
> > >
> > > public static final String HOME = "bar"
> > >
> > > am I able to access $foo.HOME, or do I need to add an accessor method?
> > >
> > > I looked through the source code, and there's no mention about fields in
> the
> > > Introspector class, only methods. I could've missed it though.
> >
> > From what I can see in UG, you should have the method.
> >
> > Bojan
> >
> 
> Sorry, I don't really catch that. In UG?

Apologies. User's Guide.

> Do you think it's a better coding practise to include a getter method for
> the class constant?

I think that's the only way, actually.

Bojan

Re: Accessing class constants

Posted by Kelvin Tan <ke...@relevanz.com>.
[snip]
> > if class Foo has
> >
> > public static final String HOME = "bar"
> >
> > am I able to access $foo.HOME, or do I need to add an accessor method?
> >
> > I looked through the source code, and there's no mention about fields in
the
> > Introspector class, only methods. I could've missed it though.
>
> From what I can see in UG, you should have the method.
>
> Bojan
>

Sorry, I don't really catch that. In UG?

Do you think it's a better coding practise to include a getter method for
the class constant?

Thanks

Kelvin


Re: Accessing class constants

Posted by Bojan Smojver <bo...@binarix.com>.
Kelvin Tan wrote:
> 
> I'm running Velocity 1.1. Pretty sure this has been asked before, but
> couldn't find a reference to it in the mailing list archives.
> 
> Am I able to access class constants directly (ie not through an accessor
> method)?
> 
> if class Foo has
> 
> public static final String HOME = "bar"
> 
> am I able to access $foo.HOME, or do I need to add an accessor method?
> 
> I looked through the source code, and there's no mention about fields in the
> Introspector class, only methods. I could've missed it though.

>From what I can see in UG, you should have the method.

Bojan

Re: Accessing class constants

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 10/24/01 10:26 PM, "Kelvin Tan" <ke...@relevanz.com> wrote:

> I'm running Velocity 1.1. Pretty sure this has been asked before, but
> couldn't find a reference to it in the mailing list archives.
> 
> Am I able to access class constants directly (ie not through an accessor
> method)?
> 
> if class Foo has
> 
> public static final String HOME = "bar"
> 
> am I able to access $foo.HOME, or do I need to add an accessor method?
> 
> I looked through the source code, and there's no mention about fields in the
> Introspector class, only methods. I could've missed it though.
> 

I will put this in a FAQ area :)

It's in the archives.

We don't allow you to access class members.

But, there is a little tool called the 'FieldMethodizer' that you would use
like

Context.put("mything", new FieldMethodizer( foo ) );

And then in the template

$mything.HOME

Would work


-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.