This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create custom annotation

 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please guide
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic