Enums and Annotations

Posted by PeterMmm on Stack Overflow See other posts from Stack Overflow or by PeterMmm
Published on 2010-03-27T19:53:16Z Indexed on 2010/03/27 20:03 UTC
Read the original article Hit count: 264

Filed under:
|

I want to use an Annotation in compile-safe form.

To pass the value() to the Annotation i want to use the String representation of an enum.

Is there a way to use @A with a value from enum E ?

public class T {

    public enum E {
        a,b;
    }

    // C1: i want this, but it won't compile
    @A(E.a)
    void bar() {

    // C2: no chance, it won't compile
    @A(E.a.toString())
    void bar2() {

    }
    // C3: this is ok
    @A("a"+"b")
    void bar3() {

    }

    // C4: is constant like C3, is'nt it ?
    @A(""+E.a)
    void bar4() {

    }
}

@interface A {
    String value();
}

© Stack Overflow or respective owner

Related posts about java

Related posts about annotations