[ 
https://issues.apache.org/jira/browse/VELOCITY-102?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12622908#action_12622908
 ] 

Adrian Tarau commented on VELOCITY-102:
---------------------------------------

Sorry, I meant to say "scan for static methods/fields of any object present in 
context and not only for a Class object".

For example I have a tool ViewTool and I'm using this tool as : 
context.put("view", new ViewTool()). 

I also have some public static fields and methods : ViewTool.CONTEXT or 
ViewTool.getPath() and I would like to have those accessible in my templates.

Maybe even to introduce in Context something like void put(Class staticTool) to 
allow automatic name creation. In my opinion it would be better to have in 
templates ViewTool.CONTEXT instead of view.CONTEXT cause you will "know" it is 
a static member access.Allowing the users to name the tool in the context could 
lead to some confusion when reading a template. Right now you know that every 
access to a member is done against an instance but if we introduce access to 
static members and static access will look like instance access could be 
confusing.

Of course it can creates conflicts, two classes with the same name but in 
different package cannot be used with this automatic naming but on the other 
hand not every class in your project actually goes in the context. 
As a workaround you can extend the class with static members and provide a 
different name to be used in the context.Also an exception could be threw in 
case of conflicts like this one.

Any thoughts?

> Add Support for Static Utility Classes
> --------------------------------------
>
>                 Key: VELOCITY-102
>                 URL: https://issues.apache.org/jira/browse/VELOCITY-102
>             Project: Velocity
>          Issue Type: Improvement
>          Components: Engine
>    Affects Versions: 1.3-rc1
>         Environment: Operating System: All
> Platform: All
>            Reporter: Juozas Baliuka
>            Priority: Minor
>             Fix For: 1.6
>
>
> Support for Static Utility Classes is:
> 1) define namespaces
>   
>   context.defineNamespace("Math").add( java.lang.Math.class  ) ;
>    used as $Math.sin(0)
> 2) Add / Replace / remove methods in namespace :
>   
> context.getNamespace("Math").add(MyRandom.class.getMethod("randomString",new 
> Class[]{int.class}))
>   used as $Math.randomString(12)
> 3) "union" on namespaces
>  context.defineNamespace("Utils")
>           .add(context.getNamespace("Math"))
>           .add(context.defineNamespace("Collections",Collections.class ) );
>  used as:
>   $Utils.sin(0)
>   $Utils.sort($list)
> 4) Global namespace
>   context.getGlobalNamespace().add( Math.class ); 
>   used as $sin(0)
> 5) inline namespaces:
>  #use java.lang.Math as Math
>    $Math.sin(0)
>  #end
>  #with Math
>    $sin(0)
>  #end

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to