Hi Marcus,
> The only reason it doesn't work in java without reflection is because you
> can't refer to an anonymous or private class.
Exactly.
> However, you can get the
> class instance from an instance of such a class and use it to access the
> method using relfection. In dynamically typed languages like velocity you
> don't have this problem since they always get the class from the object.
> That is, except in velocity where there are explicit checks to prevent
> overcoming this problem.
>
> It's very handy to do something like this:
>
> final Database db=getDatabase();
> context.put("user", new Object() {
> // users have a name and a shoe
> public String getName() {
> return db.findName(USER_ID);
> }
> public Object getShoe() {
> return new Object() {
> // shoes have a size and a make
> public int getSize() {
> return db.findShoeSize(USER_ID);
> }
> public String getMake() {
> return db.findShoeMake(USER_ID);
> }
> };
> }
> });
>
> It gets a lot less attractive if you have to make these classes named, and
> it gets really spaghetti if you had several nested anonymous classes (I
> have only 1 nested class above) and have to flatten these out.
I not quite sure of the underlying philosophy of Velocity here, but by
defining an interface, you can let Velocity recognize the methods.
eg. In your test example,
public interface User
{
public String getName();
public Object getShoe();
}
context.put("user", new User() {...});
I guess it's still worksome, but it gives the developers a clearer
view of the objects. :)
> Well, I can just remove those checks from my own copy of velocity for my own
> use.
The good thing about Velocity, is that you can customise the
introspection routine without altering the core library. For example,
the PublicFieldUberspect exposes public fields to introspection.
cf. http://wiki.apache.org/jakarta-velocity/PublicFieldUberspect
In your case, all you have to do is override Uberspect#getMethod() and
everything goes well!
Best regards,
-- Shinobu
--
Shinobu "Kawai" Yoshida <[EMAIL PROTECTED]>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]