aspose file tools*
The moose likes Java in General and the fly likes Custome Annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Custome Annotation" Watch "Custome Annotation" New topic
Author

Custome Annotation

Amar Deep Singh Chaudhary
Ranch Hand

Joined: Dec 26, 2006
Posts: 49
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


"A thinking mind never dies"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Amar Deep Singh Chaudhary
Ranch Hand

Joined: Dec 26, 2006
Posts: 49
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

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

Joined: Dec 26, 2006
Posts: 49
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

Hint: peruse java.lang.Class
Amar Deep Singh Chaudhary
Ranch Hand

Joined: Dec 26, 2006
Posts: 49
Can you provide some example of doing that, i am not able to get that.
Thanks a lot
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 26, 2006
Posts: 49
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

Joined: Jan 28, 2008
Posts: 467
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.


Visit my blog: http://jnassef.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custome Annotation