• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Create custom annotation

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please guide
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic