velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Wellman <>
Subject Evaluating an enum with an abstract method
Date Fri, 31 Oct 2008 03:00:49 GMT


We're in the process of upgrading from Velocity 1.4 to 1.5 and we've noticed
that calling Java 5 enum objects that implement an abstract method fail to
render in 1.5 (they succeed in 1.4).  Specifically, Velocity renders the
expression as writen (e.g. $myEnum.method() in a template gets output as

I've written an example test case which demonstrates the issue:

    public void parseAnEnumWithAnAbstractMethod() throws Exception {
        runner.push("fancyEnum", FancyEnum.greeter);

        // This works OK
        assertEquals("greeter", runner.render("$"));

        // This fails and prints "$fancyEnum.messageFor(6)"
        assertEquals("Hello there #6",

Here's what the enum class looks like:

    public enum FancyEnum {
        greeter {
            public String messageFor(int value) {
                return "Hello there #" + value;

        public abstract String messageFor(int value);

And here's the the code I used to exercise Velocity:

public class VelocityRunner {
    private VelocityContext context;

    public VelocityRunner() throws Exception {
        context = new VelocityContext();

    public void push(String name, Object object) {
        context.put(name, object);

    public String render(String inputString) throws Exception {
        StringWriter w = new StringWriter();
        Velocity.evaluate(context, w, "velocityRunner", inputString );
        return w.toString();

Am I missing something?

Thanks for your advice.

View this message in context:
Sent from the Velocity - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message