This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Annotations for Enum Constants Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Annotations for Enum Constants" Watch "Annotations for Enum Constants" New topic
Author

Annotations for Enum Constants

Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
I face a very strange problem
To explain it I use simple examples

I have an Annotation intValue as given below



I want to use this Annotation for Enum constants as given below

Now I try to find the annotation via Reflection

isAnnotationPresent(intValue.class) always returns false
I tried the following

but result is the same
Can you tell me what is wrong with this code.


I just want to know is there any body call my bean's Getter and Setter methods with "Please" in front - My favorite quip from Bugzilla
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

No idea, but why not just use the ordinal() method each enumerated object inherits from java.lang.Enum?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Or add a constructor to the enum...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Originally posted by Anoop Krishnan:
...

the annotation is not present on the class (enum Numbers)


it's still not present on the class,


[B]

should return true,
[/B]
[ January 03, 2008: Message edited by: Bill Shirley ]
[ January 03, 2008: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
Dear Friends

Thank you for your updates.

I am aware of the solutions which you suggested but my real problem is much more complex and I just explained it with a simple example

I want to find out why is it not possible to get the Annotation via reflection
I am sure that I am doing something wrong otherwise it should work.

Java doc for Annotation Target says ElementType.FIELD can be applied to Enum constants
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Well Bill already gave you the answer. The annotation is not for the class, so of course you can't get it through the class. It is for the enum constants which are in fact special kinds of fields.

To make it a bit more generic:

An enum constant's toString returns the name of the constant, so this code should work also for TWO and THREE.
Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
Thank you Bill Shirley

It works now
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Annotations for Enum Constants
 
Similar Threads
What is an interface what has only constants called?
enum: can anyone post the rules for enum declarations
Both imports work for enums ?
Gnerics Doubt.
Storing Enum Types