This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Create custom annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Create custom annotation" Watch "Create custom annotation" New topic
Author

Create custom annotation

Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Hi Friends,

How can we create a custom or our own annotation? What all we need to install? Are there any plugins in eclipse for the same? Please guide.

Thanks,


Poornima Sharma
SCJP 6.0
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

You can create your own annotations like this:

See this tutorial for more information.

You don't need to install anything special to create your own annotations. They are a standard part of the Java language since Java version 5.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Just don't expect the compiler to do anything special with them. There are only three annotations recognized by Oracle's compiler, and it's not a coincidence they are all located in the java.lang package; they are @Override, @Deprecated and @SuppressWarnings. Some IDEs add support for their own annotations to their own compilers. An example is the @NotNull annotation of IntelliJ IDEA.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
I went through the tutorial but it was not clear to me. Rob said that there is a @NotNull annotation by IntelliJ, how can we create such an annotation ourselves.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  87

You have not really described what you are trying to do. This makes it almost impossible to help you, as Annotations can cover chapters in books. But to give you a very simple example, I could create a simple annotation like:

And then use it in my application as:


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Hi Andrew,

Thanks for the reply, could you please let me know how can I learn annotations in deep, are there any books or any tutorials available? I am really curious to learn it but i could not find anything satisfactory on internet.

Thanks for your guidance.
Poornima Sharma
Ranch Hand

Joined: Sep 09, 2008
Posts: 114
Please guide
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  87

One of the best books (and not only for annotations - it is one of the best books a Java developer can have) is Effective Java (2nd Edition). It has several sections on annotations, and effectively shows how to create a framework such as JUnit using annotations.

Java (A Beginner's Tutorial) also has a chapter on annotations. However the link I gave you is for the Kindle edition, and while I know it is also available under Safari, I don't know if hard copy books are still available.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create custom annotation