vikingsteve
vikingsteve

Reputation: 40378

Error: "attribute value must be constant". Can I constract a constant from an enum at compile time?

The following code works fine, where PROCESS_UPDATES is a constant.

    public static final String PROCESS_UPDATES = "ProcessUpdates";
    public static final String PROCESS_UPDATES = "ProcessSnapshots";
    // etc....

    @Produce(uri = "seda:" + MyClass.PROCESS_UPDATES)
    protected ProducerTemplate processUpdatesTemplate;

However to avoid a billion constant strings all over the place, I was experimenting with an enum design pattern.

    public enum Route { ProcessUpdates, ProcessSnapshots }

In most circumstances, I can write "seda:" + Route.ProcessSnapshots and it looks much neater.

However my trusty test code now fails, because the uri in the annotation must be constant.

    @Produce(uri = "seda:" + MyClass.Route.ProcessUpdates)    // compiler error
    protected ProducerTemplate processUpdatesTemplate;

Error: Attribute value must be constant.

The thing is, that Route.ProcessUpdates.toString() kind of "is" a constant, but the compiler doesn't see it that way.

I head a read about constants and annotations but I didn't see any answer.

So is there a nice way to construct a constant String from an enum at compile-time in java?

Thanks for any tips. vikingsteve

Upvotes: 5

Views: 8413

Answers (1)

vikingsteve
vikingsteve

Reputation: 40378

I didn't find a solution to this.

As far as I can see, it's not possible.

I needed to use string constants... public static final String XYZ = "Xyz"; - etc.

Upvotes: 6

Related Questions