• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custome Annotation

 
Amar Deep Singh Chaudhary
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ranchers

i want to create my custom annotation which perform some special task for we, for here just suppose that i want to show a message on console, how show i do that. i did this in this way-
i created a annotatin

// i expect that this should display the message-- Print My Annotation Executed. but it is not. is anyone will tell me that how i can achieve this through annotation and how can i return some value from annotation.

Please let me know if any one have some idea.

Thanks in Advance
Amar Deep Singh
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on EJB. For more information, please read this. This post has been moved to a more appropriate forum.

Also, please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.
 
Amar Deep Singh Chaudhary
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amar Deep Singh Chaudhary wrote:Hi, Ranchers

i want to create my custom annotation which perform some special task for we, for here just suppose that i want to show a message on console, how show i do that. i did this in this way-
i created a annotatin

// i expect that this should display the message-- Print My Annotation Executed. but it is not. is anyone will tell me that how i can achieve this through annotation and how can i return some value from annotation.

Please let me know if any one have some idea.

Thanks in Advance
Amar Deep Singh
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amar Deep Singh Chaudhary wrote:i expect that this should display the message-- Print My Annotation Executed. but it is not.

Why would you expect that? You have written no code to retrieve the value of the annotation.

But more importantly, why would you use an annotation at all for something like this?
 
Amar Deep Singh Chaudhary
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i have some other purpose of doing that, it's just prototype that how should i implement this. My question is that where i should need to write the actual implementation to print the message. Will you please get answer for me.


Thank You
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: peruse java.lang.Class
 
Amar Deep Singh Chaudhary
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you provide some example of doing that, i am not able to get that.
Thanks a lot
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you *really* trying to accomplish?

As implied in a previous response, annotations don't spontaneously execute themselves: annotations must be searched for *then* acted upon. For example, Struts 2 validation annotations are read at runtime by a class to determine what type of validation a property requires. Some annotations are processed at class load in order to augment the class's bytecode.

Without knowing what you're really trying to do it's impossible to help, but at the very least we can tell you that an annotation doesn't do anything by itself.
 
Amar Deep Singh Chaudhary
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you said that annotation can't do anything until we specify, i want to know that where we should define the implemtation of task for custom annotation, so that annotation can perform some task to us. i am conscious about implemntation of the logic which can deliver something for us. Here i want just to print a message and i want to know that where should i implement this.


Amar Deep Singh
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations don't do anything. Other parts of the program should use reflection to read the annotations on specific classes/methods/fields and decide what to do with them. Annotations are just metadata to mark your code for other parts of your application to make use of them. Finally, in your annotation, you should specify the target of the annotation (what can be annotated with this annotation), whether it's a field, method, another annotation, and/or a class/interface. An example on how to do this in your code is as follows



As you can see, using annotations, as using reflection as a whole, is not really that simple. You have to do lots of reflection, and the example I've shown you here is really simple since I know which particular method is annotated by which particular annotation. In a real life program you would do a lot of scanning of your classes to find the annotations on the classes/fields/methods and act according to the annotation and/or the values declared in them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic