velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: Evaluating an enum with an abstract method
Date Fri, 31 Oct 2008 04:58:17 GMT
That's because greeter.getClass() returns FancyEnum$1 which is not a
public class.

You'll need to make messageFor(int) a non-abstract method, then let
the anonymous inner class override that:

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

        public String messageFor(int value) {
            throw UnsupportedOperationException("requires
implementation by subclass");
        }
    }


On Thu, Oct 30, 2008 at 8:00 PM, Daniel Wellman <etldan@gmail.com> wrote:
>
> Hello,
>
> 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
> "$myEnum.method()).
>
>
> 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("$fancyEnum.name()"));
>
>        // This fails and prints "$fancyEnum.messageFor(6)"
>        assertEquals("Hello there #6",
> runner.render("$fancyEnum.messageFor(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 {
>        Velocity.init();
>        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.
>
> Cheers,
> Dan
> --
> View this message in context: http://www.nabble.com/Evaluating-an-enum-with-an-abstract-method-tp20259439p20259439.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Mime
View raw message