• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Annotations for Enum Constants

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No idea, but why not just use the ordinal() method each enumerated object inherits from java.lang.Enum?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or add a constructor to the enum...
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bill Shirley

It works now
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic