Annotation of enum members (JDK 1.5)

E

Emmeran Seehuber

Hi,

is it possible to annotate members of enums? It doesn't seem so -- at least
I can't get the values at runtime :(

This is my annotation class:

@Retention(RetentionPolicy.RUNTIME)
public @interface BCComment {
String value();
}

And this is the class containing the enum:

public class ByteCodeCommands {
public enum ByteCode {
@BCComment("Anfang einer Methode")
METHODPROLOG,
@BCComment("Focus auf die Instance-Variablen")
FOCUSSELFVARIABLES,
// ...
}

public static void main(String[] args) throws IOException {
// ...
for (ByteCode code : ByteCode.values()) {
Annotation[] annotations = ByteCode.class.getAnnotations();
Annotation[] annos = code.getClass().getAnnotations();
System.out.println(annotations.length);
System.out.println(annos.length);
BCComment comment = code.getClass().getAnnotation(BCComment.class);
System.out.println(comment);
}
// ...
}
}


When running this program it prints:
0
0
null

What is wrong with my code? Is it not possible to annotate enum members?

Thanks.

cu,
Emmy
 
T

Tony Morris

Emmeran Seehuber said:
Hi,

is it possible to annotate members of enums? It doesn't seem so -- at least
I can't get the values at runtime :(

This is my annotation class:

@Retention(RetentionPolicy.RUNTIME)
public @interface BCComment {
String value();
}

And this is the class containing the enum:

public class ByteCodeCommands {
public enum ByteCode {
@BCComment("Anfang einer Methode")
METHODPROLOG,
@BCComment("Focus auf die Instance-Variablen")
FOCUSSELFVARIABLES,
// ...
}

public static void main(String[] args) throws IOException {
// ...
for (ByteCode code : ByteCode.values()) {
Annotation[] annotations = ByteCode.class.getAnnotations();
Annotation[] annos = code.getClass().getAnnotations();
System.out.println(annotations.length);
System.out.println(annos.length);
BCComment comment = code.getClass().getAnnotation(BCComment.class);
System.out.println(comment);
}
// ...
}
}


When running this program it prints:
0
0
null

What is wrong with my code? Is it not possible to annotate enum members?

Thanks.

cu,
Emmy


You are attempting to get the annotation from the class when it appears on
the fields (not the class).
This is perhaps your intention:

for (Field f : ByteCode.class.getFields()) {
Annotation[] annotations = f.getAnnotations();
System.out.println(annotations.length);
BCComment comment = f.getAnnotation(BCComment.class);
System.out.println(comment);
}

Also, take a look at java.lang.annotation.Target to restrict the constructs
that a particular annotation can appear on.
 
E

Emmeran Seehuber

Tony Morris wrote:

[...]
You are attempting to get the annotation from the class when it appears on
the fields (not the class).
This is perhaps your intention:

for (Field f : ByteCode.class.getFields()) {
Annotation[] annotations = f.getAnnotations();
System.out.println(annotations.length);
BCComment comment = f.getAnnotation(BCComment.class);
System.out.println(comment);
}

Also, take a look at java.lang.annotation.Target to restrict the
constructs that a particular annotation can appear on.

Thank you! That works.

cu,
Emmy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top